type numType = number | string ; param { * number function digitLength ( num ) { const eSplit = num . toString ( ) . split ( /[eE]/ ) ; const len = ( eSplit [ 0 ] . split ( '.' ) [ 1 ] || '' ) . length - + ( eSplit [ 1 ] || 0 ) ; return len > 0 ? len : 0 ; } let _boundaryCheckingState = true ; param flag function enableBoundaryChecking ( flag = true ) { _boundaryCheckingState = flag ; } param { * number 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 iteratorOperation ( arr , operation ) { const [ num1 , num2 , ... others ] = arr ; let res = operation ( num1 , num2 ) ; others . forEach ( num => { res = operation ( res , num ) ; } ) ; return res ; } /**
 * 把错误的数据转正
 * strip(0.09999999999999998)=0.1
 */ function strip ( num , precision = 15 ) { return + parseFloat ( Number ( num ) . toPrecision ( precision ) ) ; } param { * number function float2Fixed ( num ) { 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 ) ; } /**
 * 精确乘法
 */ function times ( ... nums ) { if ( nums . length > 2 ) { return iteratorOperation ( nums , times ) ; } const [ num1 , num2 ] = nums ; 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 ( ... nums ) { if ( nums . length > 2 ) { return iteratorOperation ( nums , plus ) ; } const [ num1 , num2 ] = nums ; const baseNum = Math . pow ( 10 , Math . max ( digitLength ( num1 ) , digitLength ( num2 ) ) ) ; return ( times ( num1 , baseNum ) + times ( num2 , baseNum ) ) / baseNum ; } export { plus , times } ;