-
Notifications
You must be signed in to change notification settings - Fork 17
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
Added intval and ceil to not loose precision during weight conversion #29
Added intval and ceil to not loose precision during weight conversion #29
Conversation
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.
Thanks for the PR! This definitely needs to be addressed because we don't want to introduce any float imprecision errors. I've left a comment on the approach. If you're okay with that let me know and I'll also roll this out on the naive pricing casts before merging.
src/Model/ShippingMethod.php
Outdated
(int) $min_weight, | ||
(int) $max_weight, |
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.
Isn't it better to use round()
for this as it corrects float calculation imprecision both ways?:
(int) $min_weight, | |
(int) $max_weight, | |
(int)round($data['min_weight'] * 1000.0), | |
(int)round($data['max_weight'] * 1000.0), |
This also inlines the operation again. If there are no additional steps involved I think introducing a new variable reduces readability.
src/Model/ShippingMethod.php
Outdated
$min_weight = intval(ceil($data['min_weight'] * 1000)); | ||
$max_weight = intval(ceil($data['max_weight'] * 1000)); | ||
|
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.
$min_weight = intval(ceil($data['min_weight'] * 1000)); | |
$max_weight = intval(ceil($data['max_weight'] * 1000)); |
I'm okay with that, see my last commit.
After a bit of thinking, I think is better to use :
So we always give a valid range of weights even if round errors occurs. |
@AlexisPPLIN We're not trying to fix rounding errors but float math precision errors. We have no say in what way the imprecision will occur, but we always know it will be close to the intended result. I still think rounding inaccuracies away is the better option for this. Consider in your proposed situation we would receive
We would I hope this managed to convince you =) |
This pull request fixes #27.
Conversion from Kg to Grams now uses
intval
andceil
.I've also added this special case to the existing test of
ShippingMethod
.Hope this will be merged soon !