# [fpc-devel] Round function issue

Juan Carlos Díaz Vigo juancarlos.diazvigo at gmail.com
Thu May 23 21:28:57 CEST 2019

```Dear The Free Pascal Developers

Greetings from Perú!
First and foremost, I would like to thank you for the great work you have
done and are currently doing in providing the world with a great compiler
for the Pascal programming language: Free Pascal.

I am writing to you concerning an issue that I consider as being of the
greatest importance since it affects two compiler modes of the Free Pascal
Compiler. The round function in FPC (version 3.0.4) when the parameter is
equidistant from the two nearest integers (.5) rounds the number towards
the even number. This feature of the FPC's round transfer function when it
comes to rounding (.5) integers hinders both compliance with the ISO 7185
standard and compatibility with Turbo Pascal.

The Pascal ISO 7185:1990 standard document states that "If x is positive or
zero, round(x) shall be equivalent to trunc(x+0.5); otherwise, round(x)
shall be equivalent to trunc(x-0.5)." (Pascal ISO7185:1990, (C)ISO/IEC
1991, online copy. Page 44). As for Turbo Pascal, in its version 6.0
library reference document one can read: "If X is exactly halfway between
two whole numbers, the result is the number with the greatest absolute
magnitude." (Turbo Pascal(R) Version 6.0 Library Reference. Copyright(C)
1983, 1990 by Borland International. Page 120).

I became aware of this issue when getting strange results for a program I
wrote that involved the calculation of rounded numbers in a statement like
this: round((upper - lower) / 2), where, for instance, a value that I
expected to be 13 was calculated as 12. Since the next statements would
take the values returned by the round function to do more calculations, the
program would produce wrong results. Shortly afterwards I wanted to know
what the results would be if I compiled the program with other Pascal
compilers. I did so, GNU Pascal being one of them, and they all did yield
the expected program results and the expected values calculated by the
round function, that is 13 instead of 12 for the example above.

I would appreciate it if you did not suggest that I use GNU Pascal if I
want an ISO 7185 standard compliant Pascal compiler. I do have both Free
Pascal and GNU Pascal compilers installed on my machine and I have noticed
there is a great difference in terms of execution speed when a program does
a lot of calculations in numerical applications. In this respect Free
Pascal is superior (thanks a lot, guys!). Furthermore, GNU Pascal
development is halted while Free Pascal is rising to become the de facto
ISO 7185 compliant compiler because of its ubiquitous nature, active
development, and speed.

Hence, it will be advantageous to the whole community of Pascal programmers
if FPC completely complies with the ISO 7185 standard, and in order to do
so, a first great step will be providing an ISO 7185 compliant round
function. Besides, in doing so, FPC will also be more compatible with Turbo
Pascal.

Please, do not get me wrong. I do know I don't have the authority to demand
anything at all. I don't want to do that. I am just a humble programming
student who knows he has a lot to learn. I also know that you guys are for
sure much more knowledgeable about computers and algorithms than I am, and
I thank you again and congratulate you for the great job you have done in
producing the Free Pascal Compiler. But, can I suggest something be made in
order for the FPC round function to recognize (.5) numbers and treat them
in accordance with the ISO 7185 standard, at least when compiling with the
ISO mode?

I thank you in advance and look forward to hearing from you shortly.

Best regards

Juan C. Díaz