In JavaScript, mathematical operations can be implemented in two different ways:
1.+ 、-、 *,/,% and other operators.
2. Use the Math object's calculation function. For example, use Math.pow (2,3) to calculate 2 of the 3-time side.
Unlike Java, mathematical operations in JavaScript do not throw any errors. The overflow of the calculation, divided by 0, and the root of the negative numbers are all legal, and the result is a special value in javascript: positive and negative infinity (infinity), plus or minus 0, NaN (non-number):
1. Positive and negative infinity. When the calculated result is larger than the maximum number (Number.MAX_VALUE) that JavaScript can represent, the result is positive infinity, and when the calculation results are more than the minimum number (-number.max_value) that JavaScript can represent, The result is negative infinity. The infinity-related + 、-、 *,/and other mathematical operations are governed by the rules on limit computing in higher mathematics. The result of 1/0 is positive infinity,-1/0 and negative infinity.
2. Plus or minus 0. When the result is positive, but less than the smallest number (Number.min_value) that JavaScript can represent, the result is positive 0, and when the result is negative but larger than the maximum negative decimal (-number.min_value) that JavaScript can represent, The result is minus 0. In general, developers do not need to care about the difference between positive and negative 0.
3.NaN. For certain special computations that are not represented by a positive or negative infinity, JavaScript uses Nan to denote (notably, Nan's literal meaning is "non-number", but its type is a few). These special calculations include:
1). 0/0.
2). Infinity/infinity.
3. To root out negative numbers.
4. Numeric conversion operations on non-numeric strings.
For Infinity and Nan, they are both "infinite" and "non-number" print results, as well as global variable names in JavaScript that represent these two special values. In fact, in ECMAScript 3, these two global variables can also be assigned to other values, and ECMAScript 5 fixes this maddening rule so that the two global variables are read-only. In addition to direct access to infinity and Nan variables, you can also use these two special values by accessing the member variables of the number object:
1.Infinity is equivalent to number.positive_infinity.
2.-infinity and number.negative_infinity are equivalent.
3.NaN is equivalent to Number.NaN.
In JavaScript, Nan is an interesting special value that has a special attribute: it is not equal to any other value (including itself). There are two ways to determine whether a value is Nan:
1. For variable x, determine if X!=x is true. This expression is true only if X is Nan.
2. For variable x, call the global function isNaN () in JavaScript to determine if isNaN (x) is true. Using this method to determine Nan is actually not rigorous, because in 4 cases the expression isNaN (x) is true:
1). x is Nan.
2). x is a string, and the string is not a number.
3). x is the object.
4). x is undefined.
In addition to isNaN (), JavaScript has another useful global function: Isfinite (). For a 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 the string is a number that is not Nan, not positive or negative infinity.
3). A is null.
4). A is a Boolean value.
Because a non-numeric type such as null, undefined has an effect on the result, the individual considers it best to determine the type of the parameter before using isNaN () or isfinite ().
Experiment
Copy Code code 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 ("the"));//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);//true
Console.log (Isfinite (Infinity));//false
Console.log (Isfinite (NaN));//false
Console.log (Isfinite (")");//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