为什么与期望不同 [英] Why different from expectation
问题描述
嗨:全部:
我对数据类型问题有疑问。根据C
标准。 IEEE 754
浮动4 3.4x10-38E..3.4x10 + 38E
双8 1.7x10-308E..1.7x10 + 308E
长双12 ???
代码函数是(0.000200-0.000300)*(0.000200-0.000300)
假设结果(差异)应该是10(-8)
的差异我声称是双倍的;
但是代码的结果是:
fp1 0.000200
------------------------
fp2 0.000300 >
相差0.000000
这意味着结果为零。
为什么?
谢谢
bin YE
Hi: All:
I have question with the data type problem. According to the C
standard. IEEE 754
float 4 3.4x10-38E..3.4x10+38E
double 8 1.7x10-308E..1.7x10+308E
long double 12 ???
The code function is (0.000200-0.000300)*(0.000200-0.000300)
suppose the result (difference ) should be the 10 (-8)
difference I claim is double;
But THe code result is :
fp1 0.000200
------------------------
fp2 0.000300
difference is 0.000000
which means the result is zero.
Why?
Thanks
bin YE
推荐答案
" yezi" <叶***** @ hotmail.com>写道:
"yezi" <ye*****@hotmail.com> writes:
我对数据类型问题有疑问。根据C
标准。 IEEE 754
浮动4 3.4x10-38E..3.4x10 + 38E
双8 1.7x10-308E..1.7x10 + 308E
长双12 ???
C标准允许但不要求IEEE 754浮点数。
代码函数为(0.000200-0.000300)*(0.000200-0.000300 )
假设结果(差异)应该是10(-8)
差异我声称是双倍的;
但是代码结果是:
fp1 0.000200
------------------------
fp2 0.000300
差异是0.000000
这意味着结果为零。
I have question with the data type problem. According to the C
standard. IEEE 754
float 4 3.4x10-38E..3.4x10+38E
double 8 1.7x10-308E..1.7x10+308E
long double 12 ???
The C standard allows, but does not require, IEEE 754 floating-point.
The code function is (0.000200-0.000300)*(0.000200-0.000300)
suppose the result (difference ) should be the 10 (-8)
difference I claim is double;
But THe code result is :
fp1 0.000200
------------------------
fp2 0.000300
difference is 0.000000
which means the result is zero.
很难说出你在描述什么问题。尝试发布
一些实际代码及其产生的输出,并描述
与预期输出的差异。
一些想法:
浮点数不精确。例如,值0.1不能是完全用二进制浮点表示的
;最接近的等价物
在double类型中,在我试过的一个系统上,是关于
0.10000000000000000555111512312578。
如果你''使用%f输出格式,它不会很好地显示非常好的小b值。打印结果0.000000并不意味着该值实际上等于0.0,只是它在一个接近0.0的某个范围内(其中) 1.0e-8可能是)。请尝试使用%g
格式;它转换为非常大的科学记数法或者非常小的价值。
-
Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>
圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>
我们必须做点什么。这是事情。因此,我们必须这样做。
It''s difficult to tell what problem you''re describing. Try posting
some actual code along with the output it produces, and describe how
it differs from the output you expected.
A couple of thoughts:
Floating-point is inexact. For example, the value 0.1 cannot be
represented exactly in binary floating-point; the closest equivalent
in type double, on one system I''ve tried, is about
0.10000000000000000555111512312578.
If you''re using a "%f" output format, it''s not going to show very
small values very well. A printed result of 0.000000 doesn''t imply
that the value is actually equal to 0.0, just that it''s within a
certain range close to 0.0 (which 1.0e-8 probably is). Try the "%g"
format instead; it switches to scientific notation for very large or
very small values.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
" yezi" <叶***** @ hotmail.com>写道:
"yezi" <ye*****@hotmail.com> writes:
我对数据类型问题有疑问。根据C
标准。 IEEE 754
浮动4 3.4x10-38E..3.4x10 + 38E
双8 1.7x10-308E..1.7x10 + 308E
长双12 ???
代码功能是(0.000200-0.000300)*(0.000200-0.000300)
假设结果(差异)应该是10(-8)
我声称的差异是双倍的;
但代码结果是:
fp1 0.000200
----------------------- -
fp2 0.000300
差值是0.000000
这意味着结果为零。
I have question with the data type problem. According to the C
standard. IEEE 754
float 4 3.4x10-38E..3.4x10+38E
double 8 1.7x10-308E..1.7x10+308E
long double 12 ???
The code function is (0.000200-0.000300)*(0.000200-0.000300)
suppose the result (difference ) should be the 10 (-8)
difference I claim is double;
But THe code result is :
fp1 0.000200
------------------------
fp2 0.000300
difference is 0.000000
which means the result is zero.
还有一件事我忘记了提及:阅读C FAQ的第14部分。
< http://www.eskimo.com/~scs/C-faq/top.html>。
>
-
Keith Thompson(The_Other_Keith) ks***@mib.org < ; http://www.ghoti.net/~kst>
圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>
我们必须做点什么。这是事情。因此,我们必须这样做。
One more thing I forgot to mention: Read section 14 of the C FAQ.
<http://www.eskimo.com/~scs/C-faq/top.html>.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
谢谢,我将首先阅读常见问题解答。
thanks, I will first read the FAQ .
这篇关于为什么与期望不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!