/**
* floatObj 包含加減乘除四個方法,能確保浮點數運算不丟失精度
*
* 我們知道計算機編程語言里浮點數計算會存在精度丟失問題(或稱舍入誤差),其根本原因是二進制和實現位數限制有些數無法有限表示
* 以下是十進制小數對應的二進制表示
* 0.1 >> 0.0001 1001 1001 1001…(1001無限循環)
* 0.2 >> 0.0011 0011 0011 0011…(0011無限循環)
* 計算機里每種數據類型的存儲是一個有限寬度,比如 JavaScript 使用 64 位存儲數字類型,因此超出的會舍去。舍去的部分就是精度丟失的部分。
*
* ** method **
* add / subtract / multiply /divide
*
* ** explame **
* 0.1 + 0.2 == 0.30000000000000004 (多了 0.00000000000004)
* 0.2 + 0.4 == 0.6000000000000001 (多了 0.0000000000001)
* 19.9 * 100 == 1989.9999999999998 (少了 0.0000000000002)
*
* floatObj.add(0.1, 0.2) >> 0.3
* floatObj.multiply(19.9, 100) >> 1990
*
*/
var floatObj = function() {
/*
* 判斷obj是否為一個整數
*/
function isInteger(obj) {
return Math.floor(obj) === obj;
}
/*
* 將一個浮點數轉成整數,返回整數和倍數。如 3.14 >> 314,倍數是 100
* @param floatNum {number} 小數
* @return {object}
* {times:100, num: 314}
*/
function toInteger(floatNum) {
var ret = {times: 1, num: 0};
if (isInteger(floatNum)) {
ret.num = floatNum;
return ret;
}
var strfi = floatNum + '';
var dotPos = strfi.indexOf('.');
var len = strfi.substr(dotPos+1).length;
var times = Math.pow(10, len);
//var intNum = parseInt(floatNum * times + 0.5, 10); //測試負數會有精度丟失
var intNum = parseInt(floatNum * times , 10);
ret.times = times;
ret.num = intNum;
return ret;
}
/*
* 核心方法,實現加減乘除運算,確保不丟失精度
* 思路:把小數放大為整數(乘),進行算術運算,再縮小為小數(除)
*
* @param a {number} 運算數1
* @param b {number} 運算數2
* @param op {string} 運算類型,有加減乘除(add/subtract/multiply/divide)
*
*/
function operation(a, b, op) {
var o1 = toInteger(a);
var o2 = toInteger(b);
var n1 = o1.num;
var n2 = o2.num;
var t1 = o1.times;
var t2 = o2.times;
var max = t1 > t2 ? t1 : t2;
var result = null;
switch (op) {
case 'add':
if (t1 === t2) { // 兩個小數位數相同
result = n1 + n2;
} else if (t1 > t2) { // o1 小數位 大於 o2
result = n1 + n2 * (t1 / t2);
} else { // o1 小數位 小於 o2
result = n1 * (t2 / t1) + n2;
}
return result / max;
case 'subtract':
if (t1 === t2) {
result = n1 - n2;
} else if (t1 > t2) {
result = n1 - n2 * (t1 / t2);
} else {
result = n1 * (t2 / t1) - n2;
}
return result / max;
case 'multiply':
result = (n1 * n2) / (t1 * t2);
return result
case 'divide':
result = (n1 / n2) * (t2 / t1);
return result;
}
}
// 加減乘除的四個接口
function add(a, b) {
return operation(a, b, 'add');
}
function subtract(a, b) {
return operation(a, b, 'subtract');
}
function multiply(a, b) {
return operation(a, b, 'multiply');
}
function divide(a, b) {
return operation(a, b, 'divide');
}
// 如果是整數,就不會保留小數點,只保留小數點超過指定位數的情況
function toFixed(num, digits) {
var times = Math.pow(10, digits);
var des = num * times;
return parseInt(des, 10) / times;
}
// exports
return {
add: add,
subtract: subtract,
multiply: multiply,
divide: divide,
toFixed: toFixed
}
}();
另一種方案可參考:
https://github.com/MikeMcl/big.js