浮点问题 [英] floating point problem

查看:62
本文介绍了浮点问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨......我遇到了关于浮点的奇怪问题

比较......问题是......


main()

{

浮动a = 0.7;

if(0.7 a)

printf(" hi");

else

printf(" hello");

}


i认为答案应该是打招呼...但奇怪的是它很好....但是

a = 0.8答案是你好...再次为a = 0.9答案是嗨.... />
程序在linux系统上运行....如果有人解释这个问题它

将非常有用... thanx提前...再见..


问候,


eric

hi....i have encountered strange problem regarding floating point
comparison...the problem is...

main()
{
float a=0.7;
if(0.7 a)
printf("hi");
else
printf("hello");
}

i think the answer should have hello...but strangely it is hi....but
for a=0.8 the answer is hello...again for a=0.9 the answer is hi....the
program is run on a linux system....if anybody explains this problem it
will be very helpful...thanx in advance...bye..

regards,

eric

推荐答案

< a href =mailto:di ********** @ yahoo.com> di ********** @ yahoo.com 写道:

hi ....我遇到了关于浮点的奇怪问题

比较......问题是......


main()

{

浮动a = 0.7;

if(0.7 a)

printf(" hi");

else

printf(" hello");

}


i认为答案应该有问好......但奇怪的是它是嗨....但是

a = 0.8答案是你好...再次a = 0.9答案是嗨....

程序是在linux系统上运行的。 ...如果有人解释这个问题它

将非常有帮助... ...提前...再见...
hi....i have encountered strange problem regarding floating point
comparison...the problem is...

main()
{
float a=0.7;
if(0.7 a)
printf("hi");
else
printf("hello");
}

i think the answer should have hello...but strangely it is hi....but
for a=0.8 the answer is hello...again for a=0.9 the answer is hi....the
program is run on a linux system....if anybody explains this problem it
will be very helpful...thanx in advance...bye..



欢迎到了浮点的欢乐,不精确的世界。请参阅

组的常见问题解答,所有内容都将被解释。


-

Ian Collins。

Welcome to the joyous, imprecise world of floating point. Consult the
group''s FAQ and all will be explained.

--
Ian Collins.



di ********** @ yahoo.com 写道:

hi ....我遇到了关于浮点的奇怪问题

比较..问题是......


main()

{

浮动a = 0.7;

if(0.7 a)

printf(" hi");

else

printf(" hello");

}


i认为答案应该有问好......但奇怪的是它是嗨....但是

a = 0.8你好答案是...你好...... = 0.9答案是嗨....

程序在linux系统上运行....如果有人解释这个问题它

将非常有帮助... ...提前感谢...再见..
hi....i have encountered strange problem regarding floating point
comparison...the problem is...

main()
{
float a=0.7;
if(0.7 a)
printf("hi");
else
printf("hello");
}

i think the answer should have hello...but strangely it is hi....but
for a=0.8 the answer is hello...again for a=0.9 the answer is hi....the
program is run on a linux system....if anybody explains this problem it
will be very helpful...thanx in advance...bye..



我得不到相同的结果。它确实说嗨对于第一个,但是

是你好对于第二和第三个案件。这是你的意思吗?

I don''t get the same results. It does say "hi" for the first one, but
is "hello" for the second and third cases. Is this what you meant?


> hi ....我遇到了关于浮点的奇怪问题
>hi....i have encountered strange problem regarding floating point

>比较......问题是......
>comparison...the problem is...




中没有大多数非整数值的确切表示
二进制浮点数。最接近的可能值可能高于或低于确切数学值的b $ b。


0.7为双倍:

之前:0.699999999999999844568776552478084340691566467285 156250000000

价值:0.699999999999999955591079014993738383054733276367 187500000000

后:0.700000000000000066613381477509392425417900085449 218750000000

0.7浮动:

之前: 0.699999928474426269531250000000000000000000000000 000000000000

价值:0.699999988079071044921875000000000000000000000000 000000000000

后:0.700000047683715820312500000000000000000000000000 000000000000


0.8双倍:

之前:0.799999999999999933386618522490607574582099914550 781250000000

价值:0.800000000000000044408920985006261616945266723632 812500000000

之后:0.800000000000000155431223447521915659308433532714 843750 000000

0.8浮动:

之前:0.799999952316284179687500000000000000000000000000 000000000000

价值:0.800000011920928955078125000000000000000000000000 000000000000

之后:0.800000071525573730468750000000000000000000000000 000000000000

There is no exact representation for most non-integer values in
binary floating point. The closest possible value may be above or
below the exact mathematical value.

0.7 as double:
Before: 0.699999999999999844568776552478084340691566467285 156250000000
Value: 0.699999999999999955591079014993738383054733276367 187500000000
After: 0.700000000000000066613381477509392425417900085449 218750000000

0.7 as float:
Before: 0.699999928474426269531250000000000000000000000000 000000000000
Value: 0.699999988079071044921875000000000000000000000000 000000000000
After: 0.700000047683715820312500000000000000000000000000 000000000000

0.8 as double:
Before: 0.799999999999999933386618522490607574582099914550 781250000000
Value: 0.800000000000000044408920985006261616945266723632 812500000000
After: 0.800000000000000155431223447521915659308433532714 843750000000

0.8 as float:
Before: 0.799999952316284179687500000000000000000000000000 000000000000
Value: 0.800000011920928955078125000000000000000000000000 000000000000
After: 0.800000071525573730468750000000000000000000000000 000000000000


>
main()

浮动a = 0.7;

if(0.7 a)

printf(" hi");

else

printf(" hello");
}

我认为答案应该是你好......但奇怪的是它是嗨...但是对于a = 0.8
答案是你好...再次a = 0.9答案是嗨.....
程序在linux系统上运行....如果有人解释这个问题它将非常有用...而且提前...再见..
>
main()
{
float a=0.7;
if(0.7 a)
printf("hi");
else
printf("hello");
}

i think the answer should have hello...but strangely it is hi....but
for a=0.8 the answer is hello...again for a=0.9 the answer is hi....the
program is run on a linux system....if anybody explains this problem it
will be very helpful...thanx in advance...bye..



Gordon L. Burditt

Gordon L. Burditt


这篇关于浮点问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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