不正确的printf o / p表示双打 [英] incorrect printf o/p for doubles

查看:57
本文介绍了不正确的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.

推荐答案



" Yogesh Khanolkar" <哟************** @ azuresolutions.com>在留言中写道

news:b5 ************************** @ posting.google.c om ...

"Yogesh Khanolkar" <yo**************@azuresolutions.com> wrote in message
news:b5**************************@posting.google.c om...


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

#include< stdio.h>
#包括< float.h>
#include< limits.h>
main()
{
double 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


2003-10-30,Yogesh Khanolkar< yo ** ************@azuresolutions.com>写道:
On 2003-10-30, Yogesh Khanolkar <yo**************@azuresolutions.com> wrote:

#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);
}

#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);
}




看起来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


Yogesh Khanolkar< yo ***** *********@azuresolutions.com>写道:
Yogesh Khanolkar <yo**************@azuresolutions.com> wrote:
我从下面的代码得到错误的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);
}


这不会编译,因为''val2''是从未宣布。额外的

问题是在main()前面缺少''int''(当你不使用C99编译器时,你只能获得

)并且

最后缺少return语句 - main()总是返回一个int。

我得到的结果是15241578753183967100000.000000而不是正确的值。
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);
}
This won''t compile because ''val2'' is never declared. Additional
problems are the missing ''int'' in front of main() (you only get
away with this when you don''t use a C99 compiler) and the
missing return statement at the end - main() always returns an int.
The result I get is 15241578753183967100000.000000 instead of the correct value.




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

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

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

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

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

问候,Jens

-

_ _____ _____

| || _ _ || _ _ | Je *********** @ physik.fu-berlin.de

_ | | | | | |

| | _ | | | | | | http://www.physik.fu-berlin.de/~toerring

\ ___ / 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表示双打的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆