bignumber.js icon indicating copy to clipboard operation
bignumber.js copied to clipboard

Implement polyfill for TC39 bigdecimal

Open hildjj opened this issue 5 years ago • 4 comments

See: https://github.com/tc39/proposal-decimal/issues/17 (polyfill)

and: https://github.com/tc39/proposal-decimal/issues/18 (babel)

hildjj avatar Feb 28 '20 16:02 hildjj

Okay, here's a preliminary polyfill implemented from a severely hacked decimal.js:

bigdecimal.zip

The API is as described at the TC39 Decimal proposal and the Decimal: For Stage 1 slideshow.

The zip file contains an ES module version and an ES 3+ compatible UMD version.,

Example usage:

import BigDecimal from './bigdecimal.min.mjs';

let a, b, c, options;

a = BigDecimal(0.1);
b = BigDecimal('0.1');

console.log( a['==='](b) )              // false

console.log(a.toString());              // '0.1000000000000000055511151231257827021181583404541015625'
console.log(b.toString());              // '0.1'

console.log( a['!=='](b) );             // true
console.log( a['=='](b) );              // false
console.log( a['!='](b) );              // true
console.log( a['>'](b) );               // true
console.log( a['>='](b) );              // true
console.log( a['<'](b) );               // false
console.log( a['<='](b) );              // false

console.log( a['+'](b).toString() );    // '0.2000000000000000055511151231257827021181583404541015625'

c = a['-'](b);
console.log( c.toString() );            // '5.5511151231257827021181583404541015625e-18'
console.log( c.toFixed() );             // '0.0000000000000000055511151231257827021181583404541015625'

console.log( a['*'](b).toString() );    // '0.01000000000000000055511151231257827021181583404541015625'

a = BigDecimal('34.63');
b = BigDecimal('2.2343465');
c = BigDecimal(b);

console.log( b['==='](c) );

console.log( a['%'](b).toString() );

options = { maximumSignificantDigits: 20, roundingMode: BigDecimal.ROUND_HALF_UP };

console.log( BigDecimal.add(a, b, options).toString() );
console.log( BigDecimal.sub(a, b, options).toString() );
console.log( BigDecimal.mul(a, b, options).toString() );
console.log( BigDecimal.div(a, b, options).toString() );
console.log( BigDecimal.mod(a, b, options).toString() );
console.log( BigDecimal.pow(a, BigDecimal(11), options).toString() );

options = { maximumFractionDigits: 2, roundingMode: BigDecimal.ROUND_UP };

console.log( BigDecimal.round(a, options).toString() );

console.log( b.toExponential() );
console.log( b.toExponential(4, { roundingMode: BigDecimal.ROUND_HALF_UP }) );

console.log( b.toFixed(4, { roundingMode: BigDecimal.ROUND_HALF_UP }) );
console.log( b.toPrecision(4, { roundingMode: BigDecimal.ROUND_HALF_UP }) );

I haven't implemented a BigDecimal.partition method as it seems an odd inclusion and its behaviour was not adequately specified.

MikeMcl avatar Mar 03 '20 23:03 MikeMcl

Ping https://github.com/tc39/proposal-decimal/issues/17 for input

hildjj avatar Mar 05 '20 21:03 hildjj

@MikeMcl do you have a non-minified version checked in somewhere?

hildjj avatar Mar 05 '20 21:03 hildjj

Yes, I have a non-minified version. I don't want to expose it before I've had a chance to clean it up a bit.

MikeMcl avatar Mar 06 '20 00:03 MikeMcl