Hacker News new | past | comments | ask | show | jobs | submit login

I sometimes, in Python, cautiously use floating point numbers as dictionary keys. I do this when what I mean is "this exact floating-point number" and not "the result of an equivalent computation." It's almost never a good idea, because it's really easy to trip yourself up by assuming the latter when only the former actually holds.

And even the former apparently isn't guaranteed on all hardware. I found this gem in the Java documentation, I don't know which processors actually do this:

>"Arithmetic operations on signaling NaNs turn them into quiet NaNs with a different, but often similar, bit pattern. However, on some processors merely copying a signaling NaN also performs that conversion. In particular, copying a signaling NaN to return it to the calling method may perform this conversion."

Source: https://docs.oracle.com/javase/7/docs/api/java/lang/Double.h...

If you're putting floats in a dict, you can't use NaNs anyway, since they aren't equal to each other.

This is the stuff of nightmares.

You could hardly avoid this if you, say, memoized some function that has floating-point inputs.

Indeed, and for that reason I'd be very cautious when attempting to memoize a function with floating-point inputs.

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact