ES6 learning notes -- numerical expansion

preface:

ES6 also provides many convenient operations for our numerical operation. Please refer to the official documents for details.

1, Binary and octal notation

ES6 provides a new way to write Binary and Octal values, which are represented by prefixes 0b (or 0b) and 0o (or 0o) respectively.

0b111110111 === 503 // true, binary
0o767 === 503 // true, octal

2, Number object method

methoddescribe
Number.isFinite()Check whether a value is finite, that is, it is not infinite
Number.isNaN()Used to check whether a value is NaN.
Number.parseInt()Converts a string to an integer type
Number.parseFloat()Converts a string to a floating point number type
Number.isInteger()Used to determine whether a value is an integer.

ES6 transplants the global methods parseInt() and parseFloat() to the Number object, and the behavior remains completely unchanged. The purpose of this is to gradually reduce the global method and make the language modular.

3, Number.EPSILON

ES6 adds a tiny constant Number.EPSILON on the Number object. According to the specification, it represents the difference between 1 and the minimum floating-point Number greater than 1. The value is: 2 to the - 52nd power

Number.EPSILON === Math.pow(2, -52)
// true
Number.EPSILON
// 2.220446049250313e-16
Number.EPSILON.toFixed(20)
// "0.00000000000000022204"

It is mainly used to set an error range for floating point calculation. For example, the error range is set to the - 50th power of 2 (i.e. number. Epsilon * math. Pow (2,2)), that is, if the difference between two floating-point numbers is less than this value, we consider the two floating-point numbers to be equal.

function withinErrorMargin(left, right) {
    return Math.abs(left - right) < Number.EPSILON * Math.pow(2, 2);
}
console.log(0.1 + 0.2 === 0.3); // false
console.log(withinErrorMargin(0.1 + 0.2, 0.3)); // true
console.log(1.1 + 1.3 === 2.4); // false
console.log(withinErrorMargin(1.1 + 1.3, 2.4)); // true

4, Security integer and Number.isSafeInteger()

The range of integers that JavaScript can accurately represent is (- 253 + 1) to (253 - 1). Beyond this range, this value cannot be accurately represented.
ES6 introduces Number.MAX_SAFE_INTEGER and Number.MIN_SAFE_INTEGER these two constants are used to represent the upper and lower limits of this range.

console.log(Number.MAX_SAFE_INTEGER === Math.pow(2, 53) - 1);
// true
console.log(Number.MAX_SAFE_INTEGER === 9007199254740991);
// true
console.log(Number.MIN_SAFE_INTEGER === -Number.MAX_SAFE_INTEGER);
// true
console.log(Number.MIN_SAFE_INTEGER === -9007199254740991);
// true

Number.isSafeInteger() is used to determine whether an integer falls within this range.

Number.isSafeInteger(3) // true
Number.isSafeInteger(1.2) // false
Number.isSafeInteger(9007199254740990) // true
Number.isSafeInteger(9007199254740992) // false
Number.isSafeInteger(Number.MIN_SAFE_INTEGER - 1) // false
Number.isSafeInteger(Number.MIN_SAFE_INTEGER) // true
Number.isSafeInteger(Number.MAX_SAFE_INTEGER) // true
Number.isSafeInteger(Number.MAX_SAFE_INTEGER + 1) // false

5, Exponential operator

ES2016 adds an index operator (* *).

// Equivalent to 2 * * (3 * * 2), the power of 2 (the 2nd power of 3), that is, the 9th power of 2
2 ** 3 ** 2 
// 512

let a = 1.5;
a **= 2; // Power of 1.5
// Equivalent to a = a * a;
let b = 4;
b **= 3; // The third power of 4
// Equivalent to b = b * b * b;

6, BigInt data type

BigInt is only used to represent integers. There is no limit on the number of bits. Integers with any number of bits can be accurately represented.

1234 // Ordinary integer
1234n // BigInt
// Operation of BigInt
1n + 2n // 3n

BigInt and ordinary integers are two values that are not equal.

42n === 42 // false

For more details, please refer to the official website

7, Extension of Math object

Please refer to here for details

Keywords: Javascript ECMAScript

Added by ThinkGeekness on Sat, 06 Nov 2021 05:06:57 +0200