-
-
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
Add rounding mode for #round
methods
#10228
Comments
So I've have a more refined proposal. Most important step is to figure out how to name things, in this case: rounding modes. Julia's
I guess before deciding on names, there's also the question about which ones should be included. I doubt we should support all of them. Even if it's not hard to implement, some of the EDIT: Added rounding modes defined in IEEE-754 to the overview. |
@straight-shoota I'd suggest to use Ruby names. |
Ruby has only three modes: I also like "nearest" over "half" because it clearly expresses that it generally chooses the nearest value and the specifics only regard tie braking at midpoint. |
I was referring to
|
Ah, you mean https://ruby-doc.org/stdlib-3.0.0/libdoc/bigdecimal/rdoc/BigDecimal.html#method-c-mode. That link would've helped ;) Still, I'd strongly suggest to avoid up/down for clarity. |
As another reference I added the rounding modes defined by IEEE-754 to the overview. For now, I would only implement the main rounding modes present in all four references. It seems to be a relatively clear distinction between common modes (= supported by all) and uncommon ones (= supported by only 1 or 2 libraries). The common ones are also supported as LLVM intrinsics. Regarding naming, I also think that Now this is my updated proposal:
|
In the process of adding rounding methods to
BigDecimal
, it became evident that existing#round
methods (which it should be compatible with) lack a rounding mode option. See discussion in #7126 (comment) ff.The prototype definition is
Number#round(digits = 0, base = 10) : self
. The implementations onInt
,Float32
andFloat64
are performance-oriented overrides with default arguments. There is alsoComplex#round(digits = 0) : self
.#7126 proposes the introduction of rounding modes to determine rounding behaviour for half steps.
This should be supported by
Number#round
as well.For reference, the respective methods in Ruby are
Float#round
andInteger#round
with the signatureround([ndigits] [, half: mode]) → integer or float
and support rounding mode option as well.The discussion in #7126 also touches the general usefulness of
base
argument and supportingbase
adds more complexity for implementing rounding modes. I agree that there seems little value to this options which isn't available in Ruby and other languages.But I think potential removal of
base
argument should be a separate discussion. The current implementation ofNumber#round(digits = 0, base = 10)
can just keep existing next to a newNumber#round(digits = 0, mode = :up)
, so there would be no mixing ofbase
andmode
options.Related: #5714
The text was updated successfully, but these errors were encountered: