SoFunction
Updated on 2025-04-06

Deep understanding of floating point numbers in JavaScript

js has only one numerical data type. Whether it is an integer or a floating point number, js classifies it as a 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 coded number formulated by the IEEE754 standard (what is this thing, I don’t know, just look it up and check it out)

So how does js express integers? Double precision floating point numbers can perfectly represent integers with up to 53-bit precision (no concept, not dealing with too much data, not used up!), 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 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 quite special. JS does not directly calculate operands as floating point numbers, but implicitly converts them into 32-bit integers to perform operations. (To be precise, it will be converted into an integer representing 2 of 32-bit big-endian (to be honest, I really don't know what it means here, please search for popular science)) Use bitwise or operation expressions as

example:

8|1; //9

Computation process

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

Integer 8 is represented as 32-bit binary as:

0000 0000 0000 0000 0000 0000 0000 1000

It may also be

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

The parameter toString is the conversion base

(The following is what I tried to convert with other cardinality, and it has nothing to do with this article)

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

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

Integer 1 is represented as 32-bit binary as:

0000 0000 0000 0000 0000 0000 0000 0001

Run 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 uses the standard library function parseInt verification, and uses 2 as the cardinality. The leading 0 does not affect the calculation result, which is unnecessary.

parseInt('1001',2) //9

(The following is what I tried to convert with other cardinality, and it has nothing to do with this article)

parseInt('1001',8) //513

parseInt('1001',16) //4097

The process of summarizing the arithmetic operation is to convert operands into integers, then use integer bit mode to perform operations, and finally convert the result into a standard js floating point number.

Warning of floating point numbers: Notoriously inaccurate. for example

0.1+0.2; //0.30000000000000004

Reason: Although the accuracy of 64-bit floating-point numbers is already very high, double-precision floating-point numbers can only represent a finite set of numbers, but cannot represent all sets of real numbers. Floating point operations can only produce approximate results, rounding to the closest representable real number. When you perform a series of operations, as the rounding error accumulates, the operation results will become less accurate. Rounding also causes some deviations in the laws of arithmetic operations. For example, the law of bonding. For any real number

x,y,z will always satisfy (x+y)+z=x+(y+z)

Floating points are not necessarily:

(0.1+0.2)+0.3; //0.6000000000000001

0.1+(0.2+0.3); //0.6

Floating point numbers weigh the accuracy and performance. When you 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

Then divide the magnification factor. Note that the integer range should be within -253~253.

Summarize

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

2. The integer of js is just a subset of double-precision floating-point numbers, not a single type

3. Bit operation treats numbers as signed integers of 32 bits

4. Beware of the accuracy of floating-point operations

The above article in-depth understanding of floating point numbers in JavaScript is all the content I have shared with you. I hope you can give you a reference and I hope you can support me more.