goog.isNumber(NaN) returns true

667 views
Skip to first unread message

Andre Tannus

unread,
May 21, 2012, 10:32:08 AM5/21/12
to closure-lib...@googlegroups.com
This is disturbing.

I understand the inclination to mimic typeof as much as possible but it doesn't make it less disturbing.

How do you guys make sure something "Is a number" and is "not Not a Number"?

goog.isNumberAndNotNotANumber()?? haha...

André


--
A ciência consiste em perturbar um sistema e analisar sua reação. Eu, sou uma perturbação.

André Tannús | Ideas at Epungo
ata...@epungo.com.br | +55 11 8053-7636 | +55 11 2389-4360 

"Somewhere, something incredible is waiting to be known."
Carl Sagan

Thomas Broyer

unread,
May 21, 2012, 10:43:46 AM5/21/12
to closure-lib...@googlegroups.com


On Monday, May 21, 2012 4:32:08 PM UTC+2, Andre Tannus wrote:
This is disturbing.

I understand the inclination to mimic typeof as much as possible but it doesn't make it less disturbing.

How do you guys make sure something "Is a number" and is "not Not a Number"?

goog.isNumberAndNotNotANumber()?? haha...

goog.isNumber(foo) && !isNaN(foo) ? 

Andre Tannus

unread,
May 21, 2012, 11:01:45 AM5/21/12
to closure-lib...@googlegroups.com
goog.isNumber(foo) && !isNaN(foo) ? 

That's what I'm currently doing...
Awful lot to write, especially when you're retrieving, from cookies (i.e. {string}), a bunch of stuff that you'd expect to be numbers.
E.g. google.maps.Map.setZoom(), for instance, can't handle strings such as '7'.

It feels to me like goog.isNumber() should behave as its semantics indicate and goog.isJsNumber or something else should be the exception.
But, given what has been said on this (behavior of typeof), goog.isValidNumber() would be enough...

Nathan Naze

unread,
May 21, 2012, 2:32:45 PM5/21/12
to closure-lib...@googlegroups.com
The name might be misleading, but it essentially is asking if it's a
numeric value (which NaN is -- its type is 'number').

isNaN and isFinite exist in the language to differentiate NaN and
infinite values from the rest of number.

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/isNaN

Nathan
Reply all
Reply to author
Forward
0 new messages