Skip to main content
November 6, 2008
Question

isNumeric() returns true for "NaN"

  • November 6, 2008
  • 2 replies
  • 1135 views
According to the docs isNumeric(arg) returns "True, if arg can be converted to a number; otherwise False."

However (in CFMX7)
<cfset temp = "NaN">
<cfoutput>#isNumeric(temp)#</cfoutput> <!---outputs "YES"--->
<cfset temp = temp + 1> <!---generates error The value "NaN" cannot be converted to a number--->

So obviously something is wrong.
This topic has been closed for replies.

2 replies

BKBK
Community Expert
Community Expert
November 7, 2008
Coldfusion MX7 is actually right! NaN is numeric in the same sense that infinity is numeric. MX7 is also right in saying that NaN, just like infinity, cannot be converted to a number. In fact, it cannot even be converted to itself! One definition of NaN is:

x is NaN if and only if (x = x) is false.

However, this makes arithmetic assumptions that fail in the real world of computers. CF8 gets it right. To avoid inconsistency in coding, IsNumeric("NaN") should return False.

November 7, 2008
I agree. The sole usefulness of the function for me is to determine whether a value can be converted to a number that CF recognizes as a number, not something that in mathematical theory is a number. I had to program a workaround when I got an unexpected error, not a big deal but still annoying.

Ken
Inspiring
November 7, 2008
Yes, the "NAN" case is mentioned in the comments here.
http://www.coldfusioncookbook.com/entry/15/You-need-to-test-a-string-to-see-if-it-is-a-valid-numeric-value.

For what it is worth, it returns "NO" with ColdFusion 8.