为什么RoundTo的结果(87.285,-2)=> 87.28 [英] Why is the result of RoundTo(87.285, -2) => 87.28

查看:132
本文介绍了为什么RoundTo的结果(87.285,-2)=> 87.28的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我预计结果是87.29。我也尝试过SimpleRoundTo,但是会产生相同的结果。



在帮助中还有一个奇怪的例子:
ms-help:// embarcadero。 rs2010 / vcl / Math.RoundTo.html

  RoundTo(1.235,-2)=> 1.24 
RoundTo(1.245,-2)=> 1.24 // ???有没有人知道哪个函数需要得到87.29的结果?

我的意思是:如果最后一个数字> = 5向上舍入,如果< 5回合。正如学校所教导的:)



我使用Delphi2010和 SetRoundMode(rmNearest)。我也尝试用 rmTruncate
值为87.285的值存储在 double 变量中。



另外奇怪的是:

  SimpleRoundTo(87.285,-2)=> 87.29 

 code> x:= 87.285; // double 
SimpleRoundTo(x,-2)=> 87.28


解决方案

87.285不完全可表示,最近的双稍微小一点。



浮点数的经典参考是什么每个计算机科学家应该了解浮动 -
点算术



对于基于货币的计算,如果确实是这样,你应该使用基数10数字类型而不是基数2浮点数。在Delphi中,这意味着货币


I expected that the result would be 87.29. I also tried SimpleRoundTo, but produces the same result.

In the help there is also a "strange" example: ms-help://embarcadero.rs2010/vcl/Math.RoundTo.html

RoundTo(1.235, -2) => 1.24
RoundTo(1.245, -2) => 1.24 //???

Does anybody know which function I need to get the result of 87.29? I mean: If the last digit >= 5 round up, if < 5 round down. As taught in the school :)

I use Delphi2010, and SetRoundMode(rmNearest). I also tried with rmTruncate. The value 87.285 is stored in a double variable.

Also strange:

SimpleRoundTo(87.285, -2) => 87.29

but

x := 87.285; //double
SimpleRoundTo(x, -2) => 87.28

解决方案

87.285 is not exactly representable and the nearest double is slightly smaller.

The classic reference on floating point is What Every Computer Scientist Should Know About Floating- Point Arithmetic.

For currency based calculations, if indeed this is, you should use a base 10 number type rather than base 2 floating point. In Delphi that means Currency.

这篇关于为什么RoundTo的结果(87.285,-2)=&gt; 87.28的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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