美文网首页微信小程序
js,Ts 数字的精度计算

js,Ts 数字的精度计算

作者: Morbid_D | 来源:发表于2020-12-22 13:25 被阅读0次

    前端计算对后端返回的数据进行计算时,就会出现1.333333333类似的数据,这是因为js的精度计算并没有达到一定的要求。因为自己也出现了这样的问题,所以去寻找了一下答案,并且进行了实际的测试。
    找到的方法:
    1.math.js
    该js的下载地址 https://cdnjs.cloudflare.com/ajax/libs/mathjs/3.16.0/math.min.js
    2.number-precision(建议使用么,本人使用) 内存小,可以和math.js和BigDecimal.js媲美
    下载地址 https://github.com/nefe/number-precision/blob/master/build/index.js
    使用方法

    image.png

    若网速有问题,可以复制一下代码,这里是js的写法

    'use strict';

    Object.defineProperty(exports, '__esModule', { value: true });

    /*! *****************************************************************************
    Copyright (c) Microsoft Corporation.
    Permission to use, copy, modify, and/or distribute this software for any
    purpose with or without fee is hereby granted.
    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
    REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
    AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
    INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
    LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
    OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
    PERFORMANCE OF THIS SOFTWARE.
    user ,medthor
    NP.strip(num) // strip a number to nearest right number
    NP.plus(num1, num2, num3, ...) // addition, num + num2 + num3, two numbers is required at least.
    NP.minus(num1, num2, num3, ...) // subtraction, num1 - num2 - num3
    NP.times(num1, num2, num3, ...) // multiplication, num1 * num2 * num3
    NP.divide(num1, num2, num3, ...) // division, num1 / num2 / num3
    NP.round(num, ratio) // round a number based on ratio
    ***************************************************************************** /
    /
    global Reflect, Promise */

    function __spreadArrays() {
    for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
    for (var r = Array(s), k = 0, i = 0; i < il; i++)
    for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
    r[k] = a[j];
    return r;
    }

    /**

    • @desc 解决浮动运算问题,避免小数点后产生多位数和计算精度损失。
    • 问题示例:2.3 + 2.4 = 4.699999999999999,1.0 - 0.9 = 0.09999999999999998
      /
      /
      *
    • 把错误的数据转正
    • strip(0.09999999999999998)=0.1
      /
      function strip(num, precision) {
      if (precision === void 0) { precision = 15; }
      return +parseFloat(Number(num).toPrecision(precision));
      }
      /
      *
    • Return digits length of a number
    • @param {number} num Input number
      /
      function digitLength(num) {
      // Get digit length of e
      var eSplit = num.toString().split(/[eE]/);
      var len = (eSplit[0].split('.')[1] || '').length - +(eSplit[1] || 0);
      return len > 0 ? len : 0;
      }
      /
    • 把小数转成整数,支持科学计数法。如果是小数则放大成整数
    • @param {number} num 输入数
      /
      function float2Fixed(num) {
      if (num.toString().indexOf('e') === -1) {
      return Number(num.toString().replace('.', ''));
      }
      var dLen = digitLength(num);
      return dLen > 0 ? strip(Number(num) * Math.pow(10, dLen)) : Number(num);
      }
      /
    • 检测数字是否越界,如果越界给出提示
    • @param {number} num 输入数
      /
      function checkBoundary(num) {
      if (_boundaryCheckingState) {
      if (num > Number.MAX_SAFE_INTEGER || num < Number.MIN_SAFE_INTEGER) {
      console.warn(num + " is beyond boundary when transfer to integer, the results may not be accurate");
      }
      }
      }
      /
    • 精确乘法
      /
      function times(num1, num2) {
      var others = [];
      for (var _i = 2; _i < arguments.length; _i++) {
      others[_i - 2] = arguments[_i];
      }
      if (others.length > 0) {
      return times.apply(void 0, __spreadArrays([times(num1, num2), others[0]], others.slice(1)));
      }
      var num1Changed = float2Fixed(num1);
      var num2Changed = float2Fixed(num2);
      var baseNum = digitLength(num1) + digitLength(num2);
      var leftValue = num1Changed * num2Changed;
      checkBoundary(leftValue);
      return leftValue / Math.pow(10, baseNum);
      }
      /
      *
    • 精确加法
      /
      function plus(num1, num2) {
      var others = [];
      for (var _i = 2; _i < arguments.length; _i++) {
      others[_i - 2] = arguments[_i];
      }
      if (others.length > 0) {
      return plus.apply(void 0, __spreadArrays([plus(num1, num2), others[0]], others.slice(1)));
      }
      var baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2)));
      return (times(num1, baseNum) + times(num2, baseNum)) / baseNum;
      }
      /
      *
    • 精确减法
      /
      function minus(num1, num2) {
      var others = [];
      for (var _i = 2; _i < arguments.length; _i++) {
      others[_i - 2] = arguments[_i];
      }
      if (others.length > 0) {
      return minus.apply(void 0, __spreadArrays([minus(num1, num2), others[0]], others.slice(1)));
      }
      var baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2)));
      return (times(num1, baseNum) - times(num2, baseNum)) / baseNum;
      }
      /
      *
    • 精确除法
      /
      function divide(num1, num2) {
      var others = [];
      for (var _i = 2; _i < arguments.length; _i++) {
      others[_i - 2] = arguments[_i];
      }
      if (others.length > 0) {
      return divide.apply(void 0, __spreadArrays([divide(num1, num2), others[0]], others.slice(1)));
      }
      var num1Changed = float2Fixed(num1);
      var num2Changed = float2Fixed(num2);
      checkBoundary(num1Changed);
      checkBoundary(num2Changed);
      // fix: 类似 10 ** -4 为 0.00009999999999999999,strip 修正
      return times(num1Changed / num2Changed, strip(Math.pow(10, digitLength(num2) - digitLength(num1))));
      }
      /
      *
    • 四舍五入
      /
      function round(num, ratio) {
      var base = Math.pow(10, ratio);
      return divide(Math.round(times(num, base)), base);
      }
      var _boundaryCheckingState = true;
      /
      *
    • 是否进行边界检查,默认开启
    • @param flag 标记开关,true 为开启,false 为关闭,默认为 true
      */
      function enableBoundaryChecking(flag) {
      if (flag === void 0) { flag = true; }
      _boundaryCheckingState = flag;
      }
      var index = {
      strip: strip,
      plus: plus,
      minus: minus,
      times: times,
      divide: divide,
      round: round,
      digitLength: digitLength,
      float2Fixed: float2Fixed,
      enableBoundaryChecking: enableBoundaryChecking,
      };

    exports.strip = strip;
    exports.plus = plus;
    exports.minus = minus;
    exports.times = times;
    exports.divide = divide;
    exports.round = round;
    exports.digitLength = digitLength;
    exports.float2Fixed = float2Fixed;
    exports.enableBoundaryChecking = enableBoundaryChecking;
    exports['default'] = index;

    《==================这里是ts的写法=========================》
    'use strict';

    export interface NP {
    strip: (num: number, precision?: number) => number;
    plus: (...nums: number[]) => number;
    minus: (...nums: number[]) => number;
    times: (...nums: number[]) => number;
    divide: (...nums: number[]) => number;
    round: (num: number, ratio: number) => number;
    digitLength: (num: number) => number;
    float2Fixed: (num: number) => number;
    enableBoundaryChecking: (flag?: boolean) => void;
    }

    /**

    • 解决浮动运算问题,避免小数点后产生多位数和计算精度损失。
    • 问题示例:2.3 + 2.4 = 4.699999999999999,1.0 - 0.9 = 0.09999999999999998
      */

    /**

    • 把错误的数据转正
    • strip(0.09999999999999998)=0.1
      */
      const strip = (num: number, precision: number = 15): number => {
      if (num === undefined || num === null) {
      throw new Error('Invalid number input');
      }
      return +parseFloat(Number(num).toPrecision(precision));
      };

    /**

    • Return digits length of a number
    • @param {number} num Input number
      */
      const digitLength = (num: number): number => {
      if (num === undefined || num === null) {
      throw new Error('Invalid number input');
      }
      const eSplit = num.toString().split(/[eE]/);
      const len = (eSplit[0].split('.')[1] || '').length - +(eSplit[1] || 0);
      return len > 0 ? len : 0;
      };

    /**

    • 把小数转成整数,支持科学计数法。如果是小数则放大成整数
    • @param {number} num 输入数
      */
      const float2Fixed = (num: number): number => {
      if (num === undefined || num === null) {
      throw new Error('Invalid number input');
      }
      if (num.toString().indexOf('e') === -1) {
      return Number(num.toString().replace('.', ''));
      }
      const dLen = digitLength(num);
      return dLen > 0 ? strip(Number(num) * Math.pow(10, dLen)) : Number(num);
      };

    /**

    • 检测数字是否越界,如果越界给出提示
    • @param {number} num 输入数
      */
      const checkBoundary = (num: number): void => {
      if (_boundaryCheckingState) {
      if (num > Number.MAX_SAFE_INTEGER || num < Number.MIN_SAFE_INTEGER) {
      console.warn(${num} is beyond boundary when transfer to integer, the results may not be accurate);
      }
      }
      };

    /**

    • 精确乘法
      */
      const times = (num1: number, num2: number, ...others: number[]): number => {
      if (num1 === undefined || num2 === undefined) {
      throw new Error('Invalid number input');
      }
      if (others.length > 0) {
      return times(times(num1, num2), others[0], ...others.slice(1));
      }
      const num1Changed = float2Fixed(num1);
      const num2Changed = float2Fixed(num2);
      const baseNum = digitLength(num1) + digitLength(num2);
      const leftValue = num1Changed * num2Changed;
      checkBoundary(leftValue);
      return leftValue / Math.pow(10, baseNum);
      };

    /**

    • 精确加法
      */
      const plus = (num1: number, num2: number, ...others: number[]): number => {
      if (num1 === undefined || num2 === undefined) {
      throw new Error('Invalid number input');
      }
      if (others.length > 0) {
      return plus(plus(num1, num2), others[0], ...others.slice(1));
      }
      const baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2)));
      return (times(num1, baseNum) + times(num2, baseNum)) / baseNum;
      };

    /**

    • 精确减法
      */
      const minus = (num1: number, num2: number, ...others: number[]): number => {
      if (num1 === undefined || num2 === undefined) {
      throw new Error('Invalid number input');
      }
      if (others.length > 0) {
      return minus(minus(num1, num2), others[0], ...others.slice(1));
      }
      const baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2)));
      return (times(num1, baseNum) - times(num2, baseNum)) / baseNum;
      };

    /**

    • 精确除法
      */
      const divide = (num1: number, num2: number, ...others: number[]): number => {
      if (num1 === undefined || num2 === undefined) {
      throw new Error('Invalid number input');
      }
      if (others.length > 0) {
      return divide(divide(num1, num2), others[0], ...others.slice(1));
      }
      const num1Changed = float2Fixed(num1);
      const num2Changed = float2Fixed(num2);
      checkBoundary(num1Changed);
      checkBoundary(num2Changed);
      // fix: 类似 10 ** -4 为 0.00009999999999999999,strip 修正
      return times(num1Changed / num2Changed, strip(Math.pow(10, digitLength(num2) - digitLength(num1))));
      };

    /**

    • 四舍五入
      */
      const round = (num: number, ratio: number): number => {
      if (num === undefined || ratio === undefined) {
      throw new Error('Invalid number input');
      }
      const base = Math.pow(10, ratio);
      return divide(Math.round(times(num, base)), base);
      };

    let _boundaryCheckingState = true;

    /**

    • 是否进行边界检查,默认开启
    • @param flag 标记开关,true 为开启,false 为关闭,默认为 true
      */
      const enableBoundaryChecking = (flag: boolean = true): void => {
      _boundaryCheckingState = flag;
      };

    const NP: NP = {
    strip,
    plus,
    minus,
    times,
    divide,
    round,
    digitLength,
    float2Fixed,
    enableBoundaryChecking
    };

    export default NP;

    相关文章

      网友评论

        本文标题:js,Ts 数字的精度计算

        本文链接:https://www.haomeiwen.com/subject/twbynktx.html