JavaScript Basics-Numerical and mathematical

Source: Internet
Author: User

The pits in the values:

1. Inside JavaScript, all numbers are stored as 64-bit floating-point number, even if the integer is the same. So 1 and 1.0 are the same number, and there are no integers at all in the JavaScript language bottom. However, JavaScript automatically converts 64-bit floating-point numbers to 32-bit integers after the bitwise operation.

2, floating-point numbers are not accurate values, so the comparison involving decimals to be particularly careful. Among other things related to 0.3 as. 0.1 + 0.2 = 0.30000000000000004 instead of 0.3, so 0.1 + 0.2 ===0.3 returns false.

3, numerical accuracy: International standard ieee754,javascript floating point 64 bits, starting from the left is the---> First bit: The sign bit, 0 is positive, 1 is negative; 2nd to 12th (total 11 bits): Exponential section ; 13th to 64th (total 52 digits): Fractional part (valid number). The sign bit determines the positive or negative of a number, the exponent part determines the size of the value, and the fractional part determines the precision of the value.

  There are 11 bits in the exponential part, so the size range is 0 to 2047. IEEE 754 stipulates that if the value of the exponential portion is between 0 and 2047 (excluding two endpoints), then the first digit of the valid number is always 1, and the 64-bit floating-point numbers are not guaranteed to exist. That is, the valid numbers are always in 1.xx...xx the form, and xx..xx some of them are stored in 64-bit floating-point numbers, which can be as long as 52 bits. As a result, JavaScript provides a maximum of 53 bits of valid digits.

The accuracy can be up to 53 bits, which means that the absolute value is less than or equal to 2 of the 53-square integer, that is, 253 to 253, which can be represented precisely . values greater than 2 of 53 are not able to maintain precision (whether it is operations or storage). Since 2 of the 53 is a decimal value of 16 bits, the simple rule is that JavaScript can handle the decimal number of 15 bits precisely.

4. Range of values: according to the standard, the exponent portion of the 64-bit floating-point number is 11 bits, which means that the maximum value of the exponent part is 2047 (2 11-square minus 1). That is, the exponent portion of a 64-bit floating-point number is a maximum of 2047, and half of the value is negative, then JavaScript can represent a range of 21024 to 2-1023 (open interval), which is beyond the range.

If a number is greater than or equal to 2 of the 1024, then a "forward overflow" occurs, that is, JavaScript cannot represent such a large number, and then it returns Infinity .

If a number is less than or equal to 2-1075 squares (the exponent portion of the minimum-1023, plus the 52 bits of the fractional part), then it will occur as a "negative overflow", that is, JavaScript cannot represent such a small number, which will return 0 directly.

5, Scientific notation: The following two cases, JavaScript will automatically convert the value to scientific notation, and other cases are directly expressed in the literal form. (1) The number before the decimal point is more than 21 digits. (2) After the decimal point more than 0 more than 5.

Special values:

1, 0:javascript +0,-0, 3, but the size of itself is equal. Almost all occasions the whole 0 and negative 0 are considered normal 0, the only difference is +0, 0 do the denominator. divided by positive zero +Infinity , divided by minus -Infinity。但是0/0得NaN。NaN与任何数(包括它自己)的运算,得到的都是NaN。 0.

2,NaN: Indicates not a number, "non-numeric". It mainly occurs when parsing a string into a numeric error. the nan data type belongs to number. Not NaN equal to any value, including itself.

3, Infinite: Infinity, there are positive and negative points.

The attribute of number:

  

Number's method:

-parseint ()---1, convert a string to an integer, parseInt The return value is only two possible, either a decimal integer, or a NaN . for those numbers that are automatically converted to scientific notation, the parseInt notation for scientific notation is treated as a string, leading to some strange results. If the string 0x 0X begins or starts, parseInt it is parsed in hexadecimal numbers. If the string 0 begins, it is parsed according to the 10 binary.

2. Binary conversion (other decimal): The parseInt method can also accept the second parameter (between 2 and 36), which represents the binary of the parsed value, and returns the decimal number corresponding to that value. By default, parseInt the second parameter is 10, which is the default decimal-to-decimal. If the second parameter is 0 , undefined and null , is ignored directly. The first step of the calculation is converted to a number, and the second step is calculated by the binary.

-parsefloat ()--- parseFloat method is used to convert a string to a floating-point number. If the string conforms to the scientific notation, the corresponding conversion is made into decimals.

-IsNaN ()---Determine if a value is NaN .

-isfinite ()---Indicates if a value is a normal value.

JavaScript Basics-Numerical and mathematical

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: info-contact@alibabacloud.com 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.