go-gl / mathgl

A pure Go 3D math library.
BSD 3-Clause "New" or "Revised" License
554 stars 65 forks source link

`mgl64.Round(0, 2)` returns `-0` #86

Closed HashimTheArab closed 2 years ago

HashimTheArab commented 2 years ago

image image im not sure how this is even possible lmao

pwaller commented 2 years ago

Nice find. Signed zero is a thing that floating point numbers have :)

https://en.wikipedia.org/wiki/Signed_zero

It looks like this is arising from the current definition of Round, which at a glance is doing 'tie breaks round towards nearest', with 'rounding towards zero'.

https://github.com/go-gl/mathgl/blob/592312d8590acb0686c14740dcf60e2f32d9c618/mgl64/util.go#L137-L146

The if t > 0 is false, so it's rounding as if 'zero is negative'. The sign is appearing from math.Ceil(-0.5): https://go.dev/play/p/RtDbZu9JNlF

I think the fix is to change the condition to if t >= 0 because negative zero was neglected. Patches welcome? :)