modulo

# raul mihaila (12 years ago)

I think the definition of modulo is wrong. people.mozilla.org/~jorendorff/es6-draft.html#sec-5.2 k should be of the same sign as x not as y.

Also, shouldn't ToUint32 and ToUint16 return only positive numbers? If the argument is, for example, -3, the result will be -3 (assuming that the modulo definition is wrong). Or maybe the argument will never be negative?

# Claude Pache (12 years ago)

Le 16 sept. 2013 à 11:30, raul mihaila <raul.mihaila at gmail.com> a écrit :

I think the definition of modulo is wrong. people.mozilla.org/~jorendorff/es6-draft.html#sec-5.2 k should be of the same sign as x not as y.

I think the definition is correct, and is more useful than the opposite convention. It is indeed the opposite convention of the one used by the remainder operator (% in JS), but it is not the remainder operator. (BTW, is there any chance that the modulo operator [1] will be introduced in EcmaScript, rather sooner than later?)

[1] strawman:modulo_operator

In fact, since x modulo y is apparently only use with positive (and integral) y in the specification, the definition should be simplified, by restricting to the case where y is positive.

Also, shouldn't ToUint32 and ToUint16 return only positive numbers? If the argument is, for example, -3, the result will be -3 (assuming that the modulo definition is wrong). Or maybe the argument will never be negative?

Assuming the modulo definition is correct, ToUint32 and ToUint16 work as expected (if the argument is -3 the result is 2^32-3, resp. 2^16-3.

# raul mihaila (12 years ago)

But if the definition of modulo is correct, then isn't ToInt32 wrong? people.mozilla.org/~jorendorff/es6-draft.html#sec-7.1.5 Here, -3 is transformed into 3.

# Claude Pache (12 years ago)

Le 16 sept. 2013 à 15:12, raul mihaila <raul.mihaila at gmail.com> a écrit :

But if the definition of modulo is correct, then isn't ToInt32 wrong? people.mozilla.org/~jorendorff/es6-draft.html#sec-7.1.5 Here, -3 is transformed into 3.

If one follows carefully the algorithm, one has:

  • step 4: int == -3;
  • step 5: int32bit == 2^32 - 3;
  • step 6 (since int32bit ≥ 2^31): returnint32bit - 2^32 == -3`.

(Note that at no step there is a direct sign change like -3 into 3 or vice versa; but the value shifts by multiples of 2^32 in order to go into the right interval.)

# Claude Pache (12 years ago)

I have almost opened a duplicate of ecmascript#980. I find that the somewhat convoluted definition given at the end of 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.

can be usefully simplified and made clearer by saying:

The expression “x modulo y”, where y is a positive integer, evaluates to the unique value k such that 0 ≤ k < abs(y) and x − k = q × y for some integer q.

To the reason given not to simplify ("it doesn't hurt and maybe someday in the future it will be needed"), there is a simple one-word response: YAGNI.

At least, restricting to positive numbers for the second operand has the following concrete advantage: it saves the reader from useless metaphysical question about the sign of the result. (And for restricting to integers?... uh... just YAGNI.)

# Jason Orendorff (12 years ago)

On Mon, Sep 16, 2013 at 4:30 AM, raul mihaila <raul.mihaila at gmail.com> wrote:

I think the definition of modulo is wrong. people.mozilla.org/~jorendorff/es6-draft.html#sec-5.2 k should be of the same sign as x not as y.

It's the % operator that is wrong! Any mathematician can tell you what modulo means.

(Of course nothing can be done about %. The wrongness was inherited from Java, and it wasn’t invented for Java either...)

# raul mihaila (12 years ago)

Thanks Claude, I was missing something, it's clear now.

# raul mihaila (12 years ago)

If I understand correctly, q = floor(x / y), in x - k = q x y, where k = x modulo y. If this is correct I think that such a note would be nice to have for non-mathematicians. :)

Raul M

# Brendan Eich (12 years ago)

Since no one else replied, I will take a stab.

raul mihaila <mailto:raul.mihaila at gmail.com> September 17, 2013 2:46 PM If I understand correctly, q = floor(x / y), in x - k = q x y, where k = x modulo y. If this is correct I think that such a note would be nice to have for non-mathematicians. :)

It follows from 5.2, Algorithm Conventions, specifically:

The mathematical function floor(x) yields the largest integer (closest to positive infinity) that is not larger than x.

NOTE floor(x) = x−(x modulo 1).

# raul mihaila (12 years ago)

I saw that. I'm sure the spec is clear enough for implementers. Somebody mentioned that book authors should explain the spec to the developers. But the books aren't enough. For example, I didn't see something like this in any JS book that I've read. Array.apply(null, { length: 5 }).map(Number.call, Number); twitter.com/cowboy/status/288702647479984128 When an empty array or an 'empty array-like object' (but with a valid length property that is greater than 0) is used in the apply method, you'd think that no arguments are actually passed, because it's not intuitive to think otherwise. Only when you read the spec you see that something like this is possible. (I'm not saying that there's absolutely no book that explains this, but I can't read them all). I don't know what can be done... Regarding the modulo/floor section, I would prefer something like this instead:

The mathematical function floor(x) produces the largest integer (closest to positive infinity) that is not larger than x. 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, where q = floor(x / y). NOTE floor(x) = x-(x modulo 1).

Raul M