Introduction to the value range in JavaScript _ javascript skills

Source: Internet
Author: User
This article mainly introduces the value range in JavaScript. This article is a thorough understanding of the Number type in JavaScript, you can refer to all the numbers in JavaScript for the type of integer or decimal Number. In the program, the essence of the Number type is a 64-bit floating point Number, which is consistent with the double Type Floating Point Number in Java; therefore, all the numbers in JavaScript are floating point numbers. Following the IEEE 754 standard (Floating Point Arithmetic standard), the value range expressed by JavaScript is plus or minus 1.7976931348623157 multiplied by 10 to the power of 308, the minimum decimal Number can be expressed by plus or minus 5 multiplied by the negative 324 power of 10. The two boundary values can be obtained by accessing the MAX_VALUE attribute and MIN_VALUE attribute of the Number object respectively.

For integers, according to the requirements of the ECMAScript standard (http://ecma262-5.com/ELS5_HTML.htm#Section_8.5), JavaScript can represent and perform exact arithmetic operations in the Integer Range: Positive and Negative 2's 53 power, that is, the range from the minimum value-9007199254740992 to the maximum value + 9007199254740992. For integers that exceed this range, JavaScript can still perform operations, but the accuracy of the calculation results is not guaranteed. It is worth noting that for bitwise operations (such as shift operations) of integers, JavaScript only supports 32-bit integer numbers, that is, integers from-2147483648 to + 2147483647.

Lab

Display the absolute values of the maximum number of JavaScript and the absolute values of the smallest decimal places:

The Code is as follows:


Console. log (Number. MAX_VALUE );
Console. log (Number. MIN_VALUE );


The displayed results are 1.7976931348623157e + 308 and 5e-324.

JavaScript cannot provide accurate calculation results for integers outside the 53 power range of positive and negative 2:

The Code is as follows:


Var a = 9007199254740992;
Console. log (a + 3 );


The correct calculation result should be 9007199254740995, but the JavaScript calculation result is 9007199254740996. After you try to change the formula, you can find that as long as the integer is greater than 9007199254740992, such calculation results are frequently incorrect. If the deviation in the calculation accuracy is acceptable, the consequences of the following example are even more serious:


The Code is as follows:


Var max_int= 9007199254740992;
For (var I = MAX_INT; I <MAX_INT + 2; ++ I ){
// Infinite loop
}


Due to the computing accuracy, the above for statement will be in an endless loop.

For bitwise operations, JavaScript only supports 32-bit integer numbers:


The Code is as follows:


Var smallinet = 256;
Var bigInt = 2200000000;
Console. log (smallInt/2 );
Console. log (smallInt> 1 );
Console. log (bigInt/2 );
Console. log (bigInt> 1 );


As you can see, JavaScript can perform a correct bitwise operation on an integer (256) within 32 bits. The result is the same as that of the division operation (128 ). For integers other than 32 bits, JavaScript can perform the correct division operation (1100000000), but the result obtained after bitwise operation is far from the correct result (-1047483648 ).

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.