JavaScript (2): Math, infinity, and Nan. They are not cloud games, and they have been used in middle school mathematics.

Source: Internet
Author: User
Tags natural logarithm

1. mathematical operations defined by math objects

Javascript supports some complex arithmetic operations. These complex operations are implemented by using functions and constants defined as attributes of the math object:

Math. Pow (9007199254740992) // =>: 53 power of 2

Math. Round (. 6) // => 1.0: Rounding

Math. Ceil (. 6) // => 1.0: sum up

Math. Floor (. 6) // => 0.0: round down

Math. Abs (-5) // => 5: returns the absolute value.

Math. Max (x, y, z) // returns the maximum value.

Math. Min (x, y, z) // returns the minimum value.

Math. Random () // generate a pseudo-random number greater than or equal to 0 and less than 1.0

Math. Pi // π: circumference Rate

Math. E // E: base number of the natural logarithm

Math. SQRT (3) // the square root of 3

Math. Pow (3, 1/3) // The cube root of 3

Math. Sin (0) // trigonometric function: Math. Cos, math. atan, etc.

Math. Log (10) // the natural logarithm of 10

Math. Log (100)/Math. ln10 // base-10 logarithm of 100

Math. Log (512)/Math. ln2 // the base-2 logarithm of 512

Math. exp (3) // three power of e


2. Global variables infinity and Nan

(1) What do they mean?

Javascript predefines the global variables infinity and Nan to represent positive infinity and non-numeric values.

(2) Where can I see these two horses?

Arithmetic Operations in Javascript do not report errors when overflow, underflow, or division by zero.

When the number calculation result exceeds the upper limit (overflow) That javascript can represent, the result is a special infinity value, which is expressed in infinity in JavaScript. Similarly, when the value of a negative number exceeds the range of a negative number expressed by JavaScript, the result is negative infinity, which is represented by-infinity in JavaScript. The behavior characteristics of the infinite values are consistent with what we expect: Based on their addition, subtraction, multiplication, and division, the results are still infinite (of course, their plus and minus signs are also retained ).

Underflow occurs when the computation result is infinitely close to zero and smaller than the minimum value that javascript can represent. In this case, JavaScript returns 0. When a negative number overflows, JavaScript returns a special value "negative zero ". This value (negative zero) is almost the same as normal zero. Javascript programmers seldom use negative zero.

Division by zero does not return an error in javascript: it simply returns infinity or negative infinity (-infinity ). However, with one exception, Division by zero is meaningless. The result of this division operation is also a non-number (not-a-number) value, represented by Nan. If infinity is divided by infinity, any negative number is used as an open side operation, or an arithmetic operator is used together with an operand that is not a number or cannot be converted to a number, Nan is returned.

The above should be the knowledge of Middle School Mathematics. If I don't learn programming, I guess I won't be able to use it all my life ...... T_t

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.