A deep understanding of floating point numbers in JavaScript and a deep understanding of javascript

Source: Internet
Author: User

A deep understanding of floating point numbers in JavaScript and a deep understanding of javascript

Javascript has only one numeric data type. js classifies it as a number, whether it is an integer or a floating point number.

Typeof 17; // "number"

Typeof 98.6; // "number"

Typeof-2.1; // "number"

All numbers in js are double-precision floating-point numbers. It is a 64-bit encoded number developed by the IEEE754 standard (what is this? I don't know. Check it later)

So how does js express integers? double-precision floating-point numbers can perfectly represent up to 53-bit precision integers (no concept, too much big data to be processed, no use of it !), All integers from-9007199254740992 (-253) to 9007199254740992 (253) are valid double-precision floating point numbers.

Most arithmetic operators can be calculated using integers, real numbers, or a combination of the two.

0.1*1.9 // 0.19

-99 + 100 // 1

21-12.3 // 8.7

2.5/5 // 0.5

21% 8 // 5

Arithmetic Operators are special. js does not directly calculate the operands as floating-point numbers, but implicitly converts them to 32-bit integers for calculation. (To be exact, it will be converted to an integer indicated by the 2's complement code of the 32-bit big-endian (to be honest, I really don't know what it means, so I want to know it )) the bitwise OR operation expression is

Example:

8 | 1; // 9

Operation Process

First, 8 and 1 are double-precision floating point numbers. But it can also be expressed as a 32-bit integer, that is, a 32-bit binary representation.

The integer 8 represents 32-bit binary:

0000 0000 0000 0000 0000 0000 0000

Or

(8). toString (2); // "1000"

The toString parameter is the conversion base number.

(The conversion from other bases is not related to this Article)

(8). toString (8); // "10"

(8). toString (16); // "8"

Integer 1 indicates 32-bit binary:

0000 0000 0000 0000 0000 0000 0000

Run by bit or

0000 0000 0000 0000 0000 0000 0000

0000 0000 0000 0000 0000 0000 0000

--------------------------------------------

0000 0000 0000 0000 0000 0000 0000

The same uses the standard library function parseInt for verification, and also uses 2 as the base. Leading 0 does not affect the calculation result and is not necessary.

ParseInt ('000000', 2) // 9

(The conversion from other bases is not related to this Article)

ParseInt ('20140901', 8) // 1001

ParseInt ('20140901', 16) // 1001

The process of summing up the arithmetic operation is to convert the operand to an integer, then perform the operation in the integer mode, and finally convert the result to a standard js floating point number.

Floating Point Number warning: the output name is not accurate. For example

0.1 + 0.2; // 0.30000000000000004

Cause: although the 64-bit floating point number has a high precision, the double-precision floating point number can only represent a limited number, not all real number sets. Floating-point operations can only generate approximate results, rounded to the nearest real number that can be expressed. When you perform a series of operations, the calculation results become increasingly inaccurate as the rounding error accumulates. Rounding also produces some deviations from the law of arithmetic operations. For example, combination law. For any real number

X, y, and z always meet (x + y) + z = x + (y + z)

Floating point numbers are not necessarily:

(0.1 + 0.2) + 0.3; // 0.6000000000000001

0.1 + (0.2 + 0.3); // 0.6

Floating Point Numbers weigh both precision and performance. When you are concerned about precision, be careful with the limitations of floating point numbers.

The solution is to convert the floating point operation to the Integer Operation.

(10 + 20) + 30; // 60

10 + (20 + 30); // 60

Then, remove the magnification. Note that the Integer Range is-253 ~ Within 253.

Summary

1. js numbers are double-precision floating point numbers.

2. The js integer is only a subset of the double-precision floating point number, not a separate type.

3. bitwise operations treat a number as a 32-bit signed integer

4. Be careful about the precision of floating point operations.

The above in-depth understanding of the floating point number in JavaScript is all the content shared by the editor. I hope to give you a reference and support for the help house.

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.