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

Starlark: StarlarkInt.{floordiv,mod} now work with 64-bit integers #12667

Closed
wants to merge 1 commit into from

Conversation

stepancheg
Copy link
Contributor

  • No special case for 32-bit integers, division is slow anyway
  • Switch mod to use Math.floorMod for simplicity/safety

@@ -136,6 +136,10 @@ assert_eq( product % 1234567, 1013598)
assert_eq( product % -1234567, -220969) # ditto
assert_eq(-product % 1234567, 220969) # ditto
assert_eq(-product % -1234567, -1013598)
assert_eq(1 % maxlong, 1)
assert_eq((-1) % maxlong, maxlong - 1)
assert_eq(1 % minlong, maxlong)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is odd. Python3, go.starlark.net, and java.starlark.net (without this PR) all return -maxlong for this operation. Yet the spec says:

The // and % operations on integers compute floored division and remainder of floored division, respectively. If the signs of the operands differ, the sign of the remainder x % y matches that of the dividend, x.

This means the result should be maxlong, like you have here. Do all three implementations currently have the same bug? Or is the spec missing some subtlety?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the spec is wrong. See bazelbuild/starlark#148

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To add more confusion, this is a failing assertion, it won't fail if:

assert_eq(1 % minlong, -maxlong)

with this PR implementation. Accident when I submitted the PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(When I added tests, I tried to follow Python behavior, not the spec, but somehow made a mistake.)

* No special case for 32-bit integers, division is slow anyway
* Switch `mod` to use `Math.floorMod` for simplicity/safety
@bazel-io bazel-io closed this in 3e45967 Jan 20, 2021
@stepancheg stepancheg deleted the divmod64 branch January 20, 2021 22:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants