The opening

It was another happy Friday evening, and I suddenly thought of the explanation of floating point number in the principle of calculation composition that I had learned recently, so I took advantage of the good mood and grasped the knowledge point, and went over JavaScript programming design, rhinoceros books and computer composition related books. I have some tips to share with you. If you have any questions, we can communicate with each other. This article focuses on the IEEE754 specification that the Javascript language follows, as do other languages that use it, such as Python

Introduction to the

We know that in daily development, will more or less use decimal operations, in the decimal operation, often found in the page suddenly appeared 1.2000000000000002 this strange result, have rich experience in the front end of the students know, oh, this simple because of the precision problem, You can use number.prototype. toFixed to convert the Number of digits after the decimal point, for example

letN1 = 0.1, n2 = 0.2letN3 = n1 + n2 //0.30000000000000004 n3.toFixed(2) // Note that toFixed is roundedCopy the code

At this point, the problem is solved, everyone is happy, so why don’t we go into the principle, why does this problem occur, let’s take a look at the results of my combing

Fixed-point number

In Float32, four digits are used to represent the integers from 0 to 9. In 32 bits, eight of these integers are represented by 32 bits. Then we use the two right-most integers from 0 to 9 as decimal parts. Consider the six integers from 0 to 9 on the left as integer parts. So, we can use 32 bits to represent 100 million real numbers from 0 to 999,999,999. This coding is Binary Coded Decimal (BCD), which is applied in supermarkets and banks. It basically meets the requirements of Decimal operations, but the disadvantage is that the space of 32 bits can represent 4 billion digits, which is now shortened to 100 million

Floating point Numbers

Floating-point (floating-point) floating-point (floating-point) floating-point (floating-point) floating-point (floating-point) floating-point (floating-point) floating-point (floating-point) floating-point

Let’s take a look at how decimal fractions are converted to binary fractions. In contrast to the binary representation of integers, which is “divide by two and look at the remainder,” decimal fractions are converted to binary using a similar reverse operation, which is to multiply by two and see if it exceeds one. If it’s more than 1, we write down 1, subtract 1 from the result, and loop further, taking an image from Simple Computer Principles



const n = 0.1
n.toString(2) / / "0.0001100110011001100110011001100110011001100110011001101"Copy the code

As can be seen, decimal 0.1 has an infinite loop in the process of converting binary decimals, followed by 0011, which is basically a circular process

Let’s convert it to decimal, as follows

letN = 0.1letN1 = n.tostring (2) // We take first"0.0001"Four decimal placesletn2 = (0 * Math.pow(2, -1)) + (0 * Math.pow(2, -2)) + (0 * Math.pow(2, -3)) + (1 * Math.pow(2, -4)) console.log(n2) //0.0625, four decimal digits, precision lostCopy the code

So this is an approximation, and now that we know what that is, how do we do decimals in IEFFF754

Specific you can go to understand IEFFF754 in detail, there will be a sign bit, index bit, effective bit explanation

conclusion

If you convert a JavaScript number to a string, you’ll find that Float64 is at most 55 digits, which is a double representation

letN = 0.1 n.t oString (2) / 0.0001100110011001100110011001100110011001100110011001101/55 Number (n.t oString (2)) / / 0.00011001100110011001 17Copy the code

The articles refer to advanced JavaScript Programming, the Definitive JavaScript Guide, Basic Computer Principles, and numerous blogs on the Web.