C语言 Double 和 Float 格式显示不同的结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18140331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:06:47  来源:igfitidea点击:

Double and Float format displaying different results

cfloating-pointdouble

提问by LinhSaysHi

I thought that the difference between double and float were the precision of the decimals. However, I am getting strange results with using double and float and they are no where close to one another.

我认为 double 和 float 之间的区别在于小数的精度。但是,我在使用 double 和 float 时得到了奇怪的结果,而且它们彼此并不接近。

The first segment of code is used with the float format producing the correct results:

第一段代码与浮点格式一起使用,产生正确的结果:

#include <stdio.h>

#define ABSOLUTE_VALUE(number)      ( ((number) < 0) ? -(number) : (number) )

int main (void)
{
    float number, absNumber;


    printf ("What number do you want to check the absolute value for? : ");
    scanf  ("%f", &number);

    absNumber = ABSOLUTE_VALUE(number);

    printf ("The absolute value of %.2f is %.2f\n", number, absNumber);

    return 0;
}

Output:
What number do you want to check the absolute value for? : -3
The absolute value of -3.00 is 3.00

输出:
您要检查哪个数字的绝对值?:-3
-3.00 的绝对值为 3.00

The second segment of code is used with the double format producing incorrect results:

第二段代码与 double 格式一起使用,产生不正确的结果:

#include <stdio.h>

#define ABSOLUTE_VALUE(number)      ( ((number) < 0) ? -(number) : (number) )

int main (void)
{
    double number, absNumber;


    printf ("What number do you want to check the absolute value for? : ");
    scanf  ("%d", &number);

    absNumber = ABSOLUTE_VALUE(number);

    printf ("The absolute value of %.2d is %.2d\n", number, absNumber);

    return 0;
}

Output:
What number do you want to check the absolute value for? : -3
The absolute value of -03 is 2147344384

输出:
您要检查哪个数字的绝对值?:-3
-03 的绝对值是 2147344384

回答by Yu Hao

In the second example, %dis used for integers. (disn't short for double, but for decimal)

在第二个示例中,%d用于整数。(d不是double 的缩写,而是decimal 的缩写)

scanf  ("%d", &number);

should be

应该

scanf  ("%lf", &number);

The printfis incorrect too, as %fis used for double

printf是不正确也一样,%f用于double

printf ("The absolute value of %.2d is %.2d\n", number, absNumber);

Instead, this should be:

相反,这应该是:

printf ("The absolute value of %.2f is %.2f\n", number, absNumber);


Notice that the format specifier for doubleis different for scanfand printf, this C FAQ articlehas some good explanations.

请注意,doubleforscanf和的格式说明符不同printf这篇 C FAQ 文章有一些很好的解释。

回答by Basile Starynkevitch

Your last printfformat string is wrong. Should be.

您的最后一个printf格式字符串是错误的。应该。

 printf ("The absolute value of %.2f is %.2f\n", number, absNumber);

and if you enable all compiler warnings(e.g. compile with gcc -Wall -g) you'll have a warning about that. Always enable all warnings in the compiler.

如果您启用所有编译器警告(例如 compile with gcc -Wall -g),您将收到有关该警告的警告。始终启用编译器中的所有警告。

Your scanfare also wrong, should be scanf("%lf", &number);and with all warnings the compiler would have warned you.

scanf也错了,应该是,scanf("%lf", &number);并且编译器会警告您所有警告。

BTW, scanfreturns the "number of successfully matched items" and you should test its result.

顺便说一句,scanf返回“成功匹配项目的数量”,您应该测试其结果。

You really need to carefully read the documentation of printf(3)and of scanf(3). (In a linux terminal, you could type man 3 printfto get it).

您确实需要仔细阅读printf(3)scanf(3)的文档。(在 linux 终端中,您可以键入man 3 printf以获取它)。

Notice that arguments are converted to doubleand the conversion specifier for double-s is %fin printfand %lfin scanf

请注意,参数被转换为double并且double-s的转换说明符是%finprintf%lfinscanf

And you should learn how to use a debugger(like gdb).

您应该学习如何使用调试器(如gdb)。

回答by Jigyasa

In your second program, you have used %d format specifier for double which is wrong. And that's why you are getting error. You should use %lf (long float or better called 'double') for printing it. For other format specifier, I think this might help you a link
You can also search different format specifiers on google by typing 'format specifiers'.

在您的第二个程序中,您已将 %d 格式说明符用于 double ,这是错误的。这就是为什么你会出错。您应该使用 %lf (长浮点数或更好地称为“double”)来打印它。对于其他格式说明,我想这可能会帮助你 的链接
,您还可以在谷歌键入“格式说明”搜索不同的格式说明。

回答by abe312

Make 2 changes for using doubledata-type in c :

在 c 中使用double数据类型进行2 处更改:

use %lf in scanf
use %f in printf

//remember : %d is for int
             %f is for float
             %c is for char

回答by Ed Heal

If you read the manual, "%d"is for integers. You need "%lf"instead.

如果您阅读手册"%d"则适用于整数。你需要"%lf"代替。

Ditto with the printf

同上 printf