-
Notifications
You must be signed in to change notification settings - Fork 156
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 fromNumber() #223
Comments
Do you mean from > typeof new Number(123)
'object' |
@fanatid No. I mean there is no functionality for creating a new BN from a Number. The BN constructor takes a string and a radix. Therefore I have to convert a number to a string before I can create a new BN with it. And unless I missed it, there's no utility function for taking a JavaScript Number and returning a equivalent BN instance. |
Actually, it's possible.. > new BN(42).muln(2)
BN { negative: 0, words: [ 84 ], length: 1, red: null } Line 39 in f953de2
Lines 77 to 79 in f953de2
Lines 113 to 141 in f953de2
|
@fanatid When I try to put a number into the constructor like: In this case the amount the expression resolves to is If I change the line to convert the number to a string first: |
@fanatid I'm proposing a function along the lines of this:
Please note, I'm happy to make a PR with the function. There are definitely cases where it's needed and where conversion to string does the trick. It seems something's up with some JS number values when executing this assertion. |
Number > 32642567254300000 <= Number.MAX_SAFE_INTEGER
false What we actually need this is better error message, not new function. |
A better error message would be good, but the new function would keep people from having to convert numbers to strings before constructing a BN. If there was a fromNumber, people would likely use it and never encounter the error condition. Also better documentation would help.
…Sent from my iPhone
On Jul 28, 2019, at 2:47 AM, Kirill Fomichev ***@***.***> wrote:
Number 32642567254300000 is unsafe, because this error raised.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER
> 32642567254300000 <= Number.MAX_SAFE_INTEGER
false
What we actually need this is better error message, not new function.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.
|
While functions for creating BN from string/number/etc probably would be helpful, I do not think that will be changed. BN used in too much things and if tomorrow major version will be released with functions which should be used instead constructor thousands packages will need change code a lot. PR's for better error messages / documentations is highly welcome! |
I'm amazed that there is no easy way to create a BN from a JavaScript number without converting that number to a string and using the BN constructor, like:
const my BN = new BN(Number(90125).toString(), 10);
Instead, I'd like to do something like:
const myBN = BN.fromNumber(90125)
The text was updated successfully, but these errors were encountered: