[fpc-pascal] round(2.5)=2
Klaus Hartnegg
hartnegg at gmx.de
Fri Jun 8 14:47:53 CEST 2018
Hi,
The round function behaves different than in TurboPascal, and its
English Wiki page differs from the German page.
When given an argument halfway between to integers, FreePascal rounds to
the nearest even number, TurboPascal always rounds up (except when it is
set to mode $N+, but default is $N-).
The German Wiki pages for Round and SetSoundMode (URLs below) contain a
warning to not use SetRoundMode, because that also affects internal
calculations, and how numbers are stored in variables when they do not
fit with full precision.
How serious is this issue compared with using a different rounding method?
The german pages suggest to use this function instead:
function round(x: Float): Integer; { requires unit math for 'float' }
begin
if x > 0 then
round := trunc(x + 0.5)
else
round := -trunc(-x + 0.5);
end;
That is fine, except that I will likely forget it in some unit, and
manually inspecting all units is less reliable than when the compiler
ensures it.
In Turbo-Pascal the browsing feature (search,symbol) of the IDE can show
a list of all places where system.round gets called. The FreePascal IDE
does not show that list (see my other email).
Is there some other reliable(!) way to fix or check this? Maybe a map
file with all functions that got linked into the executable?
The Wiki pages are
- wiki.freepascal.org/Round
- wiki.freepascal.org/Round/de
- wiki.freepascal.org/SetRoundMode
- wiki.freepascal.org/SetRoundMode/de
If the warning is correct, there should also be a note on
https://www.freepascal.org/docs-html/rtl/math/setroundmode.html
thanks,
Klaus
More information about the fpc-pascal
mailing list