Why does typeof(0/0) return number even though it's NaN? How to check if a number is real

Printing typeof(0/0) or type(0/0) returns number even though printing 0/0 will return nan which stands for not a number. Is there a way to check if a number is actually a number or not? Seems like type and typeof are useless in this situation.

Just check if the number is equal to itself. It will return false if it’s NaN.

If nan == nan then print(“what”) end — should not print if it’s nan
5 Likes

Interesting behavior on Roblox’s end, thanks!

1 Like

It’s actually pretty common, e.g. isnan - cppreference.com

Might be overengineering a problem that nobody has but some of their functions should be imported. :wink:

image

math.isfinite or math.isnan would be pretty cool to see, even if they can be done with one line in LuaU.

1 Like

I think it’s to help make things easier for developers. You could write a function where there’s a number divided by a variable, and that variable could hit 0 at some point. Instead of breaking the code, it returns NaN (treated as a number). Doing this allows you to continue applying math to it. If you apply math to NaN, it will return inf. Although NaN * 0 will still equal NaN.

Isn’t division by 0 in most cases infinity? and some mathematicians consider infinity to be a number?

2 Likes

Isn’t division by 0 in most cases infinity? and some mathematicians consider infinity to be a number?

Eh…

It’s getting a bit off topic but oh well. I haven’t found a satisfying answer, it seems to always come down to “it depends on what you mean, and what you probably mean depends on the context”.

Here’s one really unsatisfying answer that at least I don’t feel like is wrong. It explains one context where it doesn’t make sense to talk about x/0 or infinity like that, but then mentions at the end that these other contexts exist but not what they are: Indeterminate: the hidden power of 0 divided by 0 - YouTube

Here’s another one: Problems with Zero - Numberphile - YouTube I don’t really like it because it speculates on how computers divide and why we get the results we do for 1/0 and 0/0, but doesn’t actually add any knowledge just guesswork :confused:

From a bit of Googling, AFAICT the exact behavior is just up to the designers of the CPU so there isn’t any single answer to “how computers do division” or “what computers do when they divide by 0”. Some throw exceptions (errors), others loop forever (an anecdote I read in YT comments), some deal with it by giving different numeric results or results like NaN or inf.

In the context of programming, the only thing that matters is what your programming language of choice does and that you carefully use this to implement the logic you want. In Lua you get

print(1/0) -- inf
print(-1/0) -- -inf
print(0/0) -- -nan
1 Like

The - in -nan is not guaranteed both by the output of scalar->string implementation (Luau) and the actual sign bit of the scalar floating point value.

Are you sure that it will return infinity?
What does “apply math” mean in this context? The additive and multiplicative operations on the floating point number range and definition?

1 Like

Nvm, maybe I did something wrong before. It only shows as NaN now.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.