4

Citing the ECMAScript spec Section 5.2:

The notation “x modulo y” (y must be finite and nonzero) computes a value k of the same sign as y (or zero) such that abs(k) < abs(y) and x−k = q × y for some integer q.

so if y is positive, the result k of 'x modulo y' is positive regardless of the sign of x.

and if my understanding is right, ToInt32(-1) equals ToInt32(1)?

user1039304
  • 365
  • 5
  • 10
  • 1
    If you were talking about math you'd be correct, but the language isn't implemented that way. Open up a console or execute a node script messing around with different values of x and y. – reagan Aug 15 '13 at 16:17
  • @reagan Opening up a console or messing around with a node script wouldn't prove anything, since the modulo operator described in that section isn't implemented in JS. – Asad Saeeduddin Aug 15 '13 at 16:30
  • `ToInt32(-1)` does **not** equal `ToInt32(1)`. – Ted Hopp Aug 15 '13 at 17:15

3 Answers3

5

The notation x modulo y is used internally within the spec to describe the result of certain operations. So yes, the result k of x modulo y is (by definition) of the same sign as y. It is not claimed that the % operator is equivalent to modulo.

If you're interested, the actual spec for % can be found under section 11.5.3. Interestingly, it makes no use of modulo.

Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139
  • they didn't ask about the operator or symbol, they asked about javascript's interpretation of modulus operations – reagan Aug 15 '13 at 16:16
  • i know that x modulo y is used internally within the spec.i just want to know the meaning of x modulo y.for example, what is the result of '1 modulo 10' and '-1 modulo 10'? – user1039304 Aug 15 '13 at 16:17
  • 1
    @reagan Not really. They asked if "the result k of 'x modulo y' is positive regardless of the sign of x". This is (by the very definition of modulo in section 5.2) true. The question doesn't ask anything about JS's interpretation of modulus operations. – Asad Saeeduddin Aug 15 '13 at 16:18
  • Modulo: '1 modulo 10' = 1, '-1 module 10' = 1 Javascript: '1%10' = 1, '-1%10' = -1 – reagan Aug 15 '13 at 16:20
  • @user1039304 You can compute it using the definition of the operator you quoted. `1 modulo 10` would be 1, since 1 < 10 and 1-1 = q * 10 for integer 0. – Asad Saeeduddin Aug 15 '13 at 16:22
  • @reagan What does that have to do with anything? I explicitly stated in my answer that `%` is distinct from `modulo`, which isn't a JS operator. It is functor described only for the purposes of the spec, and is used to describe the `floor` function a few lines later. – Asad Saeeduddin Aug 15 '13 at 16:24
  • I believe this is the appropriate answer to the question, however, could you add a section answering user1039304's specific question? The result of x modulo y is not always positive. -5 modulo -2 produces -1. – Sacho Aug 15 '13 at 16:25
  • @Sacho I never claimed `x modulo y` is always positive. To quote the answer: "the result k of x modulo y is (by definition) of the same sign as y". This is congruent with -5 modulo -2 producing -1. – Asad Saeeduddin Aug 15 '13 at 16:29
  • @Sacho The OP's question was: "so **if y is positive**, the result k of 'x modulo y' is positive regardless of the sign of x." – Asad Saeeduddin Aug 15 '13 at 16:31
0

Copy pasting from my previous answer here:

Take a % b

1. When both +ve, Modulo & Remainder are one and the same
2. When a is -ve, they are not the same

For example;

a = -10, b = 3

Remainder of -10 % 3 = -1

for Modulo, add a greater multiple of 3 to your 'a' and calculate the remainder.

-10 + 12 = 2

2 % 3 = 2 is your answer

Community
  • 1
  • 1
Venkata Krishna
  • 14,926
  • 5
  • 42
  • 56
-1

The modulo operation is defined as the mathematical modulo operation:

Mathematical operations such as addition, subtraction, negation, multiplication, division, and the mathematical functions defined later in this clause should always be understood as computing exact mathematical results on mathematical real numbers, which do not include infinities and do not include a negative zero that is distinguished from positive zero.

Your question:

ToInt32(-1) equals ToInt32(1)

Well, no:

Let posInt be sign(number) * floor(abs(number)).

posInt = sign(-1) * floor(abs(-1)) = -1;

Let int32bit be posInt modulo 232; that is, a finite integer value k of Number type with positive sign and less than 232 in magnitude such that the mathematical difference of posInt and k is mathematically an integer multiple of 232.

int32bit = posInt mod 4294967296 = -1 mod 4294967296 = 4294967295

(wolfram alpha link for mathematical result)

If int32bit is greater than or equal to 231, return int32bit − 232, otherwise return int32bit.

Because 4294967295 >= 2147483648, we return 4294967295 - 4294967296, I.E. -1.

If we run the same steps for ToInt32(1), we get 1. So they don't have the same result.

Esailija
  • 138,174
  • 23
  • 272
  • 326