Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Detect if input of function is an imaginary number?

Imaginary numbers, as in the square root of -1 and such. I'm just curious about how someone might try to approach this problem. Keep in mind that it could be i, it could be 2i, it could be 2 + 2i, etc.

1 answer

✓ Accepted answer

If the input is not a real number, then Lua will return a value that outputs as a string but returns true when the function type is applied on it.

However, we can take advantage of this by using tostring on the value then using tonumber on the result. If it is not a real number, then tostring will produce a string that is not equivalent to a number. tonumber applied on values that are not numbers will return nil.

The following function is the result of the facts above (and is a very elegant solution, if I do say so myself):

function isNotReal(x)
	return not tonumber(tostring(x))
end

This discussion is preserved as it was. New answers, comments, and votes are closed.