1.15-1.13 = 0.1999998?请帮忙 [英] 1.15-1.13=0.1999998 ? Please help

查看:79
本文介绍了1.15-1.13 = 0.1999998?请帮忙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友,

我在两个不同的项目中有以下代码:

Dear friend,

I have the following code in two different projects:

Dim MySingle1 As Single = 1.15
Dim MySingle2 As Single = 1.13
Dim MyResult As Single = MySingle1 - MySingle2



但是最有趣的是,在一个项目 MyResult = 0.02 和另一个项目 MyResult = 0.01999998 中.

我没有解决这个问题.请给我建议.

提前谢谢您.

问候
SKPaul



But the most interesting thing is, in one project MyResult=0.02 and in another project MyResult=0.01999998.

I faild to solve this. Please please please suggest me.

Thanking you in advance.

Regards
SKPaul

推荐答案

这是由于浮点数在计算机中的保存方式以及在显示时如何取整.请参见此白皮书 [ ^ ]有关更多详细信息.
This is due to the way that floating point numbers are held in computers, and how they are rounded when being displayed. See this white paper[^] for more details.


浮点数学运算本质上是不准确的.如果需要准确性,则必须使用decimal类型.是的,在进行数学运算之前要进行转换很麻烦,但是如果要保证精度,则有必要进行转换.

顺便说一句,float(或double)将保持其值,直到您对其执行数学运算.到那时,不能保证它的值是完全准确的,也不能保证比较得到您期望的结果.

使用decimal是真正的路径.

尝试这种方式:

Floating point math is inherently inaccurate. If you want accuracy, you MUST use decimal types. Yes, it''s a hassle to convert before doing the math, but it''s necessary if you want guaranteed precision.

BTW, a float (or double) will maintain it''s value until you perform a math operation on it. At that point, it''s value is NOT guaranteed to be completely accurate, and comparisons are not guaranteed to have the results you expect.

Using decimal is the one true path.

Try it this way:

MyResult = Convert.ToSingle(Convert.ToDecimal(MySingle1) - Convert.ToDecimal(MySingle2)) 


并查看该值是否不完全符合您的期望.

为了避免强制转换,只需对所有SingleDouble变量开始使用十进制.

替代方法-使用Math.Round方法四舍五入到一定精度,但这是假数学.我更喜欢decimal类型的精度(即使对它们进行数学运算会花费更长的时间).


And see if the value isn''t precisely what you expect.

To avoid the casting, simply start using decimal for all of your Single or Double variables.

ALTERNATIVE - use the Math.Round method to round to a certain precision, but that''s fake math. I prefer the precision of the decimal type (even though it takes a little longer to do math ops on them).


我怀疑几乎没有差异-可能很好是输出数字而不是值本身的方式.

但是,如果它们在不同的项目中,所有选项是否都相同?
一个是32位,另一个是64位吗?
I suspect that there is little or no difference - it may well be the way you are outputting the numbers, rather than the values themselves.

However, if they are in different projects, are all the options the same?
Is one 32 bit, and the other 64?


这篇关于1.15-1.13 = 0.1999998?请帮忙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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