Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement polyfill for TC39 bigdecimal #256

Open
hildjj opened this issue Feb 28, 2020 · 4 comments
Open

Implement polyfill for TC39 bigdecimal #256

hildjj opened this issue Feb 28, 2020 · 4 comments

Comments

@hildjj
Copy link

hildjj commented Feb 28, 2020

See:
tc39/proposal-decimal#17
(polyfill)

and:
tc39/proposal-decimal#18
(babel)

@MikeMcl
Copy link
Owner

MikeMcl commented Mar 3, 2020

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.

@hildjj
Copy link
Author

hildjj commented Mar 5, 2020

Ping tc39/proposal-decimal#17 for input

@hildjj
Copy link
Author

hildjj commented Mar 5, 2020

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

@MikeMcl
Copy link
Owner

MikeMcl commented Mar 6, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants