Delphi XE2舍入和小数舍入
由于Delphi XE2中存在记录在案的舍入问题,我们使用Embarcadero网站上名为DecimalRounding_JH1的特殊舍入单元来实现真正的银行家舍入。可在此处找到该装置的链接: 使用此单元的小数舍入函数,对包含大量小数位的数字执行以下操作: 这是小数舍入单元JH1的舍入例程。在我们的示例中,我们使用以下参数(166426800,12,maxrererrdbl,drHalfEven)调用此小数舍入函数,其中maxrererrdbl=2.2204460493e-16*1.234375*2Delphi XE2舍入和小数舍入,delphi,delphi-xe2,rounding,Delphi,Delphi Xe2,Rounding,由于Delphi XE2中存在记录在案的舍入问题,我们使用Embarcadero网站上名为DecimalRounding_JH1的特殊舍入单元来实现真正的银行家舍入。可在此处找到该装置的链接: 使用此单元的小数舍入函数,对包含大量小数位的数字执行以下操作: 这是小数舍入单元JH1的舍入例程。在我们的示例中,我们使用以下参数(166426800,12,maxrererrdbl,drHalfEven)调用此小数舍入函数,其中maxrererrdbl=2.2204460493e-16*1.23437
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));**
最后一行是我们得到浮点错误的地方
您对发生此错误的原因有何想法?如果出现异常,这意味着您不能在指定的错误范围内将值表示为双精度 换句话说,
MaxRelerDBL
太小
尝试使用maxrrelrdbl=0000000000 1
或一些东西来测试我是否正确