javascript
Some Essential JavaScript Questions And Answers(4)
?Some Essential JavaScript Questions And Answers
Question7:
What is NaN? What is its type? How can you reliably test if a value is equal to NaN?
[譯]:NaN是什么?它是什么類型的?如何可靠地測試一個值是否等于NaN?
Answer:
The NaN property represents a value that is “not a number”. This special value results from an operation that could not be performed either because one of the operands was non-numeric (e.g., "abc" / 4), or because the result of the operation is non-numeric.
[譯]:NaN 屬性代表一個“不是數字”的值。這個特殊的值是因為運算不能執行,原因要么是因為其中的運算對象之一非數字(例如, "abc" / 4),要么是因為運算的結果非數字(例如,除數NaN)。
While this seems straightforward enough, there are a couple of somewhat surprising characteristics of NaN that can result in hair-pulling bugs if one is not aware of them.
[譯]:雖然這看上去很簡單,但 NaN 有一些令人驚訝的特點,如果你不意識到它們的話,會導致一些讓煩到想拔自己的發的bug。
For one thing, although NaN means “not a number”, its type is, believe it or not, Number:
[譯]:首先,雖然 NaN 意味著“不是數字”,但是它的類型,不管你信不信,是 Number:
console.log(typeof NaN === "number"); // logs "true"Additionally, NaN compared to anything – even itself! – is false:
[譯]:此外, NaN 和任何東西比較——甚至是它自己!——結果都是false:
console.log(NaN === NaN); // logs "false"A semi-reliable way to test whether a number is equal to NaN is with the built-in function isNaN(), but even using isNaN() is an imperfect solution.
[譯]:一種半可靠的方法來測試一個數字是否等于 NaN,是使用內置的isNaN()函數,但是即使使用 isNaN() 依然不是一個完美的解決方案。
A better solution would either be to use value !== value, which would only produce true if the value is equal to NaN. Also, ES6 offers a new Number.isNaN() function, which is a different and more reliable than the old global isNaN() function.
[譯]:一個更好的解決辦法是使用 value !== value,值等于NaN的話,只會產生true。另外,ES6提供了一個新的 Number.isNaN() 函數,這是一個不同的函數,并且比老的全局 isNaN() 函數更可靠。
Question8:
What will the code below output? Explain your answer.[譯]:以下代碼的輸出結果是?解釋你的答案。
console.log(0.1 + 0.2); console.log(0.1 + 0.2 == 0.3);Answer:
An educated answer to this question would simply be: “You can’t be sure. it might print out 0.3 and true, or it might not. Numbers in JavaScript are all treated with floating point precision, and as such, may not always yield the expected results.”
[譯]:一個有點編程基礎回答可以簡單地歸納為:“你不能確定。可能會輸出“0.3”和“true”,也可能不會。數字在JavaScript中都會被當做浮點精度來處理,因此,可能不會總是產生預期的結果。”
The example provided above is classic case that demonstrates this issue. Surprisingly, it will print out:
[譯]:以上所提供的例子就是一個演示了這個問題的典型例子。令人驚訝的是,它會輸出:
0.30000000000000004 falseA typical solution is to compare the absolute difference between two numbers with the special constant Number.EPSILON:
[譯]:比較兩個數字之間絕對差異的一個典型的解決方案是使用特殊的常量Number.EPSILON:
function areTheNumbersAlmostEqual(num1, num2) {return Math.abs( num1 - num2 ) < Number.EPSILON; } console.log(areTheNumbersAlmostEqual(0.1 + 0.2, 0.3));以下為對翻譯中Number.EPSILON的解釋,不屬于翻譯內容。
ES6的Number對象新增了一個常量Number.EPSILON。這個值很小,我們可以在控制臺下打印出來看
引入這么小的一個常量的目的在于,為浮點數計算設置一個誤差范圍。因為浮點數的計算是不精確的。如果誤差小于Number.EPSILON,我們就可以認為得到了正確的結果。
例子:
0.1+0.2; // 0.30000000000000004 0.1+0.2-0.3; // 5.551115123125783e-17 (0.1+0.2-0.3).toFixed(20); // "0.00000000000000005551" // 0.1+0.2-0.3的值是否小于Number.EPSILON,如果小于,返回true,那我們便可以認為結果正確 (0.1+0.2-0.3).toFixed(20) < Number.EPSILON; // true也就是說,Number.EPSILON是一個可接受的誤差范圍。
完結~~晚安~~
總結
以上是生活随笔為你收集整理的Some Essential JavaScript Questions And Answers(4)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 大班教案《快乐的小公主》反思
- 下一篇: 东北话上头是什么意思