Filter "inf" and "-nan(ind)"

Sometimes my math has to divide by zero. But you get stuff like -nan(ind) and inf. And the only workaround is checking if it spits out an error and making the value 0.

But how can i detect theese values?

I’ve tried math.huge() for inf, but that still leaves -nan(ind).
Trying to compare theese to 1/0 and 0/0 does nothing.
"-nan(ind)" as a string does nothing.

2 Likes

You can check for nan like this:

local isNan = value ~= value

Because nan has the weird property of being the only thing that’s not equal to itself.

8 Likes

image

Anyway thanks. :smile:

2 Likes

Define “weird” and define “thing”. Just because it is not reflexive that does not mean it is weird, nor does it imply that it’s the only value that is not reflexive.

Anyway NaN is not not equal to itself. NaN is unorderered.
The Clause 5.11 of IEEE 754-2008 says

Every NaN shall compare unordered with everything, including itself.

But the comparison operation in computer arithmetic are strictly boolean-only so it has to return either one or the other so if these operations has a NaN operand, it returns False as it’s less (but still misleading) misleading than returning it `True.

1 Like