[fpc-pascal] round(2.5)=2

Santiago A. svaa at ciberpiula.net
Wed Jun 13 11:25:46 CEST 2018


El 12/06/2018 a las 23:12, Klaus Hartnegg escribió:
> Am 11.06.2018 um 10:01 schrieb Michael Schnell:
>>  - the way of rounding is defined by the CPU hardware, even old Turbo 
>> Pascal executables do banker's rouding on modern hardware.
>
> No, it does not depend on the hardware, but on the setting of $N. 
> Turbo Pascal with $N+ rounds like FreePascal. But the default is $N-. 
> In this mode Turbo Pascal always rounds up.
>
> var
>   a : real;
> begin
>   a := 35;
>   writeln (round(a*1.1));
> end.
>
> FreePascal: 38
> Turbo Pascal: 39
>
> And when the compiler precalculates a constant expression, it always 
> rounds up as well, regardless of $N.
>
> Nobody expects that increasing a number by 10% will depend on whether 
> the resulting number will be close to an even integer. That feels 
> completely erratic.

I don't feel it is completely erratic. If instead of 35 it were 1000035 
would you mind if the result were 1100038 or 1100039? When you round 
small numbers, relative rounding errors skyrocket, that is a fact of 
life. Any software must be aware of this and live with it.

>
> This behaviour does have advantages in the special case of adding up 
> many rounded numbers, but it ruins the graphics output of my software. 
> It should at least be configurable. And in {$mode tp} it should behave 
> like Turbo Pascal, because that's what this mode is made for.
>
>>   - SetRoundMode is not only dangerous, but does not help, anyway, as 
>> there is no mode defining the "intuitive " "non-banker rouding" method.
>
> That's why I'm looking for a better solution. The the only *reliable* 
> solution appears to be compiling a modified version of FreePascal.
I think that is even more dangerous. Who knows how many internal 
freepascal will fail when you change rounding. It would be better to 
review  software to check  why it needs always round floor for 0.5

>
> Klaus
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


-- 
Saludos

Santiago A.




More information about the fpc-pascal mailing list