-
Notifications
You must be signed in to change notification settings - Fork 24.5k
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
Fix float round to zero #37204
Fix float round to zero #37204
Conversation
Signed-off-by: Chris Le Sueur <chris.lesueur@unipart.io>
Signed-off-by: Chris Le Sueur <chris.lesueur@unipart.io>
49822e4
to
eab36f7
Compare
Woa, that's a very opinionated change of semantics of our API. The doc is pretty clear that It's not clear to me that we want to give callers the illusion that there is such a thing as "infinite precision" with floats, or that they can somehow decide to ignore rounding problems and still use And then of course we have to keep in mind that there are many users in production on 12.0, and they could rely (even unknowingly) on the current behavior. This can't be considered a bugfix, so it would have to target [1] https://github.com/odoo/odoo/pull/37204/files#diff-1853706ffae19f724b24c22c740de41bL47 |
Perhaps you could illustrate your change with a supporting use case? That would help to understand the rationale :-) |
Thanks for looking at this.
If this is really the intention, then these functions should all raise an exception if passed zero, or more digits than can be represented by a non-zero precision. Silently saying that zero is not zero is not correct, no matter what the docstring says. The docstring also does not cover the possibility that an end-user provides, say,
Float imprecision is understood by every programmer; this is not a reason to to make functions less useful. Rounding functions should behave similarly to the
Either these functions should be behaving mathematically correctly or they should be raising errors. This is a bugfix, but I can rebase against master if preferred since it's of course not backwards incompatible.
This came up for us when a unit of measure on a model was unset and we were taking the rounding value from the UoM; when unset this gives you 0. Of course there are other ways of working around this issue (as we have already implemented) but they all involve making calling code more complex for no good reason - you are asking that everything that calls float util functions with precision determined (directly or otherwise) by user input make a special check for something that the underlying function could handle correctly. As was already pointed out, users of this code may already be calling it without making this check. Summary:
|
Dear @fish-face, Thank you for your contribution but the version 12.0 is no longer supported. We apology if we could not look at your request in time. This is an automated message. |
Description of the issue/feature this PR addresses:
precision_rounding = 0
should mean "as much precision as possible", and not causeunexpected incorrect calculations. In particular, rounding with a precision of 0 should not
change the input. The same is true if a huge number of digits of precision are requested
(over 323). There is also no situation where it makes sense to say that the literal value
0 is not zero, regardless of how much precision is requested.
This fixes
float_round
andfloat_is_zero
for both of these issues.An alternative would be to raise a
ValueError
if givenprecision_rounding
of zero, or avalue of
precision_digits
high enough to have the same effect. I see no advantage inthat unless it is suspected that many users of these functions are already passing in a
value of zero, which hopefully is not the case, since they'll be getting wrong outputs!
Current behavior before PR:
Passing 0 results in rounding everything to zero and declaring that 0 is not zero
Desired behavior after PR is merged:
Passing 0 will result in no rounding, and correctly reporting that 0 is zero.
--
I confirm I have signed the CLA and read the PR guidelines at www.odoo.com/submit-pr