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

Make the% consistent across both Integer sizes. #9589

Merged
merged 9 commits into from
Apr 4, 2024

Conversation

GregoryTravis
Copy link
Contributor

@GregoryTravis GregoryTravis commented Apr 1, 2024

There is no clear consensus about what % should mean when the first operand is negative.

The current behavior is that negative Integer values that fit in a Long return a negative result, while larger Integer values return a positive result:

IO.println (-9223372036854775807)%10
IO.println (-9223372036854775808)%10
IO.println (-9223372036854775809)%10

Gives

-7
-8
1 # Expected -9 here

For consistency, we'll return negative values for all Integer values. This makes it consistent with both Java, C, and C++ %.

(This is sometimes called remainder rather than modulo, but since programmers usually call this operator modulo we'll keep the original name of the method inside BigIntegerOps.)

Closes #9553.

Checklist

Please ensure that the following checklist has been satisfied before submitting the PR:

  • The documentation has been updated, if necessary.
  • Screenshots/screencasts have been attached, if there are any visual changes. For interactive or animated visual changes, a screencast is preferred.
  • All code follows the
    Scala,
    Java,
    and
    Rust
    style guides. In case you are using a language not listed above, follow the Rust style guide.
  • All code has been tested:
    • Unit tests have been written where possible.
    • If GUI codebase was changed, the GUI was tested when built using ./run ide build.

@@ -515,6 +515,11 @@ type Float
Modulus in Enso will undergo automatic conversions such that you need
not convert between Integer and Float manually.

? Negative Arguments

When the first argument to `%` is negative, the result will also be
Copy link
Member

Choose a reason for hiding this comment

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

OK.

@@ -349,8 +349,7 @@ add_specs suite_builder =
hundred_factorial%hundred_factorial . should_equal 0
10%hundred_factorial . should_equal 10

# Pending https://github.com/enso-org/enso/issues/9553
group_builder.specify "should define modulo for negative integers and decimals" pending="large integer % is incorrect" <|
group_builder.specify "should define modulo for negative integers and decimals" <|
-5%3 . should_equal -2
-5%3 . should_be_a Integer
Copy link
Member

Choose a reason for hiding this comment

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

You may want to add more examples like those mentioned in the PR description.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Member

Choose a reason for hiding this comment

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

Where?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I misunderstood you and added Examples to the documentation. Now I have added the example cases from the PR description.

@GregoryTravis GregoryTravis added the CI: Ready to merge This PR is eligible for automatic merge label Apr 3, 2024
@jdunkerley jdunkerley added the CI: Keep up to date Automatically update this PR to the latest develop. label Apr 4, 2024
@jdunkerley jdunkerley removed the CI: Keep up to date Automatically update this PR to the latest develop. label Apr 4, 2024
@mergify mergify bot merged commit c100a88 into develop Apr 4, 2024
36 of 37 checks passed
@mergify mergify bot deleted the wip/gmt/9553-remainder-consistency branch April 4, 2024 15:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI: Ready to merge This PR is eligible for automatic merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

% for large integers is incorrect
5 participants