Skip to content
This repository has been archived by the owner on Oct 10, 2019. It is now read-only.

Sign of result of "remainder" #84

Closed
jakobkummerow opened this issue Sep 14, 2017 · 5 comments
Closed

Sign of result of "remainder" #84

jakobkummerow opened this issue Sep 14, 2017 · 5 comments

Comments

@jakobkummerow
Copy link
Collaborator

The proposal currently reads:

1.1.6 BigInt::remainder (x, y)
If y is 0n, throw a RangeError exception.
Return the BigInt representing x modulo y.

where "modulo" links to https://tc39.github.io/ecma262/#sec-algorithm-conventions (side note: that should be https://tc39.github.io/ecma262/#sec-mathematical-operations instead), which says (emphasis mine):

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.

This is notably different from the % operator, which says:

The sign of the result equals the sign of the dividend.

I would assume that % on BigInts should mirror the behavior for integer-valued Numbers. Having differing behavior would be quite surprising, I think:

 7  % -4  ===  3
 7n % -4n === -1n
-7  %  4  === -3
-7n %  4n ===  1n

(I don't think this is a bug in the existing spec; modulo is mostly used with positive powers of two as the right-hand side there, where ToUint behavior is desired.)

@littledan
Copy link
Member

Yes, this should have semantics similar to the % operator on Numbers with respect to the sign. Thanks for pointing out the mismatch; I will fix this soon.

@lars-t-hansen
Copy link

Because of the matter of the sign, Scheme distinguishes between "remainder" and "modulo" and provides standard procedures for both: http://www.schemers.org/Documents/Standards/R5RS/HTML/r5rs-Z-H-2.html#%_toc_%_sec_6.2.5. I suggest you do the same.

@littledan
Copy link
Member

@lars-t-hansen We discussed this in #37 . It seems that % is neither modulo nor remainder, if I understand correctly. Name suggestions are welcome!

@lars-t-hansen
Copy link

It might be useful to provide correct (in the number-theoretic sense) remainder and modulo operations, then, since BigInt is an integer type. (Presumably as static methods.)

@littledan
Copy link
Member

@lars-t-hansen Seems not so hard for a user to implement out of % and a few conditionals, right? Would it be OK to leave this for v2?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants