简单数学中的错误 [英] Error in Simple Math
问题描述
在我的程序中,我试图做一个5.1 * 100的简单计算,如果我的成绩学校水平数学是正确的,那么
应该= 510。我的问题是vb.net
返回509.99999999999994作为答案。我做错了什么?
在.Net IDE的命令窗口中自行尝试。
在调试过程中在命令窗口输入?5.1 * 100。 />
谢谢,
Chris Baxter
cb **** *@pilotair.com
*" Chris Baxter" < CB ***** @ baxternet.net> scripsit:在我的程序中,我正在尝试对5.1 * 100进行简单的计算,如果我的成绩等级数学是正确的,那么
应该= 510。我的问题是vb.net
返回509.99999999999994作为答案。我做错了什么?
在.Net IDE的命令窗口中自行尝试。
在调试过程中在命令窗口输入?5.1 * 100。
< http://research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html>
-
Herfried K. Wagner
MVP·VB Classic,VB.NET
< http://www.mvps.org/dotnet>
你好Chris,
5.1 * 100对我们来说显然是510,因为我们认为是十进制的。
然而,将5.1转换为二进制浮点数,并且''二进制点'之后的数字
继续比可用的几个字节长一些。
因此, < storage>的事实5.1将引入一个错误 - 更不用说
乘以它。
你会认为它不需要很多位来存储5.1鉴于51
只需要6个!
51 = 32 + 16 + 2 + 1 = 110011
但5.1比这更复杂,因为它是从分数两个b
的两个幂构建的:
5.1 = 4 + 1 + 1/16 + 1 / 32 + 1/256 + 1/512 + 1/2048 + ...
= 11.00011001101000 ...
我们有在''二进制点'之后已经达到了14位数并且它仍然没有
准确,仅为5.09999765625。
有些数字只是穿上不喜欢被转换为两个权力的总和。
问候,
Fergus
再次克里斯,
我忘了添加:
如果你想过量服用,( - ),这里是's好的幻灯片演示使用v解释
浮点数清楚的例子。
http://www.cs.ualberta.ca/~amaral/co...loatingPoint/s
ld003.htm
问候
Fergus
In my program I am trying to do a simple calculation of 5.1 * 100 which
should =510 if my gradeschool level math is correct. My problem is vb.net
is returning 509.99999999999994 as the answer. Am I doing something wrong?
Try it for yourself in your Command Window in the .Net IDE.
Type ?5.1 * 100 in the command window during debugging.
Thanks,
Chris Baxter
cb*****@pilotair.com
* "Chris Baxter" <cb*****@baxternet.net> scripsit:In my program I am trying to do a simple calculation of 5.1 * 100 which
should =510 if my gradeschool level math is correct. My problem is vb.net
is returning 509.99999999999994 as the answer. Am I doing something wrong?
Try it for yourself in your Command Window in the .Net IDE.
Type ?5.1 * 100 in the command window during debugging.
<http://research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html>
--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Hi Chris,
5.1 * 100 is so obviously 510 to us because we think in decimal.
Convert 5.1 to a binary floating point number, however, and the digits
after the ''binary point'' go on somewhat longer than the few bytes available.
Thus the very fact of <storing> 5.1 is going to introduce an error - let alone
multiplying it.
You''d think that it wouldn''t need many bits to store 5.1 given that 51
only needs 6!
51 = 32 + 16 + 2 + 1 = 110011
But 5.1 is more complicated than this as it is built up from fractional
powers of two:
5.1 = 4 + 1 + 1/16 + 1/32 + 1/256 + 1/512 + 1/2048 + ...
= 11.00011001101000 ...
We''ve already got to 14 digits after the ''binary point'' and it''s still not
accurate, being only 5.09999765625.
Some numbers just don''t like being converted to sums of powers of two.
Regards,
Fergus
Hi again Chris,
I forgot to add:
If you want to overdose on this, ;-) , here''s a good slideshow explaining
floating point using very clear examples.
http://www.cs.ualberta.ca/~amaral/co...loatingPoint/s
ld003.htm
Regards
Fergus
这篇关于简单数学中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!