Number In Javascript

The JavaScript Number type is a double-precision 64-bit binary format IEEE 754 value.

sign(1bits)
exponent(11bits)
mantissa(52bits)

(1)sign×(1+mantissa)×2exponentbias(-1)^{sign} \times (1 + mantissa) \times 2^{exponent - bias}

biasbias is 10231023 for double-precision 64-bit and 127127 for single-precision 32-bit.

Number.parseFloat()

This method has the same functionality as the global parseFloat() function:

// eslint-disable-next-line unicorn/prefer-number-properties
parseFloat === Number.parseFloat // true

parseFloat() use DFA to parse string to number, when it meets a abnormal character, it will stop and return the number parsed so far, parseInt() is similar.

For example:

Number.parseFloat('123.456abc') // 123.456
Number.parseFloat('12.3.41bc') // 12.3
Number.parseFloat('-0x123') // -0
Number.parseFloat('.11.1') // 0.11
Number.parseFloat('..1') // NaN
Number.parseFloat('+1.1e-2') // 0.011
Number.parseFloat('a1.1e-2') // NaN
Number.parseFloat('.1e-2.5') // 0.001

Why 0.1 + 0.2 !== 0.3

0.10.1 in IEEE754 is below:

sign0
exponent01111111011
mantissa1001100110011001100110011001100110011001100110011010

0.20.2 in IEEE754 is below:

sign0
exponent01111111100
mantissa1001100110011001100110011001100110011001100110011010

Here is the process of add-operation for double-precision 64-bit binary format IEEE 754:

  1. Align the exponents, the smaller one will be shifted right until the exponents are equal.
  2. Add the mantissas.
  3. Normalize the result (and judge if it is overflow or underflow).
  4. Round the result to the nearest representable value.

Now we can get the binary sum result of 0.10.1 and 0.20.2:
0.1=1.1001...×240.1=1.1001... \times 2^{-4}
0.2=1.1001...×230.2=1.1001... \times 2^{-3}
1st1^{st}. 0.1=0.1100...×230.1=0.1100... \times 2^{-3}
  0.2=1.1001...×230.2=1.1001... \times 2^{-3}
2nd2^{nd}. 0.1+0.2=10.1011...×230.1+0.2=10.1011... \times 2^{-3}

   1.10011001100110011001100110011001100110011001100110100
+  0.11001100110011001100110011001100110011001100110011010
  10.01100110011001100110011001100110011001100110011001110

3rd3^{rd}. 0.1+0.2=1.01011...×220.1+0.2=1.01011... \times 2^{-2}

4th4^{th}. sign = 0
  exponent = -2 + 1023 => 01111111101
  mantissa = 0011001100110011001100110011001100110011001100110011100
=>0011001100110011001100110011001100110011001100110100

So the sum of 0.10.1 and 0.20.2 in IEEE754 is below:

sign0
exponent01111111101
mantissa0011001100110011001100110011001100110011001100110100

which is equal to 0.300000000000000040.30000000000000004.

Why 0.2 - 0.1 === 0.1

0.1=1.1001...×240.1=1.1001... \times 2^{-4}
0.2=1.1001...×230.2=1.1001... \times 2^{-3}
1st1^{st}. 0.1=0.1100...×230.1=0.1100... \times 2^{-3}
  0.2=1.1001...×230.2=1.1001... \times 2^{-3}
2nd2^{nd}. 0.10.2=0.1100...×230.1-0.2=0.1100... \times 2^{-3}

   1.10011001100110011001100110011001100110011001100110100
-  0.11001100110011001100110011001100110011001100110011010
  00.11001100110011001100110011001100110011001100110011010

3rd3^{rd}. 0.10.2=1.100...×240.1-0.2=1.100... \times 2^{-4}

4th4^{th}. sign = 0
  exponent = -4 + 1023 => 01111111011
  mantissa = 1001100110011001100110011001100110011001100110011010 =>1001100110011001100110011001100110011001100110011010

So the subtraction of 0.20.2 and 0.10.1 in IEEE754 is below:

sign0
exponent01111111011
mantissa1001100110011001100110011001100110011001100110011010

which is equal to 0.10.1.