Precision rounding
From: Olivier Sauterel (sauterel_at_ioware.ch)
Date: 02/24/04
- Next message: Stacey R. Brodsky: "What am I missing?"
- Previous message: Figaro: "Re: Unknown Error has Occured"
- Next in thread: John Herbster \(TeamB\): "Re: Precision rounding"
- Reply: John Herbster \(TeamB\): "Re: Precision rounding"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 24 Feb 2004 15:25:05 +0100
Hi,
I' ve some problem with my round routine, because in test "tmp >= precision
/ 2" does not work in sample:
RoundMultiple(0.725, 0.01)
It will return 0.72 because tmp after Modulo represent 0.00499999999996 and
should
be 0.005 ! And i think the problem is the same with expression "precision /
2".
I've already try to set the PrecisionMode with pmDouble, and it will work in
some case, but not everytime ...
Anyone can help me and maybe explain me why i've this problem ?
Thanks
Olivier
---------------------------------------------------------------
function RoundMultiple(value, precision: Double): Double;
var
tmp: Double;
begin
tmp := Modulo(value, precision);
if tmp <> 0 then
begin
result := value - tmp;
if tmp >= precision / 2 then
result := result + precision;
end
else
result := value;
end;
---------------------------------------------------------------
function Modulo(value, divisor: Double): Double;
begin
result := value - Trunc(value / divisor) * divisor;
end;
---------------------------------------------------------------
- Next message: Stacey R. Brodsky: "What am I missing?"
- Previous message: Figaro: "Re: Unknown Error has Occured"
- Next in thread: John Herbster \(TeamB\): "Re: Precision rounding"
- Reply: John Herbster \(TeamB\): "Re: Precision rounding"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|