JavaScript浮点数运算精度问题分析与解决策略
一、问题描述
近期在开发项目中,页面涉及大量JavaScript浮点数运算,发现其存在明显的精度缺陷。例如:
0.1+0.2 == 0.30000000000000004
0.1 + 0.7 == 0.7999999999999999
7*0.8 == 5.6000000000000005
5.6/7 == 0.7999999999999999
二、解决方案
JavaScript在进行数值运算后往往会产生微小的误差,这与.NET或Java等语言的精确性不同。虽然JS的设计重点并非高性能数值计算,但在某些业务场景中必须保证准确性。经过分析,大致有两种可行的解决路径。
A方案一:
对运算结果保留2至3位小数。由于前端界面涉及的计算相对简单,通常对精度的要求并不苛刻,因此截取两位小数即可满足需求。
B方案二:
将小数位数转换为整数进行运算。具体示例如下:
0.1+0.2 =》 (1+2)/10 == 0.3
0.1 + 0.7 =》 (1+7)/10 == 0.8
7*0.8 == (7*8)/10 == 5.6
5.6/7 == (56/7)/10 == 0.1
为了便于调用,可以提取一个公共方法库。例如下面的JSMath库,它重写了加减乘除功能,会先将参数转换为整数再执行运算,调用方式为JSMath(参数1).操作(参数2)。
参数1和参数2分别代表参与运算的第一个和第二个Number类型数值。计算完成后,通过Value属性获取最终结果。
(function() {
var JSMath = function() {
return this;
}
JSMath.prototype.from = function(value) {
// 支持JSMath参数传递主要是用于嵌套的调用
if ((typeof(value) == 'object') && (value.value != undefined)) {
this.value = value.value;
} else {
this.value = value;
}
return this;
}
// 加法
JSMath.prototype.add = function(value) {
var thisValueString = this.value.toString();
var valueString = value.toString();
var timesCount1 = 0;
var timesCount2 = 0;
if (thisValueString.indexOf('.') > 0) {
timesCount1 = thisValueString.split('.')[1].length;
}
if (valueString.indexOf('.') > 0) {
timesCount2 = valueString.split('.')[1].length;
}
var maxtimeCount = timesCount1 > timesCount2 ? timesCount1 : timesCount2;
this.value = (Math.pow(10, maxtimeCount) * this.value + Math.pow(10, maxtimeCount) * value) / Math.pow(10, maxtimeCount);
return this;
}
// 减法
JSMath.prototype.sub = function(value) {
var thisValueString = this.value.toString();
var valueString = value.toString();
var timesCount1 = 0;
var timesCount2 = 0;
if (thisValueString.indexOf('.') > 0) {
timesCount1 = thisValueString.split('.')[1].length;
}
if (valueString.indexOf('.') > 0) {
timesCount2 = valueString.split('.')[1].length;
}
var maxtimeCount = timesCount1 > timesCount2 ? timesCount1 : timesCount2;
this.value = (Math.pow(10, maxtimeCount) * this.value - Math.pow(10, maxtimeCount) * value) / Math.pow(10, maxtimeCount);
return this;
}
// 除法
JSMath.prototype.div = function(value) {
var thisValueString = this.value.toString();
var valueString = value.toString();
var timesCount1 = 0;
var timesCount2 = 0;
if (thisValueString.indexOf('.') > 0) {
timesCount1 = thisValueString.split('.')[1].length;
}
if (valueString.indexOf('.') > 0) {
timesCount2 = valueString.split('.')[1].length;
}
var maxtimeCount = timesCount1 > timesCount2 ? timesCount1 : timesCount2;
this.value = ((Math.pow(10, maxtimeCount) * this.value) / (Math.pow(10, maxtimeCount) * value));
return this;
}
// 乘法
JSMath.prototype.times = function(value) {
var thisValueString = this.value.toString();
var valueString = value.toString();
var timesCount1 = 0;
var timesCount2 = 0;
if (thisValueString.indexOf('.') > 0) {
timesCount1 = thisValueString.split('.')[1].length;
}
if (valueString.indexOf('.') > 0) {
timesCount2 = valueString.split('.')[1].length;
}
var maxtimeCount = timesCount1 > timesCount2 ? timesCount1 : timesCount2;
this.value = (Math.pow(10, maxtimeCount) * this.value * Math.pow(10, maxtimeCount) * value) / Math.pow(10, maxtimeCount * 2);
return this;
}
if (window.JSMath == undefined) {
window.JSMath = function(value) {
var result = new JSMath();
result.from(value);
return result;
}
}
})()
B1.基本运算
0.1+0.2
=> JSMath(0.1).add(0.2).value == 0.3
7+0.8
=> JSMath(7).times(0.8).value == 5.6
5.6/7
=> JSMath(5.6).div(7).value = 0.8
B2.多目运算
0.05 + 0.05 + 0.2
=> JSMath(JSMath(0.05).add(0.05)).add(0.2).value == 0.3
(5+0.6)/7
=> JSMath(JSMath(5).add(0.6)).div(7).value == 0.8
三、小结
以上是目前已知的几种解决方案。尚不清楚是否有更可靠的开源三方库能彻底解决此问题。以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本站。