TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Javascript's NaN

11 点作者 beghbali超过 12 年前

7 条评论

jensnockert超过 12 年前
Yet another post that underestimates the importance on NaN, it isn't 'WTF' at all if you understand its design. Sure, the naming in ES makes it a bit silly since NaN is a number, but it is something you only have to see once to understand.<p>Don't mess with IEEE 754, it is almost universally adopted for binary floating-point for very good reasons.
评论 #4566048 未加载
dap超过 12 年前
Yes, it's goofy that typeof (NaN) == 'number'. But it's not that unreasonable to have a numeric value that's explicitly invalid. Pointers and references in other languages have null, which is a legal value for any type of object, but is explicitly invalid if used as an object.<p>Also, isNaN is global.
评论 #4566071 未加载
评论 #4567054 未加载
beering超过 12 年前
One wonders if this blog post would have been written at all if the author was familiar with NaN in floating-point numbers and knew that isNaN was a thing.<p>isNaN is the defined method of checking if something Not A Number or can't be converted into a number.<p>The Real WTF (and infamous "feature" of JS) is that many things can be converted into a number.<p><pre><code> isNaN("string") =&#62; true isNaN({}) =&#62; true </code></pre> but you might not expect that<p><pre><code> isNaN("3e2") =&#62; false isNaN("") =&#62; false isNaN([]) =&#62; false</code></pre>
评论 #4566472 未加载
dreish超过 12 年前
This is hardly a JavaScript quirk. In Clojure:<p><pre><code> hackclj.core=&#62; Double/NaN NaN hackclj.core=&#62; (== Double/NaN Double/NaN) false hackclj.core=&#62; (= Double/NaN Double/NaN) false hackclj.core=&#62; (.equals Double/NaN Double/NaN) true hackclj.core=&#62; (identical? Double/NaN Double/NaN) false hackclj.core=&#62; (type Double/NaN) java.lang.Double hackclj.core=&#62; (number? Double/NaN) true </code></pre> In Perl (which doesn't have a concept of a number type distinct from other scalars such as strings, but we can see that it participates in addition differently from non-numeric scalars, which behave like 0):<p><pre><code> DB&#60;1&#62; $inf = 1e300 * 1e300 DB&#60;2&#62; $nan = $inf - $inf DB&#60;3&#62; print $nan nan DB&#60;4&#62; print ($nan == $nan) DB&#60;5&#62; print ref($nan) DB&#60;6&#62; print $nan+1 nan </code></pre> In Ruby:<p><pre><code> irb(main):001:0&#62; inf = 1e300*1e300 =&#62; Infinity irb(main):002:0&#62; nan = inf-inf =&#62; NaN irb(main):003:0&#62; nan == nan =&#62; false irb(main):004:0&#62; nan.class =&#62; Float </code></pre> Python 2.7.3 (much earlier versions got equality wrong, claiming nan == nan):<p><pre><code> &#62;&#62;&#62; inf = 1e300*1e300 &#62;&#62;&#62; nan = inf-inf &#62;&#62;&#62; nan nan &#62;&#62;&#62; nan == nan False &#62;&#62;&#62; type(nan) &#60;type 'float'&#62;</code></pre>
tikhonj超过 12 年前
Of all the things to complain about, he chose one that is <i>not</i> JavaScript's fault: the fact that NaN is a number and equality on NaN is not reflexive is a property of the floating point number standard.<p>Try it in some other language you have lying around, like Python:<p><pre><code> &#62;&#62;&#62; type(float('nan')) &#60;type 'float'&#62; &#62;&#62;&#62; float('nan') == float('nan') False </code></pre> Also Haskell:<p><pre><code> Prelude&#62; 0/0 NaN Prelude&#62; 0/0 == 0/0 False </code></pre> If anything, it's refreshing that JavaScript <i>does</i> behave like other languages in this case!
simonster超过 12 年前
The language-agnostic way to check for NaN is to check whether x != x. Some languages might also need a type check, but AFAIK JS isn't one of them.
JeremyBanks超过 12 年前
I wonder how many blog posts could have been avoided if Number had just been named Float instead.
评论 #4566169 未加载
评论 #4565866 未加载