# 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.

`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

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.