why

The biggest problem with binary floating-point numbers (not only JavaScript, but all languages that follow the IEEE 754 specification) is the following:

0.1 + 0.2 === 0.3; // false
Copy the code

Mathematically, the above condition should be true, but why is the result false? In short, 0.1 and 0.2 in binary floating-point numbers are not very precise. Instead of adding up to exactly 0.3, they add up to a closer number, 0.30000000000000004, so the condition is false.

Determine whether two numbers are equal

The most common method is to set an error range value, often referred to as “machine epsilon”, which is usually 2^-52 (2.220446049250313E-16) for JavaScript numbers. Starting with ES6, this value is defined in number. EPSILON, which we can use directly or write for versions prior to ES6

/ / polyfill: if (! Number.EPSILON) { Number.EPSILON = Math.pow(2,-52); }Copy the code

You can use number.epsilon to compare whether two numbers are equal (within a specified margin of error)

function numbersCloseEnoughToEqual(n1,n2) { return Math.abs( n1 - n2 ) < Number.EPSILON; } var a = 0.1 + 0.2; Var b = 0.3; numbersCloseEnoughToEqual( a, b ); / / true numbersCloseEnoughToEqual (0.0000001, 0.0000002); //falseCopy the code

The largest floating point Number that can be rendered is about 1.798e+308 (which is a pretty big Number), which is defined in number.max_value. The minimum floating point Number, defined in number.min_value, is about 5e-324, which is not negative but is infinitely close to 0

Math.js is used to handle the operations

Github.com/josdejong/m…

Check whether it is an integer

ES6 grammatical Number. IsInteger ()

Polyfill writing

if (!Number.isInteger) {
	Number.isInteger = function(num) {
		return typeof num == "number" && num % 1 == 0;
  	} 
}
Copy the code

Whether a safe integer is number.isSafeINTEGER ()

Check if it’s a number

Typeof NaN == “number”

ES6 grammar

Number.isNaN(123) // false Number.isNaN("123") // false Number.isNaN("abc") // false Number.isNaN(1/"abc")// true Number. IsNaN (1/0) // false because 1/0 is InfinityCopy the code

ES5 writing

isNaN(123)//false
isNaN("123")//false
isNaN("abc")//true
isNaN(1/"abc")//true
isNaN(1/0)//false
Copy the code

You can also use the ES6 syntax: Object.is(num,NaN)

Check if it is finite

IsFinite () Specifies whether the parameter is a finite number.

IsFinite () may convert the parameter to a Number, while number.isFinite () does not

Returns false if the argument is NaN, plus or minus infinity, and true otherwise.

The sample

isFinite(Infinity);  // false
isFinite(NaN);       // false
isFinite(-Infinity); // false

isFinite(0);         // true
isFinite("0");       // true
isFinite(1/0);		 // false
isFinite(null);      // true
isFinite(undefined); // false

Copy the code

ES6 grammatical Number. IsFinite ()

Number.isFinite(0);         // true
Number.isFinite("0");       // false
Number.isFinite(1/0);		// false
Number.isFinite(null);      // false
Number.isFinite(undefined);	// false
Copy the code

conclusion

Use number.isnan () to check if a Number is invalid.

Make sure the numbers are operable using number.isfinite ()