由于 Delphi XE2 中记录的舍入问题,我们使用 Embarcadero 网站上名为 DecimalRounding_JH1 的特殊舍入单元来实现真正的银行家舍入。可以在此处找到该单元的链接:
使用本单元的 DecimalRound 函数处理包含大量小数位的数字,我们
这是来自 DecimalRounding_JH1 单元的舍入例程。在我们的示例中,我们使用以下参数 (166426800, 12, MaxRelErrDbl, drHalfEven) 调用此 DecimalRound 函数,其中 maxRelErrDbl = 2.2204460493e-16 * 1.234375 * 2
Function DecimalRound(Value: extended; NDFD: integer; MaxRelErr: double;
Ctrl: tDecimalRoundingCtrl = drHalfEven): extended;
{ The DecimalRounding function is for doing the best possible job of rounding
floating binary point numbers to the specified (NDFD) number of decimal
fraction digits. MaxRelErr is the maximum relative error that will allowed
when determining when to apply the rounding rule. }
var i64, j64: Int64; k: integer; m, ScaledVal, ScaledErr: extended;
begin
If IsNaN(Value) or (Ctrl = drNone)
then begin Result := Value; EXIT end;
Assert(MaxRelErr > 0,
'MaxRelErr param in call to DecimalRound() must be greater than zero.');
{ Compute 10^NDFD and scale the Value and MaxError: }
m := 1; For k := 1 to abs(NDFD) do m := m*10;
If NDFD >= 0
then begin
ScaledVal := Value * m;
ScaledErr := abs(MaxRelErr*Value) * m;
end
else begin
ScaledVal := Value / m;
ScaledErr := abs(MaxRelErr*Value) / m;
end;
{ Do the diferent basic types separately: }
Case Ctrl of
drHalfEven: begin
**i64 := round((ScaledVal - ScaledErr));**
最后一行是我们得到一个浮点错误的地方。
关于为什么会发生此错误的任何想法?