# A deep understanding of floating-point _javascript techniques in JavaScript

Source: Internet
Author: User

JS only a numeric data type, whether it is an integer or a floating-point number, JS is classified as numbers.

typeof 17; "Number"

typeof 98.6; "Number"

typeof–2.1; "Number"

All the digits in JS are double-precision floating-point numbers. is by the IEEE754 standard to develop 64-digit coded number (this is what dongdong, do not know, back to check it)

So JS is how to express integers, double-precision floating-point number can perfectly represent up to 53-bit precision integers (no concept, not processing too much data, no use!) ), all integers from-9007199254740992 (-253) to 9007199254740992 (253) are valid double-precision floating-point numbers.

Most arithmetic operators can be computed using a combination of integers, real numbers, or both.

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, and JS does not directly calculate the operands as floating-point numbers, but instead implicitly converts them to 32-bit integers and then computes them. (To be exact, it will be converted to a 32-bit big-endian (Big-endian) of the 2 's complement representation of an integer (honestly, here really do not know what it means, for Popular science)) in bitwise-OR-op expressions

Cases:

8|1; 9

Operation Process

First, 8 and 1 are double-precision floating-point numbers. It can also be represented as a 32-bit integer, or a 32-bit binary representation.

The integer 8 is represented as a 32-bit binary for:

0000 0000 0000 0000 0000 0000 0000 1000

Or maybe.

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

The parameter of ToString is the conversion base

(Here's what I tried to convert with other cardinality, not the article)

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

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

The integer 1 is represented as a 32-bit binary for:

0000 0000 0000 0000 0000 0000 0000 0001

Run a bitwise OR

0000 0000 0000 0000 0000 0000 0000 1000

0000 0000 0000 0000 0000 0000 0000 0001

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

0000 0000 0000 0000 0000 0000 0000 1001

The same use of standard library functions parseint validation, also with 2 as the cardinality, leading 0 does not affect the results of the operation, unnecessary.

parseint (' 1001 ', 2)//9

(Here's what I tried to convert with other cardinality, not the article)

parseint (' 1001 ', 8)//513

parseint (' 1001 ', 16)//4097

The process of summing up the arithmetic operation is to convert the operand to an integer and then use the integer digit pattern to perform the operation, and finally convert the result to a standard JS floating-point number.

The warning of floating point numbers: inaccurate names. Like what

0.1+0.2; 0.30000000000000004

Reason: Although the 64-bit floating-point number is already high, double-precision floating-point numbers can only represent a limited set of numbers and cannot represent all the set of real numbers. A floating-point operation can produce only approximate results, rounded to the nearest real number that can be represented. When you perform a series of operations, as rounding errors accumulate, the results of the operations become increasingly imprecise. Rounding also causes some deviations in the law of arithmetic operations. such as the binding law. For any real number

X,y,z Total Satisfaction (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 precision and performance, and care about precision, be careful about the limitations of floating-point numbers.

The solution is to convert floating-point operations into integer operations.

(10+20) +30; 60

10+ (20+30); 60

And then remove the magnification by less. Be aware that the integer range is within -253~253.

Summarize

1, JS numbers are double precision floating-point number

2, JS Integer is only a double-precision floating-point number of a subset, not a separate type

3, the bitwise operation treats the number as a 32-bit signed integer

4, when the precision problem of the operation of the centrifugal point

The above in-depth understanding of the floating point in JavaScript is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.

Related Keywords:

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.