Open GWRon opened 2 years ago
Function Round:Double(x:Double) Inline
Return Floor(x + 0.5)
End Function
https://stackoverflow.com/questions/47302544/why-do-lots-of-old-programs-use-floor0-5-input-instead-of-roundinput and also this here https://stackoverflow.com/questions/485525/round-for-float-in-c/24348037#24348037
The classic "+ 0.5" might have an issue with values like 0.49999999999999994 (Round(variable which was calculated and is 0.5)
) which could become 1 or 0.
Hmm... seems I opened a can of worms.
We have
floor()
andceil()
to round integers downwards (to negative infinity) or upwards (to positive infinity).Often we use this here:
value = int(floatValue + 0.5)
to avoid stuff like casting3.0:float
to integer (which can meanint(2.9999997)
orint(3.000001)
).To avoid this issue we should compare the
sgn()
of the value ... or written differently:or maybe it could be written this way:
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 oldintValue = int(floatValue + 0.5)