Curriculum
Module 35 // Core JavaScript
Numbers & IEEE-754
Module Objective
Floating-point precision, NaN, Infinity, signed zero
Mental Model Realtime Simulation
INTERACTIVE_CANVASEditor_Pane
Loading...
Console_Output
Waiting for output...
Practical Code Examples
// Example 1
// 1. The 0.1 + 0.2 Paradox
const result = 0.1 + 0.2;
console.log(result); // 0.30000000000000004
console.log(result === 0.3); // false💡 Due to how binary floating-point math works, some decimals cannot be represented exactly. For financial calculations, always work with cents (integers) or use libraries like `decimal.js`.
// Example 2
// 2. Safe Integers
const max = Number.MAX_SAFE_INTEGER; // 2^53 - 1
console.log(max);
console.log(max + 1); // ✅ Works
console.log(max + 2); // ❌ Same result as max+1 (Precision lost!)💡 JavaScript numbers only guarantee precision up to ~15-17 significant digits. If you need larger numbers, use `BigInt`.
// Example 3
// 3. Special Values
console.log(1 / 0); // Infinity
console.log(-1 / 0); // -Infinity
console.log(0 / 0); // NaN (Not a Number)
console.log(typeof NaN); // "number" (The ultimate irony)💡 Infinity and NaN are technically part of the Number type. NaN results from failed math operations like `Math.sqrt(-1)`.
Engine & Memory Architecture
Binary Representation
1. IEEE 754 Standard:
- Every number takes 8 bytes (64 bits) in memory.
- 1 bit for sign, 11 bits for exponent, and 52 bits for fraction (mantissa).
2. Storage Location:
- Small integers are often optimized by engines (like V8) to stay in the Stack as 31-bit or 32-bit values (called SMIs).
- Larger numbers or doubles are stored in the Heap as "heap numbers."
3. CPU Context:
- Math operations are performed directly by the Floating Point Unit (FPU) in the CPU.
- Performance: Adding two optimized integers is significantly faster than adding two heap-allocated doubles.