#2934 Floats, negative zero, and Hash codes

SlimerDude Mon 13 Jan

Hi, I was unit testing some code using graphics::Point when I can across this interesting test failure:

sys::TestErr: Test failed: 
  Equal but different hash codes: 0 0 (0x8000000000000000) ?= 0 0 (0x0)

The 2 x Point instances being compared have the same values (as indicated) but the hash codes were different.

Digging deeper, I was introduced to the concept of negative zero in floating point numbers; an artefact from the IEEE 754 standard.

And because -0.0 is different to 0.0 they generate different hash codes, which is then being fed back into the graphics geometry classes, such as Point.

Fantom Shell v1.0.80 ('?' for help)
fansh> f0 := 0f
0.0
fansh> 0f.hash
0
fansh> -1f * f0
-0.0
fansh> (-1f * f0).hash
-9223372036854775808

As mathematically pure -0.0 may be, I'm not sure how useful it is when it comes to general purpose object equality?

I was just wondering what thoughts, perspectives, or experience others may have on this; should geometric objects such as Size or Point distinguish between 0.0 and -0.0?

Login or Signup to reply.