当前位置: 首页 > news >正文

js浮点数四则运算精度丢失以及toFixed()精度丢失解决方法

js浮点数四则运算精度丢失以及tofixed精度丢失解决方法

    • 一、js浮点数计算精度丢失的一些例子
      • 1、四则运算精度丢失:
      • 2、toFixed() 四舍五入精度丢失:
    • 二、浮点数计算精度丢失的原因
    • 三、解决办法
      • 1、使用 big.js(如果有大量连续的计算推荐使用)
      • 2、解决四则运算精度丢失问题
        • 方法1:没有具体要求保留几位小数的,最简单的方法是直接用 `toFixed()`
        • 方法2:有具体要求精确到第几位,用`科学计数法`对运算结果进行四舍五入
          • 实现原理:
          • 使用方法:
      • 3、解决 toFixed() 精度丢失问题:重写 toFixed 方法(重点!!!!)
    • 四、判断小数是否相等
    • 五、其他由浮点数引起的问题

一、js浮点数计算精度丢失的一些例子

1、四则运算精度丢失:

0.1+0.2 = 0.30000000000000004 0.3 - 0.2 = 0.0999999999999999810.22*100 = 1022.00000000000012.4/0.8 = 2.999999999999999632.2*100 = 3220.000000000000532.2*1000 = 32200.000000000004(32.2*100 + 3.14*100) / 100 = 35.34 // 这里的精度怎么又不丢失了?32.3*100 = 3229.999999999999532.3*1000 = 32299.999999999996...

2、toFixed() 四舍五入精度丢失:

(1.335).toFixed(2); // '1.33'
(6.265).toFixed(2); // '6.26'

二、浮点数计算精度丢失的原因

js采用64位浮点数表示法(几乎所有现代编程语言所采用),这是一种二进制表示法。二进制浮点数表示法并不能精确表示类似 0.1 这样简单的数字。

这个问题不只在js中才会出现,在任何使用二进制浮点数的编程语言中都会出现。

JavaScript的未来版本或许会支持十进制数字类型以避免精度丢失的问题。

三、解决办法

1、使用 big.js(如果有大量连续的计算推荐使用)

  • 既解决了浮点数计算精度丢失问题,又解决了 toFixed() 四舍五入精度丢失问题。
  • big.jsbig.js, bignumber.js, decimal.js 三姐妹中功能最少的,但也是体积最小的,压缩版只有3k,对于处理js精度丢失已经足够用了。
  import Big from 'big.js'// 运算const plus = Big(0.1).plus(0.2); // 加const minus = Big(0.3).minus(0.1); // 减const mul = Big(10.22).times(100); // 乘const div = Big(2.4).div(0.8); // 除// toFixedconst fixed = new Big(6.265).toFixed(2); // 6.27console.log(plus.toNumber(),minus.toNumber(),mul.toNumber(),div.toNumber())// 0.3 0.2 1022 3

2、解决四则运算精度丢失问题

方法1:没有具体要求保留几位小数的,最简单的方法是直接用 toFixed()

从上面四则运算精度丢失的例子可以看到,四则运算的精度丢失主要会出现很多位 0 或很多位 9。

function precision(val) {return +val.toFixed(8);
}precision(0.1 + 0.2)

方法2:有具体要求精确到第几位,用科学计数法对运算结果进行四舍五入

MDN 已经给出了具体代码(也是利用“科学计数法”扩大 10 的 n 次不会出现精度丢失的特性):

function round(number, precision) {return Math.round(+number + 'e' + precision) / Math.pow(10, precision);
}round(1.005, 2);    //1.01
round(1.002, 2);    //1

或者:

/*** Decimal adjustment of a number.** @param {String}  type  The type of adjustment.* @param {Number}  value The number.* @param {Integer} exp   The exponent (the 10 logarithm of the adjustment base).* @returns {Number}      The adjusted value.*/
function decimalAdjust(type, value, exp) {// If the exp is undefined or zero...if (typeof exp === 'undefined' || +exp === 0) {return Math[type](value);}value = +value;exp = +exp;// If the value is not a number or the exp is not an integer...if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0)) {return NaN;}// Shiftvalue = value.toString().split('e');value = Math[type](+(value[0] + 'e' + (value[1] ? +value[1] - exp : -exp)));// Shift backvalue = value.toString().split('e');value = +(value[0] + 'e' + (value[1] ? +value[1] + exp : exp));return value;
}export default {round: (value, exp) => {return decimalAdjust('round', value, exp);},floor: (value, exp) => {return decimalAdjust('floor', value, exp);},ceil: (value, exp) => {return decimalAdjust('ceil', value, exp);}
};
实现原理:
比如 1022.0000000000001 要保留2位小数,先用 e2 把这个数扩大 100 倍,
再用 Math.round(), Math.floor(), Math.ceil() 取整,然后再用 e-2 缩小回来。
使用方法:
Decimal.round(val, precision)console.log(Decimal.round(1.13265, -3))  //1.133
console.log(Decimal.round(3.17, -3))  //3.17
console.log(Decimal.round(0.1+0.2, -3))  //0.3
console.log(Decimal.round(3.17))  //3
console.log(Decimal.round(3.17, 0))  //3
console.log(Decimal.round(31216, 1))  //31220
console.log(Decimal.round(31213, 2))  //31200

precision 可选值:不传,0,负数,正数。

  • 不传、0: 精确到整数。
  • 正数: 1就是个位为0,十位是个位四舍五入的值。
  • 负数: 精确到小数点后几位

3、解决 toFixed() 精度丢失问题:重写 toFixed 方法(重点!!!!)

function toFixed(number, precision = 2) {number = Math.round(+number + 'e' + precision) / Math.pow(10, precision) + '';let s = number.split('.');if ((s[1] || '').length < precision) {s[1] = s[1] || '';s[1] += new Array(precision - s[1].length).fill('0').join('');}return s.join('.');
}toFixed(6) // '6.00'

四、判断小数是否相等

function epsEqu(x,y) {  return Math.abs(x - y) < Math.pow(2, -52);// 因为 Number.EPSILON === Math.pow(2, -52),所以也可以这么写:// return Math.abs(x - y) < Number.EPSILON;
}
// 举例
0.1 + 0.2 === 0.3 // false
epsEqu(0.1 + 0.2, 0.3) // true
小数比较时,要给它一个误差范围,在误差范围内的都算相等。

五、其他由浮点数引起的问题

parseInt(0.0000008) // -> 8
http://www.lryc.cn/news/5620.html

相关文章:

  • 高姿态下的面部表情识别系统
  • English Learning - Day59 作业打卡 2023.2.13 周一
  • 图机器学习
  • ArcGIS中ArcMap创建渔网Create Fishnet:生成指定大小的格网矢量文件
  • TensorRT中的自定义层
  • 部署智能合约到公链
  • Windows server——部署DNS服务(3)
  • 9. QML_OpenGL--2. 在QQuick中搭建加载OpenGL框架
  • 亚马逊云科技携手滴普科技,打造数据智能新标杆
  • CGO 跨平台静态编译
  • 股票买卖接口怎么来的?
  • 【Python学习笔记】29.Python3 面向对象
  • MySQL 索引
  • 学会使用LoadRunner录制脚本
  • 产品经理必看的高效产品文档撰写指南
  • Prometheus 的介绍和安装
  • ViewModel快速上手1-原生kotlin
  • Flutter(一)介绍、Dart语言简介
  • 【数据结构】---顺序表的实现
  • JavaScript刷LeetCode拿offer-经典高频40题vaScript刷LeetCode拿offer-经典高频40题
  • 动态规划,这将是你见过最详细的讲解
  • 【服务器数据恢复】FreeNAS层UFS2文件系统数据恢复案例
  • Zookeeper安装和基本使用
  • 字节面试惨败,闭关修炼再战美团(Android 面经~)
  • 【机器学习实战】七、梯度下降
  • 什么是极速文件传输,极速文件传输如何进行大文件传输
  • Spring Boot 日志
  • 好用的研发管理看板工具有哪些?10款主流看板管理软件盘点
  • 【软考系统架构设计师】2022下案例分析历年真题
  • Java skill - @JsonAlias 和 @JsonProperty