We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
js在浮点数进行加法和乘法时(减法和除法类比即可)经常会出现精度问题,下面分别给出加法和乘法中的解决方式,主要是通过先将运算式中的值先变成整数:
加法:
function accAdd(arg1, arg2) { var r1, r2, m; try { r1 = arg1.toString().split(".")[1].length } catch (e) { r1 = 0 } try { r2 = arg2.toString().split(".")[1].length } catch (e) { r2 = 0 } m = Math.pow(10, Math.max(r1, r2)); return (arg1 * m + arg2 * m) / m; };
乘法:
function FxF(f1, f2) { f1 += ''; f2 += ''; var f1Len, f2Len; try { f1Len = f1.toString().split(".")[1].length } catch (e) { f1Len = 0 } try { f2Len = f2.toString().split(".")[1].length } catch (e) { f2Len = 0 } f1Len && (f1 = f1.replace('.', '')); f2Len && (f2 = f2.replace('.', '')); return f1 * f2 / Math.pow(10, f1Len + f2Len); }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
js在浮点数进行加法和乘法时(减法和除法类比即可)经常会出现精度问题,下面分别给出加法和乘法中的解决方式,主要是通过先将运算式中的值先变成整数:
加法:
乘法:
The text was updated successfully, but these errors were encountered: