Numbers in javascript

./code/numbers.js
 1// all numbers are of 64-bit floating point type
 2
 3let a = 32;   // decimal integer
 4let b = 0x20; // hexadecimal integer
 5let c = 0X20;
 6console.log(a, b, c); // 32 32 32
 7
 8let d = 0b00010000; // binary integer
 9let e = 0o20;
10console.log(d, e); // 16 16
11
12console.log(a / 5); // 6.4. Note that it is not an integer!
13
14d = 0b1000_0000; // use _ to separate digit for readability
15console.log(d);  // 128
16
17console.log(2 ** 3); // 8, ** means power
18
19// Note that we don't need to import Math
20//
21// Round to the nearest integer
22console.log(Math.round(0.6)); // 1
23console.log(Math.round(0.5)); // 1
24console.log(Math.round(0.2)); // 0
25
26console.log(Math.trunc(3.9)); // 3
27
28// Round to the nearest integer
29console.log(Math.round(-0.6)); // -1
30
31console.log(Math.ceil(-0.6)); // -0
32console.log(Math.ceil(0.6));  // 1
33console.log(Math.random());   // uniform distribution in the range 0 <= x < 1.0
34
35console.log(Math.PI); // 3.141592653589793
36console.log(Math.E);  // 2.718281828459045
37
38// max accepts arbirtrary number of arguments
39console.log(Math.max(2, 3));          // 3
40console.log(Math.max(2, 3, 10));      // 10
41console.log(Math.max(2, 3, 10, 100)); // 100
42
43console.log(Math.pow(2, 5)); // 32
44
45// note that sin() is not in degrees
46console.log(Math.sin(30));          // -0.988
47console.log(Math.sin(Math.PI / 6)); // 0.4999999
48
49// natural log. base in 2.718
50console.log(Math.log(Math.E)); // 1
51
52console.log(Math.log(100) / Math.LN10); // 2
53console.log(Math.log10(100));           // 2
54console.log(Math.log2(1024));           // 10
55
56// log1p(x) == log(1 + x)
57console.log(Math.log1p(3), Math.log(1 + 3)); // 1.38629, 1.38629
58
59console.log(Math.exp(2)); // e^2, 7.38905609893065
60
61// division by 0 is not an error!
62console.log(1 / 0);               // Infinity
63console.log(-1 / 0);              // -Infinity
64console.log(0 / 0);               // NaN
65console.log(Infinity / Infinity); // NaN
66console.log(Number.isNaN(NaN));   // true
67
68// a string to a number
69console.log(Number("123") === 123); // true
70
71console.log(parseInt("20") === 20); // true
72
73// parseInt supports hexadecimal!
74// It does not support octal or binary.
75console.log(parseInt("0x20") === 32); // true
76
77console.log(parseInt("20", 16) === 32); // true
78
79// octal
80console.log(parseInt("20", 8) === 16); // true