bcc
bcc copied to clipboard
Add native "round()" command
We have floor()
and ceil()
to round integers downwards (to negative infinity) or upwards (to positive infinity).
ceil(0.6) = 1
floor(0.6 = 0
ceil(-1.1) = -1
floor(-1.1) = -2
Often we use this here:
value = int(floatValue + 0.5)
to avoid stuff like casting 3.0:float
to integer (which can mean int(2.9999997)
or int(3.000001)
).
To avoid this issue we should compare the sgn()
of the value ... or written differently:
if floatValue < 0
value = int(floatValue - 0.5)
else
value = int(floatValue + 0.5)
endif
or maybe it could be written this way:
value = int(floatValue + 0.5 - (floatValue < 0))
I think it would be good to have a native round()
command which does a similar thing for the developer (in C we can of course write it ... shorter).
Having a round()
command at hand might avoid issues as most people will most likely have used the old intValue = int(floatValue + 0.5)