๐Ÿ”ฐfloating-point

๐Ÿšง under construction

JS โŸฉ value โŸฉ primitive โŸฉ number โŸฉ floating-point

the implementation of JavaScriptโ€™s Number๏ผš

  • based on the โ€œIEEE 754โ€ standard.

  • uses the โ€œdouble precisionโ€ format (aka โ€œ64-bit binaryโ€).

๐Ÿ“— You Don't Know JS: Types & Grammar

The exponent is an 11-bit from 0 to 2047, in biased form: an exponent value of 1023 represents the actual zero. Exponents range from โˆ’1022 to +1023 because exponents of โˆ’1023 (all 0s) and +1024 (all 1s) are reserved for special numbers.

๐Ÿ“— Wikipedia

Floating-Point

Number.MAX_VALUE

    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚ Number.MAX_VALUE โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

      2ยนโฐยฒยณ                                               2โนโทยน
        โ†“                                                  โ†“
    = 1.1111111111111111111111111111111111111111111111111111 * 2ยนโฐยฒยณ
        โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ 52-bit โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ
    = 1.7976931348623157e+308
    โ‰ˆ 2ยนโฐยฒโด

NaN

// โญ๏ธ `NaN` is the ONLY ONE that doesn't equal to itself.
NaN === NaN,            // falseโ—๏ธ
NaN !== NaN,            // trueโ—๏ธ

isNaN('foo'),           // trueโ—๏ธ (โญ๏ธ DON'T use isNaN())

Number.isNaN('foo'),    // false (โญ๏ธ use Number.isNaN() instead)
Number.isNaN(NaN),      // true

2 / 'foo',              // NaN

ยฑInfinity & ยฑ0

const {log} = console;

/*
    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚ Number.MAX_VALUE โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

      2ยนโฐยฒยณ                                               2โนโทยน
        โ†“                                                  โ†“
    = 1.1111111111111111111111111111111111111111111111111111 * 2ยนโฐยฒยณ
        โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ 52-bit โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ
    = 1.7976931348623157e+308
    โ‰ˆ 2ยนโฐยฒโด
*/

// ---------- log ----------

const M = Number.MAX_VALUE;
const D = Math.pow(2, 970);     // 2โนโทโฐ
const d = Math.pow(2, 969);     // 2โนโถโน

[
    // -------- Infinity --------

     1 / 0,     //  Infinity
    -1 / 0,     // -Infinity

     Infinity === Number.POSITIVE_INFINITY, // true
    -Infinity === Number.NEGATIVE_INFINITY, // true

    3 + Infinity,           // Infinity
    3 / Infinity,           //  0
    -3 / Infinity,          // -0
    Infinity / Infinity,    // NaN

    // -------- Number.MAX_VALUE --------

    M,                      // 1.7976931348623157e+308

    // โญ๏ธ ๅช่ฆ M ็š„ๆœ€ๅพŒไธ€ไฝ (2โนโทยน) ็š„ไธ€ๅŠ = 2โนโทโฐ ๅฐฑ่ถณไปฅ่ฎ“ M ใ€Œ้€ฒไฝใ€่ฎŠๆˆ Infinity
    M + D,                  // Infinity

    // โญ๏ธ ๅ†ๅฐ็š„่ฉฑ๏ผŒๅ‰‡ๆœƒ่ขซ็›ดๆŽฅใ€Œๆจๆฃ„ใ€ใ€‚
    M + d,                  // M

    // -------- Number.MAX_SAFE_INTEGER --------

    Number.MAX_SAFE_INTEGER,    // 9007199254740991 = 2โตยณ - 1
    Math.pow(2, 53) - 1,        // 9007199254740991

].forEach(x => log(x));

Last updated