当前位置:首页 » 操作系统 » 源码精确度

源码精确度

发布时间: 2023-08-12 21:39:25

㈠ number-precision 实现js高精度运算 源码

type numType = number | string;

/**

* @desc 解决浮动运算问题,避免小数点后产生多位数和计算精度损失。

* 问题示例:2.3 + 2.4 = 4.699999999999999,1.0 - 0.9 = 0.09999999999999998

*/

/**

* 把错误的数据转正

* strip(0.09999999999999998)=0.1

*/

function strip(num: numType, precision = 15): number {

  return +parseFloat(Number(num).toPrecision(precision));

}

/**

* Return digits length of a number

* @param {*number} num Input number

*/

function digitLength(num: numType): number {

  // Get digit length of e

  const eSplit = num.toString().split(/[eE]/);

  const len = (eSplit[0].split('.')[1] || '').length - +(eSplit[1] || 0);

  return len > 0 ? len : 0;

}

/**

* 把小数转成整数,支持科学计数法。如果是小数则放大成整数

* @param {*number} num 输入数

*/

function float2Fixed(num: numType): number {

  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 输入数

*/

function checkBoundary(num: number) {

  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: numType, num2: numType, ...others: numType[]): number {

  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);

}

/**

* 精确加法

*/

function plus(num1: numType, num2: numType, ...others: numType[]): number {

  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;

}

/**

* 精确减法

*/

function minus(num1: numType, num2: numType, ...others: numType[]): number {

  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;

}

/**

* 精确除法

*/

function divide(num1: numType, num2: numType, ...others: numType[]): number {

  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))));

}

/**

* 四舍五入

*/

function round(num: numType, ratio: number): number {

  const base = Math.pow(10, ratio);

  return divide(Math.round(times(num, base)), base);

}

let _boundaryCheckingState = true;

/**

* 是否进行边界检查,默认开启

* @param flag 标记开关,true 为开启,false 为关闭,默认为 true

*/

// 这里可以设置边界检查(默认是true)

function enableBoundaryChecking(flag = true) {

  _boundaryCheckingState = flag;

}

// 输出上面的方法

export { strip, plus, minus, times, divide, round, digitLength, float2Fixed, enableBoundaryChecking };

export default {

  strip,

  plus,

  minus,

  times,

  divide,

  round,

  digitLength,

  float2Fixed,

  enableBoundaryChecking,

};

㈡ (二分法)C语言程序

1、打开Python开发工具IDLE,新建‘search.py’。

热点内容
不懂加工怎么看数控车床配置 发布:2025-03-11 02:54:33 浏览:596
埋点系统存储方案 发布:2025-03-11 02:41:20 浏览:442
编程要很久 发布:2025-03-11 02:41:10 浏览:195
笔记本电脑播放mp4时提醒服务器运行失败 发布:2025-03-11 02:40:32 浏览:440
吉利星瑞尊贵版配置有哪些 发布:2025-03-11 02:34:33 浏览:889
ecs中怎么配置slb 发布:2025-03-11 02:33:17 浏览:719
vb图片保存到数据库 发布:2025-03-11 02:31:05 浏览:842
元件符号编译器 发布:2025-03-11 02:30:12 浏览:73
位交换算法 发布:2025-03-11 01:57:41 浏览:342
网游跟上传 发布:2025-03-11 01:46:07 浏览:62