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

unsafe integer passed to Big constructor when using .sqrt() / option to throw for unsafe ints #115

Closed
danday74 opened this issue Jan 1, 2019 · 3 comments

Comments

@danday74
Copy link

danday74 commented Jan 1, 2019

big.js line 780:

r = new Big(s);

Here, s is potentially an unsafe integer when square rooting numbers larger than Number.MAX_SAFE_INTEGER^2

A safer op might be:

r = new Big(s + '');

Please close if no fix is needed, but I wanted to point this out. I am throwing if the Big.js constructor is passed an unsafe integer.

Would be great if big.js had a toggle option to throw in this scenario. As it currently stands, precision can be accidentally lost when doing Big(Number.MAX_SAFE_INTEGER + 1) - I appreciate a string can be passed in this occasion but as a developer, it is easy to accidentally pass an unsafe int and miss it.

@MikeMcl
Copy link
Owner

MikeMcl commented Jan 1, 2019

It doesn't matter if s is an unsafe integer, and turning it into a string is not necessary - that happens anyway in the Big constructor.

Regarding whether to have an option to throw on unsafe integers generally, that is not something I want to add to this library. It is available in bignumber.js I think.

@danday74
Copy link
Author

danday74 commented Jan 2, 2019

ok

shame about not adding the option - bignumber.js much slower than this lib

I've had to create https://www.npmjs.com/package/big.js.safe as a workaround :(

@MikeMcl
Copy link
Owner

MikeMcl commented Jan 7, 2019

Okay, but it may be misleading to call the package "safe" as precision can still easily be lost when non-integer JavaScript numbers are passed

new Big(1.0000000000000001);    // '1'
new Big(88259496234518.57);     // '88259496234518.56'
new Big(1e-324);                // '0'

@MikeMcl MikeMcl closed this as completed Jan 7, 2019
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