duktape icon indicating copy to clipboard operation
duktape copied to clipboard

Add support for BigInts

Open svaarala opened this issue 5 years ago • 7 comments

BigInts are useful to represent 64-bit values and also for some algorithms where 53-bit precision is not enough. There's a stage 3 proposal, implemented by e.g. NodeJS, here: https://github.com/tc39/proposal-bigint. http://thecodebarbarian.com/an-overview-of-bigint-in-node-js.html.

The changes required are:

  • An internal non-object type to represent bigints (heap allocated naturally, like strings)
  • Possibly a new type tag for bigints
  • C API support, at least for converting between non-bigints

There's already a bigint implementation needed by number conversion code. Ideally that implementation would be replaced by internal bigints, optimizing footprint so that without bigints the additional footprint would be minimal for number conversion.

svaarala avatar Mar 04 '19 17:03 svaarala

Will BigInt save you from this behavior?

function test() {
    var a = 5527939700884757;
    var b = 8944394323791464;

    var c = a + b;

    // should be ‭14472334024676221‬
    // but print 14472334024676220
    console.log(c);
}

test();

alex-rass-88 avatar Oct 08 '19 10:10 alex-rass-88

As you probably know, that behavior happens because ECMAScript number type is IEEE double which all arithmetic is based on. With bigints it would be:

function test() {
    var a = 5527939700884757n;
    var b = 8944394323791464n;

    var c = a + b;

    console.log(c);
}

test();  // prints 14472334024676221n

It won't save you from that behavior without code changes however, as one needs to use bigints explicitly.

svaarala avatar Oct 09 '19 10:10 svaarala

BigInt will be useful. Especially for applications like games.

alex-rass-88 avatar Oct 09 '19 10:10 alex-rass-88

I would like to add my vote for this feature with priority - it's pretty much the only modern language feature which can't be polyfilled or easily transpiled out.

TomMettam avatar May 14 '20 15:05 TomMettam

Almost all languages support 64-bit support now. In addition, 64-bit has a lot of applications in today's world.

It would be super cool if Duktape provides BigInt or internal some flag in duk_config.h which could help us work just with int64 'number' without precision loss (deferring from IEEE standard).

Just curious, is there any plan on adding above support?

acode-x avatar Dec 04 '22 14:12 acode-x

Also, related to https://github.com/svaarala/duktape/issues/196

@svaarala Could you give us any hints as to when the above would be on your to-do list

acode-x avatar Dec 04 '22 17:12 acode-x

Extremely important.

rafalsk avatar Jul 27 '23 08:07 rafalsk