Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between #QNAN and #IND in Lua

Tags:

nan

lua

If you run this through the Lua interpreter

print(0/0)
print(-(0/0))

It outputs

-1.#IND
1.#QNAN

To my understanding they are both Quiet Not-A-Numbers, but why is #QNAN equals -#IND (When I say equal I mean they print the same thing since I know that NaN != NaN)

Is there some benefit of knowing if a NaN is #IND or #QNAN and if so what is the benefit.

like image 855
Xonar Avatar asked Dec 02 '13 09:12

Xonar


1 Answers

x86 specifies a special type of QNaN, the so called QNaN Floating-Point Indefinite (Section 4.8.3.7 in the IA32 Manual):

For the floating-point data type encodings (single-precision, double-precision, and double-extended-precision), one unique encoding (a QNaN) is reserved for representing the special value QNaN floating-point indefinite. The x87 FPU and the SSE/SSE2/SSE3/SSE4.1/AVX extensions return these indefinite values as responses to some masked floating-point exceptions. Table 4-3 shows the encoding used for the QNaN floating-point indefinite.

This is what gets printed as -1.#IND by your C library implementation.

In your case, the 0/0 operation triggers a masked Invalid Operation Exception on the FPU, which is signaled by an Indefinite return value. In the -(0/0) case you feed this Indefinite value as the input to a negate operation, which returns a QNaN.

The reason why different floating point operations give different error values is to allow for easier debugging. An experienced developer might be able to track down an error more easily if she knows exactly what kind of NaN value is returned. For non-debugging purposes, since Indefinite is just a special kind of QNaN, you can often ignore the difference altogether.

like image 164
ComicSansMS Avatar answered Nov 01 '22 10:11

ComicSansMS