Hammerspoon: [Suggestion] Add hs.math.isNan

Created on 11 May 2020  路  4Comments  路  Source: Hammerspoon/hammerspoon

Putting this out there for comments...

It doesn't come up often, and there is no rush on this, but since we now have a place where this actually fits, it would be nice to have a way to quickly check if a number is actually the nan value.

~lua
hs.math.isNan = function(x)
return x ~= x
end
~

Example:

~~~lua

hs.math.isNan = function(x)
return x ~= x
end

return math.huge, hs.math.isNan(math.huge)
inf false

-- yeah, I know they'll be different; this is just an example

return math.random(), hs.math.isNan(math.random())
0.29472790984437 false

return 0/0, hs.math.isNan(0/0)
nan true
~~~

I'm indifferent on the name... isnan, isNan, isNAN, notANumber...

All 4 comments

How about isNaN? I think so long as the first N is capitalised, I don鈥檛 have strong feelings about the rest, but I do think this is a good idea to add.

Ok, I like hs.math.isNaN. The docs should be explicit that it is testing for "equaling" the mathematical concept of a numerical value being "not-a-number" -- technically a string value is also "not a number", but this test will return false in this case.

What about also adding the other related C library queries?

e.g.
~
int isinf(double value); /* gives -1 for -inf, 1 for inf, 0 otherwise /
int finite(double value); /
gives 1 for not NaN and not inf */
~

I can see a use case for both, but I wonder if hs.math.isInfinite should just return true for both math.huge and -math.huge rather then a signed response... or maybe go ahead and return -1 and 1 (since they will both evaluate to true in a conditional) but false when the number isn't one of the infinities?

And as to naming, hs.math.isFinite and hs.math.isInfinite? Or the shorter version like the C library counterparts?

And while I'm avoiding going out to get groceries and run errands, another thought...

Should hs.math also mirror the contents of the lua math library? I can see myself doing something stupid in one of my scripts like this and wondering why math.sin no longer exists:

~lua
local math = require("hs.math")
... other lua code...
... until something like:
print(math.sin(90 * math.pi / 180))
~

should have closed this when #2371 landed

Was this page helpful?
0 / 5 - 0 ratings