浮点计算不准确? [英] floating point calculation inaccurate?

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

问题描述

我在C ++ 6.0中发现了浮点值的奇怪行为

到int:


如果我在版本调试时进入监视窗口6.0以下

术语:

(1.4 - 1.0)* 10.0

结果将是4.0 - 正如预期的那样。


但是如果我把它转换成int:

(int)(( - 1.4 - 1.0)* 10.0)

结果将是3!


我在.NET中尝试使用C#:

(1.4 - 1.0)* 10.0

的评估方式不同,有时候是4.019999999991,有时是

3.9999999999999991。

根据浮点计算的不同值,

转为int:

(int)(( - 1.4 - 1.0)* 10.0)

将分别产生结果3或4。


使用楼层和ceil或Math.Floor和Math.Ceiling不会改变任何东西,因为错误的评估将首先完成。


好​​的,我明白了在浮点计算必须有限制的b $ b精度。但是,我很惊讶这个简单的术语被错误地评估了




如何在日常练习中克服这个问题?

从不从double转回到int?

使用其他舍入算法?


你做什么?

问候,
Klaus

I have found strange behaviour in casting floating point values in C++ 6.0
to int:

If I enter in the watch window while debugging in version 6.0 the following
term:
(1.4 - 1.0) * 10.0
the result will be 4.0 - as expected.

But if I cast the same to int:
(int)((1.4 - 1.0) * 10.0)
the result will be 3!

I tried the same with C# in .NET:
(1.4 - 1.0) * 10.0
will be evaluated differently, sometimes 4.019999999991, sometimes
3.9999999999999991.
In according to the different values for the floating point calculation, the
cast to int:
(int)((1.4 - 1.0) * 10.0)
will yield into results 3 or 4, respectively.

Using floor and ceil or Math.Floor and Math.Ceiling does not change
anything, because the faulty evaluation will be done at first.

OK, I understand that floating point calculation must have limited
preciseness. However, I am surprised that this simple term is evaluated
wrongly.

How to overcome this in daily practice?
Never go back from double to int?
Using other rounding algorithm?

What do you do?
Regards,
Klaus

推荐答案



" Klaus Bonadt" <博**** @ hotmail.com> écritdansle message de news:

e2 ************** @ TK2MSFTNGP12.phx.gbl ...

"Klaus Bonadt" <Bo****@hotmail.com> a écrit dans le message de news:
e2**************@TK2MSFTNGP12.phx.gbl...
我有在C ++ 6.0中将浮点值转换为int时发现了奇怪的行为:

如果我在版本6.0中调试时进入监视窗口,那么
以下术语:
(1.4 - 1.0)* 10.0
结果将是4.0 - 正如预期的那样。

但如果我将它转换为int:
(int)((1.4 - 1.0) * 10.0)
结果将是3!

我在.NET中尝试使用C#:
(1.4 - 1.0)* 10.0
将以不同的方式进行评估,有时是4.019999999991,有时是
3.9999999999999991。
根据浮点计算的不同值,
转换为int:
(int)((1.4 - 1.0)* 10.0)
将分别产生结果3或4.

使用floor和ceil或Math.Floor和Math.Ceiling不会改变任何东西,因为错误的评估将会一开始就完成。

好的,我了解浮点计算必须具有有限的精确度。但是,我很惊讶这个简单的术语被错误地评估了。

如何在日常练习中克服这个问题?
永远不要从双重回到int?
使用其他舍入算法?

你做什么?
问候,
Klaus
I have found strange behaviour in casting floating point values in C++ 6.0
to int:

If I enter in the watch window while debugging in version 6.0 the following term:
(1.4 - 1.0) * 10.0
the result will be 4.0 - as expected.

But if I cast the same to int:
(int)((1.4 - 1.0) * 10.0)
the result will be 3!

I tried the same with C# in .NET:
(1.4 - 1.0) * 10.0
will be evaluated differently, sometimes 4.019999999991, sometimes
3.9999999999999991.
In according to the different values for the floating point calculation, the cast to int:
(int)((1.4 - 1.0) * 10.0)
will yield into results 3 or 4, respectively.

Using floor and ceil or Math.Floor and Math.Ceiling does not change
anything, because the faulty evaluation will be done at first.

OK, I understand that floating point calculation must have limited
preciseness. However, I am surprised that this simple term is evaluated
wrongly.

How to overcome this in daily practice?
Never go back from double to int?
Using other rounding algorithm?

What do you do?
Regards,
Klaus




浮点数数字不允许代表所有数字,这就是为什么你输入
在输入整数值时可能会有一些不准确之处。因此,所有浮动的b $ b点计算都是一致的,只需将avoir转换为int,并且可以使用epsilon,并使用epsilon。测试双值。



floating point numbers don''t allow to represent all numbers, that''s why you
may have some inaccuracy when casted into integer values. Thus, all floating
point computations are consistent, just avoir casting to int as much as
possible, and use an "epsilon" to test double values.


Klaus Bonadt写道:
Klaus Bonadt wrote:
我发现在将C ++ 6.0中的浮点值转换为奇怪的行为int:

如果我在版本6.0中调试时进入监视窗口
以下术语:
(1.4 - 1.0)* 10.0
结果将是4.0 - 正如预期的那样。

但是,如果我将相同的内容转换为int:
(int)(( - 1.4 - 1.0)* 10.0)
结果将是3!

我在.NET中尝试使用C#:
(1.4 - 1.0)* 10.0
会有不同的评价,有时候是4.019999999991,有时是
3.9999999999999991。
根据浮点数计算的不同值,转换为int:
(int)((1.4 - 1.0)* 10.0)
将分别产生结果3或4。

使用floor和ceil或者Math.Floor和Math.Ceiling不会改变任何东西,因为错误的评估将首先完成。

好的,我unders并且浮点计算必须具有有限的精确度。但是,我很惊讶这个简单的术语被错误地评估了。


这没有错,这只是浮点计算的本质。

如何在日常实践中克服这个问题?
永远不要从double返回到int?
使用其他舍入算法?
I have found strange behaviour in casting floating point values in
C++ 6.0 to int:

If I enter in the watch window while debugging in version 6.0 the
following term:
(1.4 - 1.0) * 10.0
the result will be 4.0 - as expected.

But if I cast the same to int:
(int)((1.4 - 1.0) * 10.0)
the result will be 3!

I tried the same with C# in .NET:
(1.4 - 1.0) * 10.0
will be evaluated differently, sometimes 4.019999999991, sometimes
3.9999999999999991.
In according to the different values for the floating point
calculation, the cast to int:
(int)((1.4 - 1.0) * 10.0)
will yield into results 3 or 4, respectively.

Using floor and ceil or Math.Floor and Math.Ceiling does not change
anything, because the faulty evaluation will be done at first.

OK, I understand that floating point calculation must have limited
preciseness. However, I am surprised that this simple term is
evaluated wrongly.
It''s not wrong, it''s just the nature of floating point calculations.

How to overcome this in daily practice?
Never go back from double to int?
Using other rounding algorithm?




学习如何正确舍入浮点计算。例如,当

转换为int时,添加0.5然后截断(假设您想要向正无限的圆形

- 根据其他舍入模型的需要进行调整)。


-cd



Learn how to round floating point calculations properly. For example, when
converting to int, add 0.5 and then truncate (assuming you want round
towards positive infinty - adjust as needed for other rounding models).

-cd


使用Convert类进行舍入。如果你施放你只是得到整数部分

ofcourse。


转换类为你做舍入。

" Carl Daniel [VC ++ MVP]" < cp ***************************** @ mvps.org.nospam>

写道消息新闻:O%**************** @ tk2msftngp13.phx.gbl ...
Use the Convert class to round. If you cast you just get the integer part
ofcourse.

Convert class does the rounding for you.
"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:O%****************@tk2msftngp13.phx.gbl...
Klaus Bonadt写道:
Klaus Bonadt wrote:
我在将C ++ 6.0中的浮点值转换为int时发现了奇怪的行为:

如果我在6.0版本的调试中进入监视窗口,那么下面的术语:
(1.4 - 1.0)* 10.0
结果将是4.0 - 正如预期的那样。

但如果我将相同的内容转换为int:
(int)((1.4 - 1.0)* 10.0)
结果将是3!

我在.NET中尝试使用C#:
(1.4 - 1.0)* 10.0
将是评价不同,有时候是4.019999999991,有时是
3.9999999999999991。
根据浮点数计算的不同值,转换为int:
(int)((1.4 - 1.0) )* 10.0)
将分别产生结果3或4.

使用floor和ceil或Math.Floor和Math.Ceiling d oes不会改变什么,因为错误的评估将首先完成。

好的,我知道浮点计算必须具有有限的精确性。但是,我很惊讶这个简单的术语被错误地评估了。
这没有错,它只是浮​​点计算的本质。
I have found strange behaviour in casting floating point values in
C++ 6.0 to int:

If I enter in the watch window while debugging in version 6.0 the
following term:
(1.4 - 1.0) * 10.0
the result will be 4.0 - as expected.

But if I cast the same to int:
(int)((1.4 - 1.0) * 10.0)
the result will be 3!

I tried the same with C# in .NET:
(1.4 - 1.0) * 10.0
will be evaluated differently, sometimes 4.019999999991, sometimes
3.9999999999999991.
In according to the different values for the floating point
calculation, the cast to int:
(int)((1.4 - 1.0) * 10.0)
will yield into results 3 or 4, respectively.

Using floor and ceil or Math.Floor and Math.Ceiling does not change
anything, because the faulty evaluation will be done at first.

OK, I understand that floating point calculation must have limited
preciseness. However, I am surprised that this simple term is
evaluated wrongly.
It''s not wrong, it''s just the nature of floating point calculations.

如何在日常练习中克服这个问题?
永远不要从双向回到int?
使用其他舍入算法?

How to overcome this in daily practice?
Never go back from double to int?
Using other rounding algorithm?



学习如何圆形浮点计算正确。例如,



Learn how to round floating point calculations properly. For example,



转换为int时,添加0.5然后截断(假设您想要向正无限循环
- 根据需要调整其他舍入模型)。

-cd


when converting to int, add 0.5 and then truncate (assuming you want round
towards positive infinty - adjust as needed for other rounding models).

-cd



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

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