Replies: 1 comment 1 reply
-
Thanks, @nebkat. That is a really interesting idea. @chiphogg, I wonder if Au has solved this issue already? We could consider adding such a function; however, there is one problem lurking here. What about the integer overflow problem? Should we always first scale If we can only come up with a generic algorithm that will work for all possible combinations of |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
When dividing integers we have to accept that truncation will occur, but sometimes our desired end-result unit would have provided better accuracy:
This is normally worked around be pre-multiplying by the target unit, and mp-units is no different:
Problem solved? Not quite - this only worked because
a / b
gavemp::one
so we didn't have to factor it out.What if we have something more complicated:
This time we can't just multiply the distance by$2.82481 \frac{mpg}{km/l}$ before dividing, as it's not an integer - but even if it was we've had to know in advance that our end result will be in
km/l
and calculate that constant ourselves, and that might not always be so obvious.We could give up and turn everything to floating points, but the factorization we need to do to save ourselves is exactly the sort of calculation that the library is best at. It can easily figure out that the constant is actually:
Essentially what we would need is something similar to std::fma or Rust's muldiv - that would create the optimal factorization before performing the final division. I was initially thinking a member function
q.divide_to<U>(d)
, but a standalone function would probably be a better fit.Beta Was this translation helpful? Give feedback.
All reactions