preface

Start a small egg, JS all rely on enlightenment. Let’s have a very simple topic so that we can understand what we are going to talk about more quickly

0.1 + 0.2=?Copy the code

Normally it must be 0.3, but when you check the result on the console, you know 0.30000000000000004. Wow,amazing! Now let’s think about a few questions and then take the questions together and ask why?

  1. In what cases can accuracy be lost?
  2. Why is the accuracy lost?
  3. How to solve this situation?

The old driver starts

╭ ╩ ═ ═ ╮ ╔ ═ ═ ═ ═ ═ ═ ╗ ╔ ═ ═ ═ ═ ═ ═ ╗ ╔ ═ ═ ═ ═ ═ ═ ═ ╗ ╔ ═ ═ ═ ═ ╗ ╭ beeping ╠ ╣ don't run ╠ ╣ ╠ ╣ ╣ ╰ even ═ ═ you ╯ ╚ when ═ ═ ═ ═ does ╝ ╚ when ═ ═ ═ ═ does ╝ ╚ when ═ ═ ═ ═ ═ ╝Copy the code

JS digital precision loss of some typical problems

Add two simple floating point numbers

/ / add = = = = = = = = = = = = = = = = = = = = =
0.1 + 0.2 = 0.30000000000000004
0.2 + 0.4 = 0.6000000000000001

/ / subtraction = = = = = = = = = = = = = = = = = = = = =
0.3 - 0.2 = 0.09999999999999998
 
/ / the multiplication = = = = = = = = = = = = = = = = = = = = =
0.8 * 3 = 2.4000000000000004

/ / division = = = = = = = = = = = = = = = = = = = = =
0.3 / 0.1 = 2.9999999999999996
Copy the code

Congratulations on triggering the first Easter egg. As we all know, JS is not very good at dealing with decimals, especially those related to money. JS generally replaces these decimals with values that it can represent.

When you enter a decimal point in code or read a number with a decimal point from some data, you’re basically labeling your program as “flawed.” Sometimes the errors are so small that you can ignore them, sometimes the errors in multiple numbers cancel each other out, but sometimes the errors add up.

Integer arithmetic

/ / add = = = = = = = = = = = = = = = = = = = = =
9007199254740992 + 1 = 9007199254740992

/ / subtraction = = = = = = = = = = = = = = = = = = = = =
9007199254740993 - 1 = 9007199254740991
 
/ / the multiplication = = = = = = = = = = = = = = = = = = = = =Aye not found/ / division = = = = = = = = = = = = = = = = = = = = =
100 / 3 = 33.333333333333336
Copy the code

Why is there a loss of accuracy?

  • In JavaScript, in accordance with IEEE 754 standards, the Number type in the program is essentially a 64-bit fixed length floating point Number, that is, the standard double precision floating point Number.
  • Binary implementations and bit limits of computers Some numbers cannot be represented finitely. Just like some irrational numbers cannot be expressed in a finite way, such as PI 3.1415926… , 1.3333… And so on. JS complies with IEEE 754 specification, adopts double precision storage, occupying 64 bit.
  • When converted to binary scientific notation, only 64 bits of valid numbers are reserved, which can only be rounded like decimal, but binary has only 0 and 1, so it is rounded by 0 and 1. If something goes wrong in this step, it goes wrong step by step, and it is only natural that there will be errors in the computer’s memory of decimals. This is part of the floating point calculation in the computer error, which is the root cause of the loss of accuracy

How to solve this situation?

For integers, the probability of front-end problems is probably low, since very few businesses need to use very large integers, and as long as the results don’t exceed Math.pow(2, 53), there is no loss of precision.

A simple method

  • For decimal, the probability of front-end problems is still a lot of, especially in some e-commerce sites involving amounts and other data. Solution: Place decimals in place integers (multiples) and shrink back (divides multiples)
  • toFixed()

The class library

  • Math.js
  • big.js