Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Anyone who does not like NaNs should remember that using NaNs is just an option, nobody forces you to use them.

It is enough to enable exception generation for undefined operations and it is guaranteed that no NaNs can appear in any results.

It turns out that most people think that it is too much work to write suitable exception handlers, so they prefer to mask all exceptions and deal with NaNs and negative zeros.

In practice this just means that you must be careful when you write conditional expressions where floating-point numbers are compared, because the order relation becomes partial, so there are 14 possible relational operations instead of the only 6 that are possible for a total order, and you also must write the compared values in a way where the sign of a zero does not matter (in most expressions the sign of a zero does not influence the result; you must make some efforts to distinguish a negative zero from a positive zero).



From my perspective of implementing algorithms it matters because if I want to follow the spec correctly, I have to deal with them. Even if you don't want to use them, you pay about 10% for pretty much any function that needs special NaN handling (which is most of them). Also, pre AVX-512, these checks make vectorized algorithms way harder to write efficiently.


I agree that if you are writing a library to be used by others, you cannot know how they will choose to configure their floating-point environment, so you are forced to handle all the possible cases in your code.




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

Search: