0
由於德爾福XE2中記錄的舍入問題,我們正在使用名爲DecimalRounding_JH1的Embarcadero站點上的特殊舍入單元來實現真正的銀行家舍入。到單位的鏈接可以在這裏找到:德爾福XE2與DecimalRounding_JH1舍入
使用本機的功能DecimalRound含有大量小數位的數字,我們
這是從DecimalRounding_JH1單位四捨五入程序。在我們的例子中,我們調用這個函數DecimalRound使用以下參數(166426800,12,MaxRelErrDbl,drHalfEven),其中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));**
最後一行是我們得到一個浮點錯誤。
有關爲何發生此錯誤的任何想法?