floating-point
๐ง under construction
JS โฉ value โฉ primitive โฉ number โฉ floating-point
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
// ๅฎ็ฒพๅบฆๆตฎ้ปๆธ
sign
โโญโexpโโโฎโญโโโโโโ fraction โโโโโโโฎ
001111100010000000000000000000000 = n
โโฐโ8-bitโฏโฐโโโโโโโ 23-bit โโโโโโโโฏ
โญ๏ธ sign โด โฑ exp โญ๏ธ โฑ fraction โญ๏ธ
n = (-1)^sign * 2^[exp - (01111111)โ] * [1 + (.01)โ]
= 1 * 2^(-3) * (1 + 1/4)
= 1 * 0.125 * 1.25
= 0.15625
// ้็ฒพๅบฆๆตฎ้ปๆธ (double precision)(64-bit)
sign: 0 -> +, 1 -> -
exp : -1022 ~ 1023 (2^1024 โ 1.8 * 10^308)
frac: aโ(2)ยฏยน + aโ(2)ยฏยฒ + ... + aโ
โ(2)ยฏโตยฒ
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
// โ n = (-1)^sign * 2^[exp - (01111111111)โ] * (1 + fraction) โ
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
sign
โ โญโโ exp โโโฎ โญโโโโโโโโโโโโโโโโโโโโโ fraction โโโโโโโโโโโโโโโโโโโโฎ
0 10000000000 0100000000000000000000000000000000000000000000000000
โฐโโ11-bitโโฏ โฐโโโโโโโโโโโโโโโโโโโโโโ 52-bit โโโโโโโโโโโโโโโโโโโโโฏ
= (-1)โฐ * 2^[(10000000000)โ - (01111111111)โ] * (1.01)โ
= 1 * 2 * (1.25)
= 2.5
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
replit - JS 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));
replit - JS: ยฑInfinity & ยฑ0
Last updated
Was this helpful?