@ -78,7 +78,9 @@ var MAX = 1E9, // 0 to 1e+9
outOfRange ,
id = 0 ,
isValid = /^-?(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i ,
trim = String . prototype . trim || function ( ) { return this . replace ( /^\s+|\s+$/g , '' ) } ,
trim = String . prototype . trim || function ( ) {
return this . replace ( /^\s+|\s+$/g , '' )
} ,
ONE = BigNumber ( 1 ) ;
@ -238,8 +240,7 @@ function BigNumber( n, b ) {
}
// Determine leading zeros.
for ( i = 0 ; n . charAt ( i ) == '0' ; i ++ ) {
}
for ( i = 0 ; n . charAt ( i ) == '0' ; i ++ ) { }
b = n . length ;
@ -265,15 +266,13 @@ function BigNumber( n, b ) {
} else {
// Determine trailing zeros.
for ( ; n . charAt ( -- b ) == '0' ; ) {
}
for ( ; n . charAt ( -- b ) == '0' ; ) { }
x [ 'e' ] = e ;
x [ 'c' ] = [ ] ;
// Convert string to array of digits (without leading and trailing zeros).
for ( e = 0 ; i <= b ; x [ 'c' ] [ e ++ ] = + n . charAt ( i ++ ) ) {
}
for ( e = 0 ; i <= b ; x [ 'c' ] [ e ++ ] = + n . charAt ( i ++ ) ) { }
}
}
@ -298,16 +297,15 @@ BigNumber['fromBuffer'] = function (buf, opts) {
if ( ! opts ) opts = { } ;
var endian = { 1 : 'big' , '-1' : 'little' } [ opts . endian ]
|| opts . endian || 'big'
;
var endian = {
1 : 'big' ,
'-1' : 'little'
} [ opts . endian ] || opts . endian || 'big' ;
var size = opts . size === 'auto' ? Math . ceil ( buf . length ) : ( opts . size || 1 ) ;
if ( buf . length % size !== 0 ) {
throw new RangeError ( 'Buffer length (' + buf . length + ')'
+ ' must be a multiple of size (' + size + ')'
) ;
throw new RangeError ( 'Buffer length (' + buf . length + ')' + ' must be a multiple of size (' + size + ')' ) ;
}
var hex = [ ] ;
@ -356,9 +354,11 @@ BigNumber['config'] = function () {
return ! ( ( outOfRange = n < lo || n > hi ) ||
parse ( n ) != n && n !== 0 ) ;
} ,
has = o && typeof o == 'object'
? function ( ) { if ( o . hasOwnProperty ( p ) ) return ( v = o [ p ] ) != null }
: function ( ) { if ( a . length > i ) return ( v = a [ i ++ ] ) != null } ;
has = o && typeof o == 'object' ? function ( ) {
if ( o . hasOwnProperty ( p ) ) return ( v = o [ p ] ) != null
} : function ( ) {
if ( a . length > i ) return ( v = a [ i ++ ] ) != null
} ;
// [DECIMAL_PLACES] {number} Integer, 0 to MAX inclusive.
if ( has ( p = 'DECIMAL_PLACES' ) ) {
@ -433,9 +433,7 @@ BigNumber['config'] = function () {
if ( has ( p = 'ERRORS' ) ) {
if ( v === ! ! v || v === 1 || v === 0 ) {
parse = ( outOfRange = id = 0 , ERRORS = ! ! v )
? parseInt
: parseFloat ;
parse = ( outOfRange = id = 0 , ERRORS = ! ! v ) ? parseInt : parseFloat ;
} else {
// 'config() ERRORS not a boolean or binary digit: {v}'
@ -466,14 +464,9 @@ function ifExceptionsThrow( arg, i, j, isArray, isRange, isErrors) {
method + ' number type has more than 15 significant digits' ,
method + ' not a base ' + j + ' number' ,
method + ' base' + message ,
method + ' not a number' ] [ i ] ||
j + '() ' + i + ( isErrors
? ' not a boolean or binary digit'
: message + ( isArray
? ' or not [' + ( outOfRange
? ' negative, positive'
: ' integer, integer' ) + ' ]'
: '' ) ) ) + ': ' + arg ;
method + ' not a number'
] [ i ] ||
j + '() ' + i + ( isErrors ? ' not a boolean or binary digit' : message + ( isArray ? ' or not [' + ( outOfRange ? ' negative, positive' : ' integer, integer' ) + ' ]' : '' ) ) ) + ': ' + arg ;
outOfRange = id = 0 ;
error = new Error ( message ) ;
@ -502,12 +495,9 @@ function convert( nStr, baseOut, baseIn, sign ) {
for ( bIn = bIn || baseIn ; i < strL ; i ++ ) {
for ( arrL = arr . length , j = 0 ; j < arrL ; arr [ j ] *= bIn , j ++ ) {
}
for ( arrL = arr . length , j = 0 ; j < arrL ; arr [ j ] *= bIn , j ++ ) { }
for ( arr [ 0 ] += DIGITS . indexOf ( str . charAt ( i ) ) , j = 0 ;
j < arr . length ;
j ++ ) {
for ( arr [ 0 ] += DIGITS . indexOf ( str . charAt ( i ) ) , j = 0 ; j < arr . length ; j ++ ) {
if ( arr [ j ] > baseOut - 1 ) {
@ -530,8 +520,7 @@ function convert( nStr, baseOut, baseIn, sign ) {
arrL = arr . length ,
str = '' ;
for ( ; i < arrL ; str += DIGITS . charAt ( arr [ i ++ ] ) ) {
}
for ( ; i < arrL ; str += DIGITS . charAt ( arr [ i ++ ] ) ) { }
return str ;
}
@ -576,8 +565,7 @@ function convert( nStr, baseOut, baseIn, sign ) {
if ( e = fracBN [ 'e' ] ) {
// Append zeros according to the exponent of the result.
for ( ; ++ e ; fracArr . unshift ( 0 ) ) {
}
for ( ; ++ e ; fracArr . unshift ( 0 ) ) { }
// Append the fraction part to the converted integer part.
nStr = arrToStr ( nArr ) + '.' + arrToStr ( fracArr ) ;
@ -625,8 +613,7 @@ function divide( dvd, dvs, exp, s, base, isOdd ) {
s = dig < 0 ? 0 : dig ;
// Add zeros to make remainder as long as divisor.
for ( ; remL ++ < dvsL ; rem . push ( 0 ) ) {
}
for ( ; remL ++ < dvsL ; rem . push ( 0 ) ) { }
// Create version of divisor with leading zero.
dvsZ . unshift ( 0 ) ;
@ -658,17 +645,13 @@ function divide( dvd, dvs, exp, s, base, isOdd ) {
if ( rem [ -- remL ] < dvsT [ remL ] ) {
for ( remI = remL ;
remI && ! rem [ -- remI ] ;
rem [ remI ] = base - 1 ) {
}
for ( remI = remL ; remI && ! rem [ -- remI ] ; rem [ remI ] = base - 1 ) { }
-- rem [ remI ] ;
rem [ remL ] += base ;
}
rem [ remL ] -= dvsT [ remL ] ;
}
for ( ; ! rem [ 0 ] ; rem . shift ( ) ) {
}
for ( ; ! rem [ 0 ] ; rem . shift ( ) ) { }
} else {
break ;
}
@ -678,9 +661,7 @@ function divide( dvd, dvs, exp, s, base, isOdd ) {
qc [ qi ++ ] = cmp ? next : ++ next ;
// Update the remainder.
rem [ 0 ] && cmp
? ( rem [ remL ] = dvd [ dvdI ] || 0 )
: ( rem = [ dvd [ dvdI ] ] ) ;
rem [ 0 ] && cmp ? ( rem [ remL ] = dvd [ dvdI ] || 0 ) : ( rem = [ dvd [ dvdI ] ] ) ;
} while ( ( dvdI ++ < dvdL || rem [ 0 ] != null ) && s -- ) ;
@ -742,8 +723,7 @@ function format( n, d, exp ) {
i = c [ 0 ] == 0 ? i + 1 : exp ? d : n [ 'e' ] + i + 1 ;
// Append zeros?
for ( ; c . length < i ; c . push ( 0 ) ) {
}
for ( ; c . length < i ; c . push ( 0 ) ) { }
i = n [ 'e' ] ;
/ *
@ -754,9 +734,7 @@ function format( n, d, exp ) {
return exp == 1 || exp == 2 && ( -- d < i || i <= TO_EXP_NEG )
// Exponential notation.
? ( n [ 's' ] < 0 && c [ 0 ] ? '-' : '' ) + ( c . length > 1
? ( c . splice ( 1 , 0 , '.' ) , c . join ( '' ) )
: c [ 0 ] ) + ( i < 0 ? 'e' : 'e+' ) + i
? ( n [ 's' ] < 0 && c [ 0 ] ? '-' : '' ) + ( c . length > 1 ? ( c . splice ( 1 , 0 , '.' ) , c . join ( '' ) ) : c [ 0 ] ) + ( i < 0 ? 'e' : 'e+' ) + i
// Normal notation.
: n [ 'toS' ] ( ) ;
@ -784,12 +762,10 @@ function rnd( x, dp, base, isOdd, r ) {
* /
more = r || i < 0 || xc [ i + 1 ] != null ;
r = ROUNDING_MODE < 4
? ( next != null || more ) &&
r = ROUNDING_MODE < 4 ? ( next != null || more ) &&
( ROUNDING_MODE == 0 ||
ROUNDING_MODE == 2 && ! isNeg ||
ROUNDING_MODE == 3 && isNeg )
: next > half || next == half &&
ROUNDING_MODE == 3 && isNeg ) : next > half || next == half &&
( ROUNDING_MODE == 4 || more ||
/ *
@ -837,8 +813,7 @@ function rnd( x, dp, base, isOdd, r ) {
}
// Remove trailing zeros.
for ( i = xc . length ; ! xc [ -- i ] ; xc . pop ( ) ) {
}
for ( i = xc . length ; ! xc [ -- i ] ; xc . pop ( ) ) { }
return x ;
}
@ -1152,12 +1127,10 @@ P['minus'] = P['sub'] = function ( y, b ) {
if ( ! xc [ 0 ] || ! yc [ 0 ] ) {
// y is non-zero?
return yc [ 0 ]
? ( y [ 's' ] = - b , y )
return yc [ 0 ] ? ( y [ 's' ] = - b , y )
// x is non-zero?
: new BigNumber ( xc [ 0 ]
? x
: new BigNumber ( xc [ 0 ] ? x
// Both are zero.
// IEEE 754 (2008) 6.3: n - n = -0 when rounding to -Infinity
@ -1170,8 +1143,7 @@ P['minus'] = P['sub'] = function ( y, b ) {
if ( xc = xc . slice ( ) , a = xe - ye ) {
d = ( xLTy = a < 0 ) ? ( a = - a , xc ) : ( ye = xe , yc ) ;
for ( d . reverse ( ) , b = a ; b -- ; d . push ( 0 ) ) {
}
for ( d . reverse ( ) , b = a ; b -- ; d . push ( 0 ) ) { }
d . reverse ( ) ;
} else {
@ -1199,8 +1171,7 @@ P['minus'] = P['sub'] = function ( y, b ) {
* /
if ( ( b = - ( ( j = xc . length ) - yc . length ) ) > 0 ) {
for ( ; b -- ; xc [ j ++ ] = 0 ) {
}
for ( ; b -- ; xc [ j ++ ] = 0 ) { }
}
// Subtract yc from xc.
@ -1208,8 +1179,7 @@ P['minus'] = P['sub'] = function ( y, b ) {
if ( xc [ -- b ] < yc [ b ] ) {
for ( i = b ; i && ! xc [ -- i ] ; xc [ i ] = 9 ) {
}
for ( i = b ; i && ! xc [ -- i ] ; xc [ i ] = 9 ) { }
-- xc [ i ] ;
xc [ b ] += 10 ;
}
@ -1217,12 +1187,10 @@ P['minus'] = P['sub'] = function ( y, b ) {
}
// Remove trailing zeros.
for ( ; xc [ -- j ] == 0 ; xc . pop ( ) ) {
}
for ( ; xc [ -- j ] == 0 ; xc . pop ( ) ) { }
// Remove leading zeros and adjust exponent accordingly.
for ( ; xc [ 0 ] == 0 ; xc . shift ( ) , -- ye ) {
}
for ( ; xc [ 0 ] == 0 ; xc . shift ( ) , -- ye ) { }
/ *
* No need to check for Infinity as + x - + y != Infinity && - x - - y != Infinity
@ -1280,9 +1248,7 @@ P['modulo'] = P['mod'] = function ( y, b ) {
b = y [ 'cmp' ] ( x ) == 1 ;
x [ 's' ] = i , y [ 's' ] = j ;
return b
? new BigNumber ( x )
: ( i = DECIMAL_PLACES , j = ROUNDING_MODE ,
return b ? new BigNumber ( x ) : ( i = DECIMAL_PLACES , j = ROUNDING_MODE ,
DECIMAL_PLACES = 0 , ROUNDING_MODE = 1 ,
x = x [ 'div' ] ( y ) ,
DECIMAL_PLACES = i , ROUNDING_MODE = j ,
@ -1356,12 +1322,10 @@ P['plus'] = P['add'] = function ( y, b ) {
if ( ! xc [ 0 ] || ! yc [ 0 ] ) {
// y is non-zero?
return yc [ 0 ]
? y
return yc [ 0 ] ? y
// x is non-zero?
: new BigNumber ( xc [ 0 ]
? x
: new BigNumber ( xc [ 0 ] ? x
// Both are zero. Return zero.
: a * 0 ) ;
@ -1373,8 +1337,7 @@ P['plus'] = P['add'] = function ( y, b ) {
if ( xc = xc . slice ( ) , a = xe - ye ) {
d = a > 0 ? ( ye = xe , yc ) : ( a = - a , xc ) ;
for ( d . reverse ( ) ; a -- ; d . push ( 0 ) ) {
}
for ( d . reverse ( ) ; a -- ; d . push ( 0 ) ) { }
d . reverse ( ) ;
}
@ -1387,9 +1350,7 @@ P['plus'] = P['add'] = function ( y, b ) {
* Only start adding at yc . length - 1 as the
* further digits of xc can be left as they are .
* /
for ( a = yc . length , b = 0 ; a ;
b = ( xc [ -- a ] = xc [ a ] + yc [ a ] + b ) / 10 ^ 0 , xc [ a ] %= 10 ) {
}
for ( a = yc . length , b = 0 ; a ; b = ( xc [ -- a ] = xc [ a ] + yc [ a ] + b ) / 10 ^ 0 , xc [ a ] %= 10 ) { }
// No need to check for zero, as +x + +y != 0 && -x + -y != 0
@ -1405,8 +1366,7 @@ P['plus'] = P['add'] = function ( y, b ) {
}
// Remove trailing zeros.
for ( a = xc . length ; xc [ -- a ] == 0 ; xc . pop ( ) ) {
}
for ( a = xc . length ; xc [ -- a ] == 0 ; xc . pop ( ) ) { }
return y [ 'c' ] = xc , y [ 'e' ] = ye , y ;
} ;
@ -1494,9 +1454,7 @@ P['round'] = function ( dp, rm ) {
// 'round() decimal places out of range: {dp}'
// 'round() decimal places not an integer: {dp}'
! ifExceptionsThrow ( dp , 'decimal places' , 'round' ) )
? 0
: dp | 0 ;
! ifExceptionsThrow ( dp , 'decimal places' , 'round' ) ) ? 0 : dp | 0 ;
rm = rm == null || ( ( ( outOfRange = rm < 0 || rm > 8 ) ||
@ -1505,9 +1463,7 @@ P['round'] = function ( dp, rm ) {
// 'round() mode not an integer: {rm}'
// 'round() mode out of range: {rm}'
! ifExceptionsThrow ( rm , 'mode' , 'round' ) )
? ROUNDING_MODE
: rm | 0 ;
! ifExceptionsThrow ( rm , 'mode' , 'round' ) ) ? ROUNDING_MODE : rm | 0 ;
return setMode ( this , dp , rm ) ;
} ;
@ -1537,9 +1493,7 @@ P['squareRoot'] = P['sqrt'] = function () {
// Negative/NaN/Infinity/zero?
if ( s !== 1 || ! c || ! c [ 0 ] ) {
return new BigNumber ( ! s || s < 0 && ( ! c || c [ 0 ] )
? NaN
: c ? x : 1 / 0 ) ;
return new BigNumber ( ! s || s < 0 && ( ! c || c [ 0 ] ) ? NaN : c ? x : 1 / 0 ) ;
}
// Initial estimate.
@ -1705,18 +1659,14 @@ P['times'] = P['mul'] = function ( y, b ) {
c = xc , xc = yc , yc = c , j = a , a = b , b = j ;
}
for ( j = a + b , c = [ ] ; j -- ; c . push ( 0 ) ) {
}
for ( j = a + b , c = [ ] ; j -- ; c . push ( 0 ) ) { }
// Multiply!
for ( i = b - 1 ; i > - 1 ; i -- ) {
for ( b = 0 , j = a + i ;
j > i ;
b = c [ j ] + yc [ i ] * xc [ j - i - 1 ] + b ,
for ( b = 0 , j = a + i ; j > i ; b = c [ j ] + yc [ i ] * xc [ j - i - 1 ] + b ,
c [ j -- ] = b % 10 | 0 ,
b = b / 10 | 0 ) {
}
b = b / 10 | 0 ) { }
if ( b ) {
c [ j ] = ( c [ j ] + b ) % 10 ;
@ -1729,8 +1679,7 @@ P['times'] = P['mul'] = function ( y, b ) {
! c [ 0 ] && c . shift ( ) ;
// Remove trailing zeros.
for ( j = c . length ; ! c [ -- j ] ; c . pop ( ) ) {
}
for ( j = c . length ; ! c [ -- j ] ; c . pop ( ) ) { }
// No zero check needed as only x * 0 == 0 etc.
@ -1761,7 +1710,10 @@ P['toBuffer'] = function ( opts ) {
if ( opts !== 'mpint' ) return 'Unsupported Buffer representation' ;
var abs = this . abs ( ) ;
var buf = abs . toBuffer ( { size : 1 , endian : 'big' } ) ;
var buf = abs . toBuffer ( {
size : 1 ,
endian : 'big'
} ) ;
var len = buf . length === 1 && buf [ 0 ] === 0 ? 0 : buf . length ;
if ( buf [ 0 ] & 0x80 ) len ++ ;
@ -1789,9 +1741,10 @@ P['toBuffer'] = function ( opts ) {
if ( ! opts ) opts = { } ;
var endian = { 1 : 'big' , '-1' : 'little' } [ opts . endian ]
|| opts . endian || 'big'
;
var endian = {
1 : 'big' ,
'-1' : 'little'
} [ opts . endian ] || opts . endian || 'big' ;
var hex = this . toString ( 16 ) ;
if ( hex . charAt ( 0 ) === '-' ) throw new Error (
@ -1808,8 +1761,9 @@ P['toBuffer'] = function ( opts ) {
var hx = hex
. split ( new RegExp ( '(.{' + ( 2 * size ) + '})' ) )
. filter ( function ( s ) { return s . length > 0 } )
;
. filter ( function ( s ) {
return s . length > 0
} ) ;
hx . forEach ( function ( chunk , i ) {
for ( var j = 0 ; j < size ; j ++ ) {
@ -1830,8 +1784,7 @@ P['toBuffer'] = function ( opts ) {
* /
P [ 'toExponential' ] = P [ 'toE' ] = function ( dp ) {
return format ( this ,
( dp == null || ( ( outOfRange = dp < 0 || dp > MAX ) ||
return format ( this , ( dp == null || ( ( outOfRange = dp < 0 || dp > MAX ) ||
/ *
* Include '&& dp !== 0' because with Opera - 0 == parseFloat ( - 0 ) is
@ -1841,9 +1794,7 @@ P['toExponential'] = P['toE'] = function ( dp ) {
// 'toE() decimal places not an integer: {dp}'
// 'toE() decimal places out of range: {dp}'
! ifExceptionsThrow ( dp , 'decimal places' , 'toE' ) ) && this [ 'c' ]
? this [ 'c' ] . length - 1
: dp | 0 , 1 ) ;
! ifExceptionsThrow ( dp , 'decimal places' , 'toE' ) ) && this [ 'c' ] ? this [ 'c' ] . length - 1 : dp | 0 , 1 ) ;
} ;
@ -1981,9 +1932,7 @@ P['toFraction'] = P['toFr'] = function ( maxD ) {
// Determine which fraction is closer to x, n0 / d0 or n1 / d1?
frac = n1 [ 'div' ] ( d1 ) [ 'minus' ] ( x ) [ 'abs' ] ( ) [ 'cmp' ] (
n0 [ 'div' ] ( d0 ) [ 'minus' ] ( x ) [ 'abs' ] ( ) ) < 1
? [ n1 [ 'toS' ] ( ) , d1 [ 'toS' ] ( ) ]
: [ n0 [ 'toS' ] ( ) , d0 [ 'toS' ] ( ) ] ;
n0 [ 'div' ] ( d0 ) [ 'minus' ] ( x ) [ 'abs' ] ( ) ) < 1 ? [ n1 [ 'toS' ] ( ) , d1 [ 'toS' ] ( ) ] : [ n0 [ 'toS' ] ( ) , d0 [ 'toS' ] ( ) ] ;
return MAX_EXP = exp , DECIMAL_PLACES = dp , frac ;
} ;
@ -2009,9 +1958,7 @@ P['toPrecision'] = P['toP'] = function ( sd ) {
// 'toP() precision not an integer: {sd}'
// 'toP() precision out of range: {sd}'
! ifExceptionsThrow ( sd , 'precision' , 'toP' ) )
? this [ 'toS' ] ( )
: format ( this , -- sd | 0 , 2 ) ;
! ifExceptionsThrow ( sd , 'precision' , 'toP' ) ) ? this [ 'toS' ] ( ) : format ( this , -- sd | 0 , 2 ) ;
} ;
@ -2044,8 +1991,7 @@ P['toString'] = P['toS'] = function ( b ) {
if ( xe < 0 ) {
// Prepend zeros.
for ( ; ++ xe ; str = '0' + str ) {
}
for ( ; ++ xe ; str = '0' + str ) { }
str = '0.' + str ;
// Positive exponent?
@ -2054,8 +2000,7 @@ P['toString'] = P['toS'] = function ( b ) {
if ( ++ xe > strL ) {
// Append zeros.
for ( xe -= strL ; xe -- ; str += '0' ) {
}
for ( xe -= strL ; xe -- ; str += '0' ) { }
} else if ( xe < strL ) {
str = str . slice ( 0 , xe ) + '.' + str . slice ( xe ) ;
}
@ -2118,6 +2063,9 @@ P['valueOf'] = function () {
// EXPORT
BigNumber . config ( { EXPONENTIAL_AT : 9999999 , DECIMAL_PLACES : 0 , ROUNDING_MODE : 1 } ) ;
BigNumber . config ( {
EXPONENTIAL_AT : 9999999 ,
DECIMAL_PLACES : 0 ,
ROUNDING_MODE : 1
} ) ;
module . exports = BigNumber ;