SimpleRoundTo - fail to round negative numbers
Original Reporter info from Mantis:
-
Reporter name:
Original Reporter info from Mantis:
- Reporter name:
Description:
SimpleRoundTo function returns wrong results for negative numbers.
Rounding negative number is toward 0, not away from it and for this function is used asymmetric arithmetic rounding:
SimpleRoundTo(-1.235,-2) gave -1.24, it should be -1.23
SimpleRoundTo(-1234567,3) returns -1235000, should be -1234000
It should be:
function SimpleRoundTo(const AValue: Extended; const Digits: TRoundToRange = -2): Double;
var
RV : Extended;
begin
RV := IntPower(10, Digits);
Result := Trunc((AValue/RV) + 0.5)*RV;
end;
And accordingly for all overloaded functions...
Additional information:
THe same bug exists in latestu 3.1.1 SVN.
This is current function on math.pp wrongly handling negative numbers:
function SimpleRoundTo(const AValue: Extended; const Digits: TRoundToRange = -2): Extended;
var
RV : Extended;
begin
RV := IntPower(10, -Digits);
if AValue < 0 then
Result := Trunc((AValue*RV) - 0.5)/RV
else
Result := Trunc((AValue*RV) + 0.5)/RV;
end;
Mantis conversion info:
- Mantis ID: 34946
- OS: Ubuntu
- OS Build: 18.10
- Platform: Linux 64-bit
- Version: 3.0.4