4 ms·
A lot of people don't know this, but `BigInt`s are supported in modern JavaScript; integers of arbitrarily large precision. Try in your browser console: 2
by ralusek 7y ago
A lot of people don't know this, but `BigInt`s are supported in modern JavaScript; integers of arbitrarily large precision.
Try in your browser console:
2n ** 4096n
// output (might have to scroll right)
1044388881413152506691752710716624382579964249047383780384233483283953907971557456848826811934997558340890106714439262837987573438185793607263236087851365277945956976543709998340361590134383718314428070011855946226376318839397712745672334684344586617496807908705803704071284048740118609114467977783598029006686938976881787785946905630190260940599579453432823469303026696443059025015972399867714215541693835559885291486318237914434496734087811872639496475100189041349008417061675093668333850551032972088269550769983616369411933015213796825837188091833656751221318492846368125550225998300412344784862595674492194617023806505913245610825731835380087608622102834270197698202313169017678006675195485079921636419370285375124784014907159135459982790513399611551794271106831134090584272884279791554849782954323534517065223269061394905987693002122963395687782878948440616007412945674919823050571642377154816321380631045902916136926708342856440730447899971901781465763473223850267253059899795996090799469201774624817718449867455659250178329070473119433165550807568221846571746373296884912819520317457002440926616910874148385078411929804522981857338977648103126085903001302413467189726673216491511131602920781738033436090243804708340403154190336n
To use, just add `n` after the number as literal notation, or can cast any Number x with BigInt(x). BigInts may only do operations with other BigInts, so make sure to cast any Numbers where applicable.
I know this is about C, I thought I'd just mention it, since many people seem to be unaware of this.
- justicz 7y agoHm, does this work in Safari? https://caniuse.com/#feat=bigint https://caniuse.com/#feat=bigint
- jakear 7y ago“Syntax Error: No identifiers allowed directly after numeric literal”
- The_rationalist 7y agoDoes X work in Safari can almost always be answered by 'no' iff the feature is from a recent(less than a decade) spec
- recursive 7y agoSafari is the new IE.
- ralusek 7y agoNot yet, but I believe babel and others just transpile/polyfill it by having it fall back on a string arithmetic library for working with integers of arbitrary precision.
- recursive 7y agoThat will never be totally reliable as 1) javascript is dynamically typed 2) javascript doesn't support operator overloading. Nonetheless, there are attempts. https://www.npmjs.com/package/babel-plugin-transform-bigint https://www.npmjs.com/package/babel-plugin-transform-bigint > Update: Now it can convert a code using BigInt into a code using JSBI (https://github.com/GoogleChromeLabs/jsbi https://github.com/GoogleChromeLabs/jsbi). It will try to detect when an operator is used for bigints, not numbers. This will not work in many cases, so please use JSBI directly only if you know, that the code works only with bigints. (emphasis mine)
- waltpad 7y agoSo clearly, if llvm is used as a backend for js, this feature will come in handy. On a side note, apparently, it will also be useful for the rust folks, which has user implemented libraries to emulate C-like bitfields, and implement bigints, So this work has promising outcomes.