Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2006-10

Round function

2 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Andrea Mauri

Using round function I implemented the RoundTo function as in Delphi:

type
  TRoundToRange = -37..37;
function RoundTo(const AValue: Double; const ADigit: TRoundToRange): Double;
var
  LFactor: Double;
begin
  LFactor := IntPower(10, ADigit);
  Result := Round(AValue / LFactor) * LFactor;
end;

In Delphi RoundTo has a known bug
(http://qc.borland.com/wc/qcmain.aspx?d=8070).
And in freepascal (Windows) I noticed the same behavior. In order to respect the banker's rounding we must obtain: RoundTo(1.235, -2) ->1.24
RoundTo(1.245, -2) -> 1.24

But in reality:
RoundTo(1.235, -2) ->1.24
RoundTo(1.245, -2) -> 1.25

I didn't test it on Linux yet.
It is a known bug?
a.

Reply by email

Graeme Geldenhuys
to Andrea Mauri

A week or two ago, I also found a problem with the Banker's Rounding in Free Pascal. As far as I could see, with my tests I did, compared to Delphi an know results with set values, there is a bug in FPC implementation.

I might have forgotten to report it in Mantis though. I will double check.

Regards,
- Graeme -

Andrea Mauri wrote 31 lines
Using round function I implemented the RoundTo function as in Delphi: type TRoundToRange = -37..37; function RoundTo(const AValue: Double; const ADigit: TRoundToRange): Double; var LFactor: Double; begin LFactor := IntPower(10, ADigit); Result := Round(AValue / LFactor) * LFactor; end; In Delphi RoundTo has a known bug (http://qc.borland.com/wc/qcmain.aspx?d=8070). And in freepascal (Windows) I noticed the same behavior. In order to respect the banker's rounding we must obtain: RoundTo(1.235, -2) ->1.24 RoundTo(1.245, -2) -> 1.24 But in reality: RoundTo(1.235, -2) ->1.24 RoundTo(1.245, -2) -> 1.25 I didn't test it on Linux yet. It is a known bug? a. _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/mailman/listinfo/fpc-devel
There's no place like 127.0.0.1

Reply by email