When rounding very small decimal fractions, the round function appears to interpret the value incorrectly.
Numbered Steps to Reproduce Problem:
1. Get a small decimal fraction, for example
var/x = cos(270)
round that value
round(x)
be surprised as the result is -1 and not 0
Code Snippet (if applicable) to Reproduce Problem:
var/x = cos(270)
world.log << x
var/y = round(x)
world.log << y
Expected Results:
0 - confirmed this with python3's maths implementation
$ math.cos(math.radians(270))
-1.8369701987210297e-16
$ round(math.cos(math.radians(270)))
0
Actual Results:
-1
I originally thought that it's interpreting the value in scientific notation incorrectly
i.e round(-1.83697e-16) -> -1
whereas the number is actually
-0.0000000000000001836970198 blah blah blah which would round to 0
But perhaps I'm just missing something more obvious about the round function that explains it.
Does the problem occur:
Every time
When does the problem NOT occur?
N/A
Workarounds:
round(x, 1) seems to fix the issue for our use case