From 2eece4014ae2331210b3fe5476c21bfb1537d011 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Tue, 19 Jul 2022 12:51:46 +0800 Subject: [PATCH] Improvements to a few Number APIs --- .../global_objects/number/isnan/index.md | 5 +-- .../number/issafeinteger/index.md | 36 ++++++++++--------- .../global_objects/number/min_value/index.md | 4 ++- 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/files/en-us/web/javascript/reference/global_objects/number/isnan/index.md b/files/en-us/web/javascript/reference/global_objects/number/isnan/index.md index 22faef90172e18a..a37a10fd79acd01 100644 --- a/files/en-us/web/javascript/reference/global_objects/number/isnan/index.md +++ b/files/en-us/web/javascript/reference/global_objects/number/isnan/index.md @@ -36,12 +36,13 @@ Number.isNaN(value) ## Description -Due to both equality operators, {{jsxref("Operators", "==", - "#Equality")}} and {{jsxref("Operators", "===", "#Identity")}}, +Due to both equality operators, [`==`](/en-US/docs/Web/JavaScript/Reference/Operators/Equality) and [`===`](/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality), evaluating to `false` when checking if {{jsxref("NaN")}} _is_ {{jsxref("NaN")}}, the function `Number.isNaN()` has become necessary. This situation is unlike all other possible value comparisons in JavaScript. +Since `x !== x` is only true for `NaN` among all possible JavaScript values, `Number.isNaN(x)` can also be replaced with a test for `x !== x`, despite the latter being less readable. + In comparison to the global {{jsxref("isNaN", "isNaN()")}} function, `Number.isNaN()` doesn't suffer the problem of forcefully converting the parameter to a number. This means it is now safe to pass values that would normally diff --git a/files/en-us/web/javascript/reference/global_objects/number/issafeinteger/index.md b/files/en-us/web/javascript/reference/global_objects/number/issafeinteger/index.md index 60b7f6def6de395..831980033d261e1 100644 --- a/files/en-us/web/javascript/reference/global_objects/number/issafeinteger/index.md +++ b/files/en-us/web/javascript/reference/global_objects/number/issafeinteger/index.md @@ -16,6 +16,24 @@ provided value is a number that is a _safe integer_. {{EmbedInteractiveExample("pages/js/number-issafeinteger.html")}} +## Syntax + +```js +Number.isSafeInteger(testValue) +``` + +### Parameters + +- `testValue` + - : The value to be tested for being a safe integer. + +### Return value + +The boolean value `true` if the given value is a number that is a +safe integer. Otherwise `false`. + +## Description + A safe integer is an integer that - can be exactly represented as an IEEE-754 double precision number, and @@ -29,7 +47,7 @@ represented in IEEE-754, but the integer `2^53 + 1` can't be directly represented in IEEE-754 but instead rounds to `2^53` under round-to-nearest and round-to-zero rounding. The safe integers consist of all integers from `-(2^53 - 1)` inclusive to `2^53 - 1` -inclusive (± `9007199254740991` or ± 9,007,199,254,740,991). +inclusive (± `9_007_199_254_740_991`). Handling values larger or smaller than \~9 quadrillion with full precision requires using an [arbitrary precision arithmetic library](https://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic). @@ -38,22 +56,6 @@ information on floating point representations of numbers. For larger integers, consider using the {{jsxref("BigInt")}} type. -## Syntax - -```js -Number.isSafeInteger(testValue) -``` - -### Parameters - -- `testValue` - - : The value to be tested for being a safe integer. - -### Return value - -The boolean value `true` if the given value is a number that is a -safe integer. Otherwise `false`. - ## Examples ### Using isSafeInteger diff --git a/files/en-us/web/javascript/reference/global_objects/number/min_value/index.md b/files/en-us/web/javascript/reference/global_objects/number/min_value/index.md index f6cca2f58a59457..e22dd3486a618ce 100644 --- a/files/en-us/web/javascript/reference/global_objects/number/min_value/index.md +++ b/files/en-us/web/javascript/reference/global_objects/number/min_value/index.md @@ -15,7 +15,9 @@ The **`Number.MIN_VALUE`** property represents the smallest positive numeric val ## Description -`Number.MIN_VALUE` is the smallest positive number (not the most negative number) that can be represented within float precision — in other words, the number closest to 0. That's approximately `5E-324`. The ECMAScript spec doesn't define a precise value that implementations are required to support — instead the spec says, _"must be the smallest non-zero positive value that can actually be represented by the implementation"_. But in practice, its precise value in browsers and in Node.js is `2^-1074`. +`Number.MIN_VALUE` is the smallest positive number (not the most negative number) that can be represented within float precision — in other words, the number closest to 0. The ECMAScript spec doesn't define a precise value that implementations are required to support — instead the spec says, _"must be the smallest non-zero positive value that can actually be represented by the implementation"_. This is because small IEEE-754 floating point numbers are [denormalized](https://en.wikipedia.org/wiki/Subnormal_number), but implementations are not required to support this representation, in which case `Number.MIN_VALUE` may be larger. + +In practice, its precise value in mainstream engines like V8 (used by Chrome, Edge, Node.js), SpiderMonkey (used by Firefox), and JavaScriptCore (used by Safari) is `2^-1074`, or `5E-324`. Because `MIN_VALUE` is a static property of {{jsxref("Number")}}, you always use it as `Number.MIN_VALUE`, rather than as a property of a {{jsxref("Number")}} object you created.