本文介绍了不正确的printf o / p表示双打的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我从下面的代码中得到错误的o / p:


#include< stdio.h>

#include< float.h>

#include< limits.h>

main()

{

双倍val,val1;

val = 123456789012.123456;

val1 = 123456789012.123456;

val2 = val * val1;

printf("%f\ n",val2);

}


我得到的结果是15241578753183967100000.000000而不是正确的值。


我在带有ansi c编译器的HP-UX 11上运行。


任何人都可以告诉我为什么我会得到这个结果。

Hi,

I am getting incorrect o/p from the code below:

#include<stdio.h>
#include<float.h>
#include<limits.h>
main()
{
double val,val1;
val=123456789012.123456;
val1=123456789012.123456;
val2=val*val1;
printf("%f\n",val2);
}

The result I get is 15241578753183967100000.000000 instead of the correct value.

I am running on HP-UX 11 with ansi c compiler.

Can anyone let me know why I am getting this result.

推荐答案




它看起来足够接近我。你有什么回答?


- glen



It looks close enough for me. What answer did you expect?

-- glen





看起来val2默认为int类型,而你正试图

打印出来就像双人一样。


- James



It looks like val2 is defaulting to an int type, and you are trying to
print it out like a double.

-- James





这似乎是你的机器能够出现的最佳近似值

- 浮点变量只有有限的精度。并且18

位数看起来并不太糟糕。欢迎来到美好的世界

浮点数学;-)因为你已经包含了< float.h>

检查编译器承诺了多少位数你打印出来的价格是
DBL_DIG的值。

问候,Jens

-

_ _____ _____

| || _ _ || _ _ |

_ | | | | | |

| | _ | | | | | |

\ ___ / ens | _ | homs | _ | oerring



That seems to the best approximation your machine is able to come up
with - floating point variables have only a finite precision. And 18
digits doesn''t look too bad. Welcome to the wonderful world of
floating point math ;-) And since you''re already including <float.h>
check how many digits the compiler is promising you by printing out
the value of DBL_DIG.
Regards, Jens
--
_ _____ _____
| ||_ _||_ _| Je***********@physik.fu-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring


这篇关于不正确的printf o / p表示双打的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 01:34