解决浮点型数值计算精度丢失的一种实现

1.包装一个类拥有原数值，转换后的整数以及10的倍数
2.传入一个数字进行初始化
3.编写四则运算函数，每次运算把运算后的结果，结果的整数以及10的倍数保存起来，并返回此对象用于流式计算

``````(（0.2 * 1000) X （0.001 * 1000））/ (1000 * 1000)
``````

`没有处理无效值（空，非数字等）以及没有控制除不尽时的精度`
`未对四则运算进行覆盖测试，仅提供思路`

``````class FNumber {
val//原始的值
#FLOATVAL = 0;//私有属性
#FPOW = 0;;//变成整数的位数（任何数次方0都为1）
constructor(val) {
this.val = val
let fArray = this.#isFloat(val)
this.#FLOATVAL = fArray[ 0 ]
this.#FPOW = fArray[ 1 ]
}
let fnum = this.#COMPAREPOW(num)
let result = this.#FLOATVAL + fnum
this.#FLOATVAL = result
this.val = result / Math.pow(10, this.#FPOW)
return this
}
subtract (num) {
let fnum = this.#COMPAREPOW(num)
let result = this.#FLOATVAL - fnum
this.#FLOATVAL = result
this.val = result / Math.pow(10, this.#FPOW)
return this
}
multiply (num) {
let fnum = this.#COMPAREPOW(num)
let result = this.#FLOATVAL * fnum
this.#FLOATVAL = result
let fpow = Math.pow(10, this.#FPOW)
this.val = result / Math.pow(fpow, 2)
this.#FPOW = this.#FPOW * 2
return this
}
divide (num) {
let fnum = this.#COMPAREPOW(num)
let result = this.#FLOATVAL / fnum
this.val = result//除法会消去同项
let fArray = this.#isFloat(result)//计算出转换后的小数和次方数
this.#FLOATVAL = fArray[ 0 ]
this.#FPOW = fArray[ 1 ]

return this
}
#COMPAREPOW (num) {
let fArray = this.#isFloat(num)//计算出转换后的小数和次方数

if (this.#FPOW < fArray[ 1 ]) {
//如果当前转换为整数的位数小于要计算的数，那么把当前数变成与当前数一样的扩展
this.#FLOATVAL = this.#FLOATVAL * Math.pow(10, fArray[ 1 ] - this.#FPOW)
this.#FPOW = fArray[ 1 ]
} else if (this.#FPOW >= fArray[ 1 ]) {
//反过来就把操作数变成和当前数一样的扩展
fArray[ 0 ] = fArray[ 0 ] * Math.pow(10, this.#FPOW - fArray[ 1 ])
}
return fArray[ 0 ]
}

/**
* @return [{转换后的整数},{转为整数要乘以10的次幂}]
* @param {要转换为整数的数} num
*/
#isFloat (num) {
let str = num.toString()
let index = str.indexOf(".")
if (str.indexOf(".") > 0) {
//小数
let fpow = str.length - index - 1
return [ num * Math.pow(10, fpow), fpow ]
}
return [ num, 0 ]
}
}
``````
test