當前位置:首頁 » 操作系統 » 源碼精確度

源碼精確度

發布時間: 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 06:13:18 瀏覽:181
天翼智能路由器的初始密碼是多少 發布:2025-03-11 06:10:17 瀏覽:914
安卓機怎麼領嶺南通 發布:2025-03-11 05:56:54 瀏覽:132
求生之路2虐電腦伺服器 發布:2025-03-11 05:35:40 瀏覽:632
編譯學堂 發布:2025-03-11 05:31:06 瀏覽:185
蘋果文件夾隱藏 發布:2025-03-11 05:26:42 瀏覽:546
簡訊設置密碼如何關閉 發布:2025-03-11 05:26:39 瀏覽:915
re管理器主文件夾 發布:2025-03-11 05:26:37 瀏覽:714
手機優酷緩存在哪 發布:2025-03-11 05:25:58 瀏覽:434
摩擦引流腳本 發布:2025-03-11 05:17:31 瀏覽:545