为什么1.2%0.01不等于零? [英] Why does 1.2 % 0.01 not equal to zero?

查看:168
本文介绍了为什么1.2%0.01不等于零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于舍入误差,余数不为零。如何删除

舍入错误?

The remainder is non zero due to rounding errors. How can I remove the
rounding errors?

推荐答案

On Sun,2008年8月3日14:54:26 -0700 ,Bernard.Mangay< fc **** @ googlemail.com>

写道:
On Sun, 03 Aug 2008 14:54:26 -0700, Bernard.Mangay <fc****@googlemail.com>
wrote:

由于四舍五入,余数不为零错误。如何删除

舍入错误?
The remainder is non zero due to rounding errors. How can I remove the
rounding errors?



与处理任何浮点舍入错误的方法相同

计算:不要指望确切的结果。相反,任何时候你想做比较,你需要决定接近足够好的程度。并且

将其纳入您的比较中。


例如:


double result = 1.2%0.01;


if(结果< 0.000001)

{

//也可能为零!

}

The same way you deal with rounding errors with any floating point
calculation: don''t expect exact results. Instead, any time you want to do
a comparison, you need to decide how close is "good enough" and
incorporate that into your comparison.

For example:

double result = 1.2 % 0.01;

if (result < 0.000001)
{
// might as well be zero!
}


8月3日,11:01 * pm,Peter Duniho < NpOeStPe ... @nnowslpianmk.com>

写道:
On Aug 3, 11:01*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:

On Sun,2008年8月3日14:54:26 -0700 ,Bernard.Mangay< fcm ... @ googlemail.com *

写道:
On Sun, 03 Aug 2008 14:54:26 -0700, Bernard.Mangay <fcm...@googlemail.com*
wrote:

由于舍入误差,余数不为零。如何删除

舍入错误?
The remainder is non zero due to rounding errors. How can I remove the
rounding errors?



处理任何浮点舍入错误的方式相同*

计算:不要指望确切的结果。 *相反,任何时候你想要比较,你需要决定足够好的接近程度。并且*

将其纳入您的比较中。


例如:


* * * double result = 1.2 %0.01;


* * * if(结果< 0.000001)

* * * {

* * * * * //也可能是零!

* * *}


The same way you deal with rounding errors with any floating point *
calculation: don''t expect exact results. *Instead, any time you want todo *
a comparison, you need to decide how close is "good enough" and *
incorporate that into your comparison.

For example:

* * *double result = 1.2 % 0.01;

* * *if (result < 0.000001)
* * *{
* * * * *// might as well be zero!
* * *}



我认为这可行,但有时会给我剩余的

0.099999999未能通过你建议的修复。


I thought that might work, but sometimes it gives me a remainder of
0.099999999 which fails the fix you suggested.


Bernard.Mangay< fc **** @ googlemail.comwrote:
Bernard.Mangay <fc****@googlemail.comwrote:

由于舍入错误,余数不为零。如何删除

舍入错误?
The remainder is non zero due to rounding errors. How can I remove the
rounding errors?



如果你想要准确表示

这样的数字,请使用小数而不是双倍。


请参阅 http://pobox.com/~skeet/csharp/floatingpoint。 html 更多

信息。


-

Jon Skeet - < sk *** @ pobox .com>

网站: http://www.pobox .com / ~siget

博客: http ://www.msmvps.com/jon.skeet

C#深度: http://csharpindepth.com

Use decimal instead of double if you want accurate representations of
numbers like this.

See http://pobox.com/~skeet/csharp/floatingpoint.html for more
information.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com


这篇关于为什么1.2%0.01不等于零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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