Document the BigInt(large number) footgun #170
Comments
the 0x-prefixed numbers will be normal numbers before a BigInt is created, which can cause precision loss if the numbers are large enough. I’d suggest using the literal syntax, or passing strings to the BigInt constructor. |
Passed it as a string, works. Passed: 7640891576956012808 As a string: 7640891576956012808 The precision loss kind of sucks for my application, but, I guess I have to make due for now. I am a little bit worried about the outcome at the end as quite a bit will happen to this variable. |
Just use literals: instead of You don't need arrays to see the difference in precision between Numbers and BigInts, the interactive Node shell or Chrome's DevTools console is enough:
"number" and "hex" are really "decimal Number" and "hex Number" -- both are Numbers, neither has more or less precision than the other. In other words, Note that I'm capitalizing the term "Number". In JavaScript, "Number" refers to a specific data type, which is distinct from BigInts. Writing |
Great fantastic, thanks @jakobkummerow. The only problem is that Typescript doesn't seem to understand what 'n' means yet, so going to have to resort to BigInt everything for now. This should be fun >.> |
Seems like we should call this out explicitly in the documentation, as a gotcha to avoid, cc @sarahgp. Cc @DanielRosenwasser for TypeScript. |
I'm a little late, but TypeScript@3.2.0 released BigInt, @joshuajbouw. Is this issue still unresolved? I could try to open a PR if nobody has claimed this yet. |
Yes, documentation is still needed here, and your help would be very much appreciated! |
Sorry, I'm lost in the context here. Is this just a foot-gun that trying to write a really big numeric literal and passing it as an argument to |
Yes, exactly. Probably in this repo and MDN. Nothing needed on the TS side, though I am happy to review any documentation or blog posts you might be writing if you'd like. |
Check out microsoft/TypeScript#29863 |
For some reason, the numbers are getting a bit mixed up slightly. Will contain just the relevant code.
What I expected: For 0x6A09E667F3BCC908 to be in ctx.h[0]
What happened: 0x6a09e667f3bcc800 ended up in ctx.h[0]
I am running latest Node.
This is kind of a big problem =). I didn't know where else to put this.
The text was updated successfully, but these errors were encountered: