preface

For the technology of the current situation of the community now, I feel most of the analysis of user’s portrait users is more like a short article frequency fast rhythm, so it is a simple interview questions, try to use the shortest time mining will be behind the knowledge share with everyone, like in a subsequent interview, can help you, if is just their intellectual blind area might as well to help support the thumb up

background

0.3 - 0.1 / / 0.19999999
0.2 + 0.1 / / 0.30000000000000004
Copy the code

In Javascript, the addition or subtraction of small numbers will lead to accuracy problems, which, in summary, belong to the basic knowledge related to binary in Javascript. This article introduces how to implement methods to solve this problem, the following will list several solutions, welcome to supplement correction, thank you

Method a

The simplest and most straightforward solution, as we are familiar with, is to use third-party functionality already implemented, in this case bignumber.js

Official document Github address

import BigNumber from "bignumber.js";

x = new BigNumber(0.3)
x.minus(0.1)                        / / "0.2"
x                                   / / "0.3"

x.plus(0.2)
x                                   / / "0.3"
Copy the code

Method 2

In case the interviewer asks you to solve the problem by hand, the idea is to add the decimal to the whole number, and then convert it back to the decimal output. Note that the substr gets the index by +1

Addition & subtraction
// Get the number of decimal places
const getDecimalPlaces = num= > {
    // If it is an integer
    if (Math.floor(num) === num) {
        return 0
    }
    
    const str = String(num);
    const pointIdx = str.indexOf('. ')
    const len = str.substr(pointIdx + 1).length;
    
    return len;
}

// Number addition
const add = (num1, num2) = > {
    if (!Number.isNaN(num1) && !Number.isNaN(num2)) return new TypeError('Please pass in the number type')
    let result = 0;
    
    const num1Len = getDecimalPlaces(num1); // The length of the digit 1
    const num2Len = getDecimalPlaces(num2); // The length of the digit 2
    const maxLen = num1Len > num2Len ? num1Len : num2Len; // Take a longer digit between the two
    const maxTimes = Math.pow(10, maxLen); // The maximum multiple of 10
    
    result = (num1 * maxTimes + num2 * maxTimes) / maxTimes;
    
    return result;
}

// Number subtraction
const minus = (num1, num2) = > {
    if (!Number.isNaN(num1) && !Number.isNaN(num2)) return new TypeError('Please pass in the number type')
    let result = 0;
    
    const num1Len = getDecimalPlaces(num1); // The length of the digit 1
    const num2Len = getDecimalPlaces(num2); // The length of the digit 2
    const maxLen = num1Len > num2Len ? num1Len : num2Len; // Take a longer digit between the two
    const maxTimes = Math.pow(10, maxLen); // How many zeros need to be added
    
    result = (num1 * maxTimes - num2 * maxTimes) / maxTimes; // Price reduction is just a number manipulation notation difference
    
    return result;
}
Copy the code

Method three

Feel free to add in the comments section…

Core principles

In a nutshell: Javascript floating-point number storage follows the implementation of ieEE-754 standard. When storing numbers, the accuracy of the last few digits is caused by the storage logic, as shown in the following figure

If you store the mantissa, you have a precision problem when you add it up,

Mantissa is not stored, adding and subtracting is not a precision problem, such as 0.25,0.125,0.5… , etc.

summary

This article ends here, the level is limited inevitably have mistake, welcome correction error. Finally, I hope you can give me some compliments, which will be a great affirmation and motivation for my creation. I hope I can help you

This article refer to

Core Base Binary (I) 0.1 + 0.2! = 0.3 and IEEE-754 standard Number API Dev Tools