1 binary and octal representations
ES6 provides a new writing method, which is represented by prefixes 0b (or 0b) and 0o (or 0o) respectively.
0b11110111 === 503 // true 0o767 === 503 // true
Starting from ES5, in strict mode, octal values are no longer allowed to be represented by prefix 0. ES6 further clarifies that they are represented by 0o.
// Non strict mode (function(){ console.log(0o11 === 011); })() // true // Strict mode (function(){ 'use strict'; console.log(0o11 === 011); })() // Uncaught SyntaxError // Octal literals are not allowes in strict mode.
If you want to convert to decimal values, use the Number method.
Number('0b111'); // 7 Number('0o10'); // 8
2 Number.isFinite(),Number.isNaN()
ES6 provides two new methods on the Number object.
2.1 Number.isFinite()
Used to check whether a value is finite.
Number.isFinite(15); // true Number.isFinite(0.8); // true Number.isFinite(NaN); // false Number.isFinite(Infinity); // false Number.isFinite(-Infinity); // false Number.isFinite('foo'); // false Number.isFinite('15'); // false Number.isFinite(true); // false
2.2 Number.isNaN()
Check whether a value is NaN
Number.isNaN(NaN); // true Number.isFinite(15); // false Number.isFinite("15"); // false Number.isFinite(true); // false Number.isFinite(9/NaN); // true Number.isFinite('true'/0); // true Number.isFinite('true'/'true'); // true
2.3 comparison with traditional
The traditional method first calls Number() to convert a non numeric value into a numeric value, and then makes a judgment. The new method is only valid for numeric values, and returns false for non numeric values. Number.isNaN() returns true only for NaN and false for non NaN.
isFinite(25); // true isFinite("25"); // true Number.isFinite(25); // true Number.isFinite("25"); // false isNaN(NaN); // true isNaN("NaN"); // true Number.isNaN(NaN); // true Number.isNaN("NaN"); // false Number.isNaN(1); // false
3 Number.parseInt(),Number.parseFloat()
Completely migrate the global methods parseInt() and parseFloat() to the Number() object, and the behavior remains completely unchanged.
Number.parseInt === parseInt; Number.parseFloat === parseFloat;
// Writing method of ES5 parseInt('12.34'); // 12 parseFloat('123.45#'); // 123.45 // Writing method of ES6 Number.parseInt('12.34'); // 12 Number.parseFloat('123.45#'); // 123.45
4 Number.isInteger()
Determines whether a value is an integer. It should be noted that within JavaScript, integer and floating-point numbers are stored in the same way, so 3 and 3.0 are regarded as the same value.
Number.isInteger(25); // true Number.isInteger(25.0); // true Number.isInteger(25.1); // false Number.isInteger("25"); // false Number.isInteger(true); // false
5 Number.EPSILON
This is a very small constant. The purpose is to set an error range for floating-point calculation, that is, the error range received. Floating point calculations are imprecise
Number.EPSILON // 2.220446049250313e-16 0.1 + 0.2 // 0.30000000000000004
Error check function
function withinErrorMargin (left, right) { return Math.abs(left - right) < Number.EPSILON; }; withinErrorMargin(0.1 + 0.2, 0.3); // true withinErrorMargin(0.2 + 0.2, 0.3); // false
6 Security integer and Number.isSafeInteger()
6.1 safety data
The range of integers that JavaScript can accurately represent is between - 2 ^ 53 and 2 ^ 53 (excluding two endpoints). Beyond this range, it cannot be accurately represented for a long time.
Math.pow(2, 53); // 9007199254740992 9007199254740992 // 9007199254740992 9007199254740993 // 9007199254740992 Math.pow(2, 53) === Math.pow(2, 53) + 1 // true
ES6 introduces Number.MAX_SAFE_INTEGER and Number.MIN_SAFE_INTEGER two constants, representing the upper and lower limits of this range.
Number.MAX_SAFE_INTEGER === Math.pow(2, 53) - 1 // true Number.MAX_SAFE_INTEGER === 9007199254740991 // true Number.MIX_SAFE_INTEGER === -Number.MAX_SAFE_INTEGER // true Number.MIX_SAFE_INTEGER === -9007199254740991 // true
6.2 Number.isSafeInteger()
Used to determine whether an integer falls within the range.
Number.isSafeInteger('a'); // false Number.isSafeInteger(null); // false Number.isSafeInteger(NaN); // false Number.isSafeInteger(Infinity); // false Number.isSafeInteger(3); // true Number.isSafeInteger(1.2); // false Number.isSafeInteger(9007199254740990); // true Number.isSafeInteger(9007199254740992); // false Number.isSafeInteger(Number.MIX_SAFE_INTEGER); // true Number.isSafeInteger(Number.MIX_SAFE_INTEGER - 1); // false Number.isSafeInteger(Number.MAX_SAFE_INTEGER); // true Number.isSafeInteger(Number.MAX_SAFE_INTEGER + 1); // false
When performing operations, do not only verify the results, but also verify each value involved in the operation to prevent the value involved in the operation from exceeding the range, but the result is within the range.
7 extension of Math object
ES6 adds 17 math related methods on the Math object. All these methods are static and can only be called on the Math object.
7.1 Math.trunc()
Remove the decimal part of a Number and return the integer part. For non numeric values, the Number method will be used to convert them to numeric values. If they are null or cannot intercept integer values, NaN will be returned.
Math.trunc(4.1); // 4 Math.trunc(4.9); // 4 Math.trunc(-4.1); // -4 Math.trunc(-4.9); // -4 Math.trunc(-0.1234); // -0
7.2 Math.sign()
It is used to judge whether a number is positive, negative or zero. For a non numeric value, it will be converted to a numeric value first. If it is a null value or the value of an integer cannot be intercepted, NaN will be returned.
There are five return values
- If the parameter is a positive number, return + 1
- If the parameter is negative, return - 1
- If the parameter is 0, return 0
- If the parameter is - 0, return - 0
- Other values, return NaN
7.3 Match.cbrt()
Calculate the cube root of a number. For a non numeric value, it will be converted to a numeric value first. If it is a null value or the value of an integer cannot be intercepted, NaN will be returned.
7.4 Match.clz32()
Returns the number of leading zeros in the 32-bit unsigned integer form of a number. For decimals, only the integer part is considered.
Math.clz32(0); // 32 Math.clz32(1); // 31 Math.clz32(1000); // 22 Math.clz32(0b01000000000000000000000000000000); // 1 Math.clz32(0b00100000000000000000000000000000); // 2 // Left operator Match.clz32(1 << 1); // 30 Match.clz32(1 << 2); // 29 Match.clz32(1 << 29); // 2
For null values or other types of values, the Math.clz32 method converts them to numeric values before calculating them.
Math.clz32(); // 32 Math.clz32(NaN); // 32 Math.clz32(Infinity); // 32 Math.clz32(null); // 32 Math.clz32('foo'); // 32 Math.clz32([]); // 32 Math.clz32({}); // 32 Math.clz32(true); // 32
7.5 Math.imul()
Returns the result of multiplying two numbers in the form of 32-bit signed integers. It also returns a 32-bit signed integer.
Math.imul(2, 4); // 8 Math.imul(-1, 8); // -8 Math.imul(-2, -2); // 4
If only the last 32 bits are considered, the results of Math.imul(a, b) and a*b are the same in most cases. However, if overflow occurs, the low-order value is often inaccurate during multiplication, so it is necessary to use this method to return the correct low-order value.
(0x7fffffff * 0x7fffffff) | 0 // 0 Math.imul(0x7fffffff, 0x7fffffff) // 1
7.6 Math.fround()
Returns the single precision floating-point form of a number.
Math.fround(0) // 0 Math.fround(1) // 1 Math.fround(1.337) // 1.3370000123977661 Math.fround(1.5) // 1.5 Math.fround(NaN) // NaN
7.7 Math.hypot()
Returns the square root of the sum of squares of all parameters. If the parameter is not a numeric value, the Math.hypot method converts it to a numeric value. NaN is returned whenever a parameter cannot be converted to a value.
7.8 logarithmic method
ES6 adds four logarithmic correlation methods
7.8.1 Math.expm1()
Returns e^x - 1, which is Math.exp(x) - 1.
Math.expm1(-1) // -0.6321205588285577 Math.expml(O) // 0 Math.expml(1) // 1.718281828459045
7.8.2 Math.log1p()
Return ln(1+x), that is, Math.log(1 + x). If x is less than - 1, NaN is returned.
Math.log1p(1); // 0.6931471805599453 Math.log1p(0); // 0 Math.log1p(-1); // -Infinity Math.log1p(-2); // NaN
7.8.3 Math.log10()
Returns the logarithm of base 10 X. If x is less than 0, NaN is returned.
Math.log10(2); // 0.3010299956639812 Math.log10(1); // 0 Math.log10(0); // -Infinity Math.log10(-2); // NaN Math.log10(100000); // 5
7.8.4 Math.log2()
Returns the logarithm of base 2 X. If x is less than 0, NaN is returned.
Math.log10(2); // 1 Math.log10(1); // 0 Math.log10(0); // -Infinity Math.log10(-2); // NaN Math.log10(1024); // 10 Math.log10(2 << 29); // 29
7.9 hyperbolic function method
- Math.sinh(x) returns the hyperbolic sine of X
- Math.cosh(x) returns the hyperbolic cosine of X
- Math.tanh(x) returns the hyperbolic tangent of X
- Math.asinh(x) returns the inverse hyperbolic sine of X
- Math.acosh(x) returns the inverse hyperbolic cosine of X
- Math.atanh(x) returns the inverse hyperbolic tangent of X
8 Math.signbit()
Math.sign() is used to judge the positive and negative of a value. If the parameter is - 0, it will return - 0
Math.sign(-0) // -0
In this case, Math.sign() is not very useful when judging the positive and negative sign bits. JavaScript internally uses 64 bit floating-point numbers (international standard IEEE754) to represent values. IEEE754 specifies the sign bits when the first bit is used, 0 represents a positive number and 1 represents a negative number. So there are two zeros. Judging - 0 and + 0 at the same time is troublesome because they are equal.
**Proposal: * * introduce the Math.signbit() method to judge whether the sign bit of a number has been set.
Math.signbit(2); // false Math.signbit(-2); // true Math.signbit(0); // false Math.signbit(-0); // true
- If the parameter is NaN, false is returned
- The parameter is - 0 and returns true
- If the parameter is negative, return true
- false is returned in other cases
9 exponential operator
ES6 adds an exponential operator (* *)
2 ** 2 // 4 2 ** 3 // 8
The exponent operator can be combined with the equal sign to form a new assignment operator (* * =).
let a = 1.5; a **= 2; // Equivalent to a = a * a; a **= 3; // Equivalent to a = a * a * a;
In the V8 engine, the implementation of the exponential operator is different from that of Math.pow. For particularly large operation results, there will be slight differences between the two.
Math.pow(99, 99); // 3.697296376497263e+197 99 ** 99 // 3.697296376497268e+l97
The last significant digit is different.
10 Integer data type
10.1 introduction
All numbers in JavaScript save 64 bit floating-point numbers, which determines that the accuracy of integers can only be up to 53 binaries.
**Proposal: * * introduce a new data type Integer (Integer) to represent integers without bit limit.
In order to distinguish from Number type, Integer type data must be represented by suffix n.
1n + 2n // 3n 0b1101n // Binary 0o777n // octal number system 0xFFn // hexadecimal typeof 123n // 'integer'
JavaScript primitives provide Integer objects to generate numeric values of type Integer. The conversion rules are basically consistent with Number().
Integer(123); // 123n Integer('123'); // 123n Integer(false); // 0n Integer(true); // 1n
The following usage will report an error
new Integer(); // TypeError Integer(undefined); // TypeError Integer(null); // TypeError Integer('123n') // SyntaxError Integer('abc') // SyntaxError
10.2 calculation
In terms of mathematical operations, the four binary operators of Integer type +, -, * and * * are consistent with the behavior of Number type. Division / will round off the decimal part and return an Integer.
9n / 15n // 1n
Almost all Number operations can be used in Integer, except for two:
- Unsigned right shift operator > > >. Because Integer has no highest bit, it is meaningless
- Unary proof operator. In asm.js, the Number type or error message is always returned.
Number type cannot be mixed with Integer type
The equality operator will change the data type, and mixing is not allowed
The exact equality operator does not change the data type, so it can be mixed