-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Implement rounding mode for Number#round
#10413
Implement rounding mode for Number#round
#10413
Conversation
Looks good. I would feel more comfortable with ceiling/floor. I think they are more familiar names. But I guess that sticking with IEEE-754 is fine. |
09c5da9
to
93a77f0
Compare
The failing spec depends on #10413. |
I guess you meant #10420, right? |
|
||
# Specifies rounding behaviour for numerical operations capable of discarding | ||
# precision. | ||
enum RoundingMode |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd move it to the beginning of the file for a better discoverability.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What difference does it make? I think it should stay in proximity to the methods that use it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could even consider extracting all features related to rounding into a separate file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could even consider extracting all features related to rounding into a separate file.
Sounds even better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's leave that for a follow-up.
This breaks require "big"
BigFloat.new("1.23").round # Error: undefined local variable or method 'round_away' for BigFloat I'm not sure if anything can be done about this, because GMP doesn't implement |
It will not be fixed because GMP straight up doesn't define these rounding modes, and we cannot implement them for
|
Oh, well. Then there is probably nothing we can do? The error is probably good then. You can't use |
This patch adds
Number#round(RoundingMode)
which enables to chose rounding behaviour at runtime.Supported rounding modes (per #10228 (comment)):
TO_ZERO
TO_POSITIVE
TO_NEGATIVE
TIES_EVEN
TIES_AWAY
Implementations were already available as dedicated methods for all modes except
TIES_EVEN
which is added as#round_even
.#round_away
is the explicitly named implementation ofTIES_AWAY
mode previsouly just named#round
.#round
without arguments now delegates to#round_away
and the default rounding mode isTIES_AWAY
for keeping backward compatibility.Resolves #10228
There's already #7126 waiting with the implementation for big numbers (it will require some updates, though).