Subtracting long numbers in javascript

后端 未结 6 1810
执笔经年
执笔经年 2020-12-02 01:44

Why is q == 0 in the following script?



        
6条回答
  •  伪装坚强ぢ
    2020-12-02 02:14

    As of January 2020, BigInt datatype is going to be added to Javascript. The proposal is currently in Stage 4. It will enable precise calculation for number which are more than 2^53-1 (Number.MAX_SAFE_INTEGER).

    BigInt has been shipped in Chrome, Node, Firefox, and is underway in Safari. Read more here.

    var start = BigInt('1234567890123456789');
    var end = BigInt('1234567890123456799');
    var q = end - start;
    alert(q)

    A BigInt is created by appending n to the end of an integer literal — 10n — or by calling the function BigInt(). It is also different from Number so 1 + 1n will fail.

    You can read more about it here from MDN pages

提交回复
热议问题