-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Add RoundMode to round
and fix rounding inconsistency (Decimal uses "half_even", float uses "up")
#21800
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
Comments
In think in general our >>> s = pl.Series([0.5, 1.5, 2.5, 3.5])
>>> s.round()
shape: (4,)
Series: '' [f64]
[
1.0
2.0
3.0
4.0
]
>>> s.cast(pl.Decimal(scale=1)).round()
shape: (4,)
Series: '' [decimal[*,1]]
[
0.0
2.0
2.0
4.0
] I would in theory be ok with adding a (names got redesigned a bit, see below)
as well as each mode without the However, the question is how much work it would be to implement all of these 12 rounding modes correctly... |
Great idea! I think we can go one step at a time and don't need all at once. |
@orlp sorry for being a little fast with the implementation. You are right, let uns talk about the details here first 😉 RoundModes & NamingFrom what I see in other libraries and languages it seems like the following are common Half-VersionsRounds to the nearest value with a defined "tie-breaker"
Non-half-versionsRounds in the defined "direction"
ProposalCurrently "Decimal" uses "half_even" and "float" uses "half_up" (inconsistent) If there is time and demand for more we can add that later. |
It seems there isn't a clear consensus, for example Julia uses |
okay, I get that. How about:
and
|
To reiterate, (eventually) I'd want every
Note: not |
I am unsure if Without the "half" (tie breaker) version we usually have a SINGLE direction (round always to either a higher or lower value). Problem: One option would be to keep odd numbers with remainder of zero but I would expect to end up with only even numbers. |
@Julian-J-S The number 3 would round to itself because it's already an integer.
A bit silly, but would you expect rounding with I understand that |
imo it could be a little confusing. But on the other hand this is a very niche use-case I assume so people probably know what they are doing 😆 . But okay, I think now I fully understand the idea:
Somehow when reading @orlp
I will also set default for both to be |
Please check #17798 |
round
and fix rounding inconsistency (Decimal uses "half_even", float uses "up")
@eitsupi what should I check? The linked issue only mentions to add rounding "half to even" (bankers rounding). |
So my point is that this issue is essentially a duplication of #17798. |
no, it is not 😉 This here is much more fixing the "round problem" on a higher level. The other one has wrong assumptions and problematic ideas. The other issue says:
|
Yes, that would be great :) |
Just to point out for the record, I believe round support was added to the Decimal type in Polars after that issue was created.
Half up rounding in general is feasible if adding, multiplication and ceiling can be used, and I see no reason why the round function should not do round ties even by default. |
Description
Problem (edited) ⚠
current
round
expression has no mode and is inconsistentWhy
Depending on the industry (financial, pricing, ...) it is critical to use the "correct" rounding mode.
The two most common are
There are multiple others that can be added in the future.
The text was updated successfully, but these errors were encountered: