I have written a program to print a matrix after some computations and I am getting an output of nan for all elements. I want to break a for loop as soon as the matrix's first element becomes nan to understand the problem. How can I do this? In the terminal, I have printed the matrix a containing nan as all elements and typed a[1][1]=="nan" and a[{{1},{1}}]=="nan" both of which return false. Why are they not returning false and what statement should I use instead?
isNaN() method to check if a number is NaN, e.g. if (Number. isNaN(num)) {} . The Number. isNaN() method will return true if the provided value is NaN and has a type of number .
But if you need to know whether Lua considers it an actual number or not, rather than a string, use 'number' == type(foo) in your test conditions.
wiki. In numerical calculations sometimes we want to compare numbers with infinites and NaN (not a number).
To check wether a value is a string use type(value) == "string" . To check wether a value can be convertet do a number use tonumber(value) . It will either return a number or nil.
Your test fails because you are comparing a number with a string, "nan".
If you are sure it's a number, the easiest way is:
if a[1][1] ~= a[1][1] then
because according to IEEE 754, a nan value is considered not equal to any value, including itself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With