javascript - what is the result of 'x modulo y'? -
citing ecmascript spec section 5.2:
the notation “x modulo y” (y must finite , nonzero) computes value k of same sign y (or zero) such abs(k) < abs(y) , x−k = q × y integer q.
so if y positive, result k of 'x modulo y' positive regardless of sign of x.
and if understanding right, toint32(-1) equals toint32(1)?
the modulo
operation defined mathematical modulo operation:
mathematical operations such addition, subtraction, negation, multiplication, division, , mathematical functions defined later in clause should understood computing exact mathematical results on mathematical real numbers, not include infinities , not include negative 0 distinguished positive zero.
your question:
toint32(-1) equals toint32(1)
well, no:
let posint sign(number) * floor(abs(number)).
posint = sign(-1) * floor(abs(-1)) = -1;
let int32bit posint modulo 232; is, finite integer value k of number type positive sign , less 232 in magnitude such mathematical difference of posint , k mathematically integer multiple of 232.
int32bit = posint mod 4294967296 = -1 mod 4294967296 = 4294967295
(wolfram alpha link mathematical result)
if int32bit greater or equal 231, return int32bit − 232, otherwise return int32bit.
because 4294967295 >= 2147483648
, return 4294967295 - 4294967296
, i.e. -1
.
if run same steps toint32(1)
, 1
. don't have same result.
Comments
Post a Comment