Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integer modulo operation is not specified #493

Closed
DartBot opened this issue Nov 17, 2011 · 4 comments
Closed

Integer modulo operation is not specified #493

DartBot opened this issue Nov 17, 2011 · 4 comments
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries.

Comments

@DartBot
Copy link

DartBot commented Nov 17, 2011

This issue was originally filed by [email protected]


Behavior of int.operator% is not specified.
Apparently it is not the same as ECMA-262 modulo

@floitschG
Copy link
Contributor

Yes. the documentation has been lost over time...
Dart's modulo should follow the Euclidean definition (see http://en.wikipedia.org/wiki/Modulo_operation)


Added Area-Library, Triaged labels.

@DartBot
Copy link
Author

DartBot commented Mar 29, 2012

This comment was originally written by [email protected]


There appears to be a bug with large numbers in the Dart standalone VM when the first argument is negative:

void main() {
  print((-922337203685477580 % 284112972158697727).toString());
  print((-9223372036854775808 % 2841129721586977273).toString());
}

prints:

214114684949313328
-699982872093843989

The first result is expected since it satisfies 0 <= (a % b) <= abs(b). The second is a bug.
 

@DartBot
Copy link
Author

DartBot commented Mar 30, 2012

This comment was originally written by [email protected]


The Euclidean definition of mod is somewhat less useful without a corresponding Euclidean division operator. Together, the operators '/' and '%' should maintain the defining Euclidean properties:

q = a / b
r = a % b
q * b + r == a
0 <= r < |b|

To take a concrete example, let a = -8 and b = 3. Then the Euclidean modulus must be 1, since

q * b + r == a
q * 3 + r == -8
q * 3 == -8 - r

and 1 is the only value for r in the range [0, 2] that makes (-8 - r) a multiple of 3.

But then:

q * 3 + 1 == -8
q * 3 == -9
q == -3

In other words, a Euclidean division operator must yield -8 / 3 == -3. However, Dart's integer division yields -8 ~/ 3 == -2 since it truncates (rounds to 0).

So, while in some ways I appreciate the convenience of a '%' operator that always returns a non-negative result (e.g., due to its utility for accessing table entries using an expression like 'index % length'), it violates the natural expectation that (a / b) * b + (a % b) ==a.

@floitschG
Copy link
Contributor

Updated the documentation with r20942.


Added Fixed label.

@DartBot DartBot added Type-Defect area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. labels Apr 4, 2013
copybara-service bot pushed a commit that referenced this issue Nov 16, 2022
Changes:
```
> git log --format="%C(auto) %h %s" a70b93a..37951d1
 https://dart.googlesource.com/markdown.git/+/37951d1 Fix an ImageSyntax issue (#493)
 https://dart.googlesource.com/markdown.git/+/29196b7 Refactor CodeBlockSyntax (#488)
 https://dart.googlesource.com/markdown.git/+/32c52d6 benchmark: use Stopwatch over DateTime for benchmarking (#491)
 https://dart.googlesource.com/markdown.git/+/0a7a531 Refactor ATX headings (#486)
 https://dart.googlesource.com/markdown.git/+/2fab846 some cleanup (#489)
 https://dart.googlesource.com/markdown.git/+/816dd87 Refactor CodeSyntax (#487)
 https://dart.googlesource.com/markdown.git/+/a529e58 Fix HTML escape issues (#484)

```

Diff: https://dart.googlesource.com/markdown.git/+/a70b93a1a004cc1da04a3e3ea59821ef8391e0d6~..37951d151750acfae756b2e466f563c1c5119b3d/
Change-Id: I3f8419477df2280801be3cf9251e003f6c482984
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/270224
Commit-Queue: Samuel Rawlins <[email protected]>
Auto-Submit: Devon Carew <[email protected]>
Commit-Queue: Devon Carew <[email protected]>
Reviewed-by: Samuel Rawlins <[email protected]>
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries.
Projects
None yet
Development

No branches or pull requests

2 participants