Pergunta

I created a function to read floats (IEEE 754 binary 32-bit) from strings, for developers. I have everything finished and working perfectly, except i'm wondering if a user/or programmer enters a string such as "NaN" that does not specify QNaN or SNaN, what should it be considered? or should "NaN" be considered an invalid input and only accept "QNaN" or "SNaN"?

Foi útil?

Solução

First of all you should note that the signaling NaN (SNAN) is behaving the same as the quiet NAN (QNAN) when floating point exceptions are disabled. Usually they are disabled.

If "NaN" in your file is supposed to mean "missing data" then use QNAN.

If "NaN" is your file is supposed to mean "this must be initialized with proper data later" the use SNAN.

In the latter case an exception would be generated (if enabled, of course) in case the data is (accidentally) read and fed into a computation. However, when doing it this way (exceptions enabled) you must ensure that all your other FP computations do not generate NaN during regular computation and/or you need to implement proper exception handling. There of course you loose the advantage of FP math of being algebraically closed, i.e. be able to work in an exception free manner.

One problem with FP exceptions is also that in most programming languages the exceptions are non-continuable, whereas in the FP world this is not necessary (after exceptions the code can be continued). This is a bit too restrictive.

Personally I would suggest you to use QNAN.

Licenciado em: CC-BY-SA com atribuição
scroll top