This article mainly introduces the mathematical operations in JavaScript. This article first explains some knowledge about mathematical operations, and then provides an example of operations. If you need a friend, you can refer to JavaScript, mathematical operations can be achieved through two operations:
1. +,-, *,/, %, and other operators.
2. Use the computing functions of the Math object. For example, Math. pow (2, 3) is used to calculate the 3rd power of 2.
Unlike Java, mathematical operations in JavaScript do not throw any errors. The overflow, Division by 0, and opening of negative numbers are legal. The results are special values in JavaScript: Positive and negative Infinity (unlimited), positive and negative 0, and NaN (non-number):
1. Positive and negative Infinity. The maximum Number (Number. MAX_VALUE) is large, and the result is positive Infinity. When the calculation result is smaller than the minimum Number (-Number) That JavaScript can represent. MAX_VALUE) also takes an hour, and the result is negative Infinity. The +,-, *,/, and other mathematical operations related to Infinity follow the limit calculation rules in advanced mathematics. The result of 1/0 is positive Infinity, and the result of-1/0 is negative Infinity.
2. Positive and Negative 0. When the calculation result is positive, but smaller than the minimum decimal point (Number. MIN_VALUE), the result is positive 0; when the calculation result is negative, but greater than the maximum negative decimal (-Number. MIN_VALUE), the result is negative 0. In general, developers do not need to care about the difference between positive and negative 0.
3. NaN. For some special computing results that cannot be expressed by positive or negative Infinity, JavaScript uses NaN (it is worth noting that, although NaN literally means "not a number ", but its type is number ). These special computations include:
1). 0/0.
2). Infinity/Infinity.
3). Open the negative number.
4). convert non-numeric strings.
For Infinity and NaN, they are both "infinite" and "non-number" printed results, and they are also the global variable names in JavaScript that represent these two special values. In fact, in ECMAScript 3, the two global variables can also be assigned with other values; ECMAScript 5 corrected this maddening rule to make the two global variables read-only. In addition to directly accessing the Infinity and NaN variables, you can also use these two special values by accessing the member variables of the Number object:
1. Infinity and Number. POSITIVE_INFINITY are equivalent.
2.-Infinity and Number. NEGATIVE_INFINITY are equivalent.
3. NaN is equivalent to Number. NaN.
In JavaScript, NaN is a very interesting special value. It has a special property that is not equal to any other value (including itself. There are two methods to determine whether a value is NaN:
1. For variable x, determine x! = Whether x is true. This expression is true only when x is NaN.
2. For variable x, call the global function isNaN () in JavaScript to determine whether isNaN (x) is true. This method is not rigorous in determining NaN, because the expression isNaN (x) is true in four cases:
1). x is NaN.
2). x is a string that is not a number.
3). x is the object.
4). x is undefined.
In addition to isNaN (), JavaScript also has another useful global function: isFinite (). For variable a, isFinite (a) is true in the following cases:
1). a is number, but not NaN or positive or negative Infinity.
2). a is a string, but the content of this string is not NaN, not positive or negative Infinity numbers.
3). a is null.
4). a is a boolean value.
Since null, undefined, and other non-numeric types will affect the result, I personally think it is best to determine the type of the parameter before using isNaN () or isFinite.
Lab
The Code is as follows:
// Test Infinity
Var a = Number. MAX_VALUE;
Console. log (a * 1.1); // Infinity
Console. log (a *-1.1); //-Infinity
Console. log (1/0); // Infinity
Console. log (-1/0); //-Infinity
// Test positive/negative 0
Var B = Number. MIN_VALUE;
Console. log (B/2); // 0
Console. log (-B/2); // 0
// Test NaN
Console. log (0/0); // NaN
Console. log (Infinity/Infinity); // NaN
Console. log (Math. sqrt (-1); // NaN
Console. log (parseInt ("string"); // NaN
// Test Infinity comparison
Console. log (Infinity === Number. POSITIVE_INFINITY); // true
Console. log (-Infinity ==== Number. NEGATIVE_INFINITY); // true
// Test NaN comparison
Console. log (NaN = NaN); // false
// Test isNaN ()
Console. log (isNaN (NaN); // true
Console. log (isNaN ("42"); // false
Console. log (isNaN ("string"); // true
Console. log (isNaN ({}); // true
Console. log (isNaN (undefined); // true
Console. log (isNaN (null); // false
// Test isFinite ()
Console. log (isFinite (42); // true
Console. log (isFinite (Infinity); // false
Console. log (isFinite (NaN); // false
Console. log (isFinite ("29"); // true
Console. log (isFinite ("string"); // false
Console. log (isFinite (null); // true
Console. log (isFinite (undefined); // false
Console. log (isFinite (true); // true
Console. log (isFinite (false); // true