#2887 Strange Behavior with -0f and multiplication/division

Jeremy C Sat 25 Feb 2023

Negative Zero seems to have a few issues with Float; specifically with multiplication and division. But it very rarely comes up! I boiled it down though.

See this in fansh to see what I mean:

fansh> zero := 0f
fansh> 0f * -1f
0.0  // Wrong
fansh> zero * -1f
-0.0 // Right
fansh> 0f / -1f
0.0  // Wrong
fansh> zero / -1f
-0.0 // Right

For some reason, the literal (when not placed into a variable) seems to not produce -0f as it should. Yet placing it into a variable or even doing [-0f][0] makes it work as it should.

I can't seem to figure out why this is happening. Likely it's not an issue unless you're using the literal 0f in your code.

EDIT: Seems actually this has the same issue: "0".toFloat * -1.

Is it possibly due to the java double primitive being used versus the java Double object? I can't find anything on the internet that seems to suggest this being a bug with Java.

This behavior also matches up when you use unary negation (like -0f vs -zero).

Login or Signup to reply.