框架中的奇怪BUG? [英] Strange BUG in teh Framework?

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

问题描述

你好。

在进行简单的跟随计算时,放入变量的值

numMinusOne与计算显示在

观察窗口!!

这里是代码:

Dim xSng As Single = 6547.972

Dim yInt As Integer = 8000

Dim num As Integer = CInt(Math.Floor(xSng * yInt))

Dim numMinusOne As Integer = CInt(Math.Floor(xSng * yInt) - 1)


运行这4行后,numMinusOne将为:52383776

而Watch窗口将声明值为:

CInt(Math.Floor(xSng * yInt) - 1)= 52383775


BTW,Watch窗口是正确的。


如何来自numMinusOne没有得到正确的值?

hello.
when doing the simple following computation, the value put into the variable
numMinusOne is NOT the same as what the computation is showed to be in the
Watch window!!
here is the code:
Dim xSng As Single = 6547.972
Dim yInt As Integer = 8000
Dim num As Integer = CInt(Math.Floor(xSng * yInt))
Dim numMinusOne As Integer = CInt(Math.Floor(xSng * yInt) - 1)

After running these 4 lines, numMinusOne will be:52383776
while the Watch window will claim that the value of :
CInt(Math.Floor(xSng * yInt) - 1) = 52383775

BTW, the Watch window is correct.

how come the numMinusOne does not get the correct value?

推荐答案

David,

我不相信它是一个bug本身,由于存储浮点数如何存储的不精确性。


如果你将xSng更改为Double或Decimal,可以得到正确的结果。

有关浮点数如何在.NET中工作的完整讨论(和解释),请参阅:

http://www.yoda.arachsys .com / csharp / floatingpoint.html

http://www.yoda.arachsys.com/csharp/decimal.html


希望这会有所帮助

Jay

" David" <沓********* @ nowhere.com>在消息中写道

news:ep ************** @ TK2MSFTNGP10.phx.gbl ...
David,
I don''t believe its a bug per se, its due to the inexact nature of how
floating point numbers are stored.

If you change xSng to a Double or Decimal, you get the correct result.
For a complete discussion (and explaination) of how floating point numbers
work in .NET see:

http://www.yoda.arachsys.com/csharp/floatingpoint.html

http://www.yoda.arachsys.com/csharp/decimal.html

Hope this helps
Jay
"David" <Da*********@nowhere.com> wrote in message
news:ep**************@TK2MSFTNGP10.phx.gbl...
hello。
当进行简单的跟随计算时,输入
变量
numMinusOne的值与在
Watch窗口中显示的计算结果不同!!
这里是代码:
Dim xSng As Single = 6547.972
Dim yInt As Integer = 8000
Dim num As Integer = CInt(Math.Floor(xSng * yInt))
Dim numMinusOne作为Integer = CInt(Math.Floor(xSng * yInt) - 1)

运行这4行后,numMinusOne将为:52383776
而Watch窗口将声明值为:
CInt(Math.Floor(xSng * yInt) - 1)= 52383775

顺便说一下,Watch窗口是正确的。

为什么numMinusOne没有得到正确的值?
hello.
when doing the simple following computation, the value put into the
variable
numMinusOne is NOT the same as what the computation is showed to be in the
Watch window!!
here is the code:
Dim xSng As Single = 6547.972
Dim yInt As Integer = 8000
Dim num As Integer = CInt(Math.Floor(xSng * yInt))
Dim numMinusOne As Integer = CInt(Math.Floor(xSng * yInt) - 1)

After running these 4 lines, numMinusOne will be:52383776
while the Watch window will claim that the value of :
CInt(Math.Floor(xSng * yInt) - 1) = 52383775

BTW, the Watch window is correct.

how come the numMinusOne does not get the correct value?



David< Da ********* @ nowhere.com>写道:
David <Da*********@nowhere.com> wrote:
当进行简单的跟随计算时,放入变量
numMinusOne的值与在
Watch窗口中显示的计算结果不同!!
这里是代码:
Dim xSng As Single = 6547.972
Dim yInt As Integer = 8000
Dim num As Integer = CInt(Math.Floor(xSng * yInt))
Dim numMinusOne As Integer = CInt(Math.Floor(xSng * yInt) - 1)

运行这4行后,numMinusOne将为:52383776
而Watch窗口将声明那个值:
CInt(Math.Floor(xSng * yInt) - 1)= 52383775
BTW,Watch窗口是正确的。

怎么来的numMinusOne没有得到正确的值?
when doing the simple following computation, the value put into the variable
numMinusOne is NOT the same as what the computation is showed to be in the
Watch window!!
here is the code:
Dim xSng As Single = 6547.972
Dim yInt As Integer = 8000
Dim num As Integer = CInt(Math.Floor(xSng * yInt))
Dim numMinusOne As Integer = CInt(Math.Floor(xSng * yInt) - 1)

After running these 4 lines, numMinusOne will be:52383776
while the Watch window will claim that the value of :
CInt(Math.Floor(xSng * yInt) - 1) = 52383775

BTW, the Watch window is correct.

how come the numMinusOne does not get the correct value?




参见 http://www.pobox.com/~skeet/csharp/floatingpoint.html


你其实是计算6547.97216796875 * 8000然后拿下

楼层。


观察窗口可能会以稍微不同的方式进行计算 - 例如b $ b方式 - 例如使用Double而不是Single。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复该组,请不要给我发邮件



See http://www.pobox.com/~skeet/csharp/floatingpoint.html

You''re actually calculating 6547.97216796875 * 8000 and then taking the
floor of that.

The watch window is probably doing computation in a slightly different
way - e.g. using a Double instead of a Single.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


>你实际上在计算6547.97216796875 * 8000,然后取
> You''re actually calculating 6547.97216796875 * 8000 and then taking the
那个楼层。




hemm ......为什么会这样? Dim xSng As Single = 6547.972

表示我想使用... 6547.972,而不是6547.97216796875。你告诉我

6547.972的最佳代表是6547.97216796875 ??



hemm... why is that? Dim xSng As Single = 6547.972
means I want to use... 6547.972, not 6547.97216796875 . are you telling me
that the best representation for 6547.972 is 6547.97216796875 ??


这篇关于框架中的奇怪BUG?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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