简体   繁体   English

modf function 无法正常工作

[英]modf function doesn't work properly

I'm trying to store all digits of a decimal number.I have decided to use modf for this purpose.My code segment is;我正在尝试存储十进制数的所有数字。我决定为此目的使用modf 。我的代码段是;

struct high_precision scan_high(int *j)
{
    int i,a;
    struct high_precision mynum;
    double num1, fracpart, intpart;
    printf("Enter the values> ");
    scanf("%lf", &num1);
    if( num1 < 0 )
        mynum.sign = -1;
    else
        mynum.sign = 1;
    num1 = fabs(num1);
    fracpart = modf(num1, &intpart);
    if ( intpart > 0 && intpart < 10 )
        a = 1;
    while( intpart == 0 ) {
        fracpart *= 10;
        fracpart = modf(fracpart, &intpart);
        a -= 1;
    }
    for(i=0;fracpart > 0 && intpart != 0;i++){
        if( intpart > 0 ){
            mynum.digits[i] = intpart;
        }
        fracpart *= 10;
        fracpart = modf(fracpart, &intpart);
    }
    *j = i;
    mynum.decpt = a;

    return(mynum);
}

But somehow it doesn't work as I want.For instance;但不知何故,它不能按我的意愿工作。例如;

Enter the values> 0.009876
    0.876000 9.000000
    0.760000 8.000000
    0.600000 7.000000

It must stop at this line.But, it is continuing to count;它必须停在这条线上。但是,它还在继续计数;

1.000000 5.000000
1.000000 9.000000
1.000000 9.000000
1.000000 9.000000
1.000000 9.000000
1.000000 9.000000
0.999998 9.000000
0.999977 9.000000
0.999767 9.000000
0.997669 9.000000
0.976694 9.000000
0.766942 9.000000
0.669420 7.000000
0.694198 6.000000
0.941983 6.000000
0.419827 9.000000
0.198267 4.000000
0.982671 1.000000
0.826707 9.000000
0.267069 8.000000
0.000000 2.000000
0.000000 0.000000

That's because floating point numbers aren't stored exactly (eg aren't exactly representable).那是因为浮点数没有精确存储(例如,不能精确表示)。 Here's an example you can use to illustrate this:这是一个可以用来说明这一点的示例:

#include <stdio.h>

int main() {
  double x = 0.009876;
  printf("%.20lf\n",x);
  return 0;
}

---------- Capture Output ----------
> "c:\windows\system32\cmd.exe" /c c:\temp\temp.exe
0.00987599999999999940

> Terminated with exit code 0.

I think you are not doing things properly, let me tell why: scanf("%lf", &num1);我认为您做事不正确,让我告诉您原因: scanf("%lf", &num1); . .

Here you are already converting the input to double... so you won't get this "high precision scan" you are trying because it is getting "lost" in that first conversion.在这里,您已经将输入转换为双精度......所以您不会得到您正在尝试的这种“高精度扫描”,因为它在第一次转换中“丢失”了。

If you want the actual decimal numbers, you should find the .如果你想要实际的十进制数字,你应该找到. in the ascii string and convert it to and int there.在 ascii 字符串中并将其转换为 int 。

EDIT I coded a little program, is this what you had in mind?编辑我编写了一个小程序,这是你的想法吗?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int split_decimal(const char * in, int * intpart, unsigned int * decimals) {

    char * dot = strchr(in, '.');

    if (!dot) {
        *intpart = atoi(in);
        *decimals = 0;
        return 0;
    }

    *decimals = atoi(dot+1);
    *intpart = atoi(in);

    return 0;
}

int main(int argc, char ** argv) {

    int intpart;
    unsigned int decimals;

    split_decimal("-1.337", &intpart, &decimals);
    printf("%d.%d\n", intpart, decimals);
    split_decimal("50", &intpart, &decimals);
    printf("%d.%d\n", intpart, decimals);

    return 0;
}

Output: Output:

-1.337
50.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM