Modulo in JavaScript - large number

前端 未结 6 1859
情深已故
情深已故 2021-01-07 20:39

I try to calculate with JS\' modulo function, but don\'t get the right result (which should be 1). Here is a hardcoded piece of code.

var checkSum = 21050170         


        
6条回答
  •  心在旅途
    2021-01-07 21:24

    looks like you've fallen victim to this: What is JavaScript's highest integer value that a Number can go to without losing precision?

    just to reiterate what's in the other thread:

    they are 64-bit floating point values, the largest exact integral value is 2^53. however, from the spec section [8.5: Number Type]:

    Some ECMAScript operators deal only with integers in the range −2^31 through 2^31−1, inclusive, or in the range 0 through 2^32−1, inclusive. These operators accept any value of the Number type but first convert each such value to one of 2^32 integer values. See the descriptions of the ToInt32 and ToUint32 operators in sections 0 and 0, respectively

    But credit where credit is due. Jimmy got the accepted answer over there for doing the legwork (well, googling).

提交回复
热议问题