-
Notifications
You must be signed in to change notification settings - Fork 23
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
Provide more flexible alternative to integer_quotient() #253
Comments
I got a local implementation done a couple weeks ago, using the name constexpr auto t1 = hours(5);
constexpr auto t2 = minutes(120);
constexpr auto from_quotient_and_remainder = (t1 / t2) * t2 + (t1 % t2); Currently, the
Put it all together, and we get When we previously discussed this example, I thought that the best way to handle each of division and modulus individually was crystal clear, but when you put them together, they don't satisfy the quotient-remainder theorem. My solution was to propose a separate I still think that function should exist, but I've realized now that there are two different modulus operations, when it comes to units. There's the "quantity-like" modulus operation, which is very clearly a common-unit operation, and is equivalent to what's left over from repeated subtraction. And then there's the "quotient-and-remainder-like" modulus operation, which holds whatever we need in order to make (integer) division invertible. And since division is an arbitrary-unit operation, then so is this modulus operation. Let's look at some examples to show how this new modulus operation would work. I'm assuming we'll call the utility something like
We see that In fact, we get more than I had expected. I was assuming we would always recover the correct quantity, although the units might be different. (This was based on intuition from the quantity-like modulus, where So I think I'll try to make |
Putting this in the 0.4.0 milestone so that we can begin the deprecation pipeline for |
I have thought through the naming issues more thoroughly now. I don't think we need to block this issue on finding a name that properly conveys both integer division and "q-r-like modulus". Yes, these are related operations. But they are not the same thing, and trying to give them the same name would likely just cause unnecessary confusion.
We'll need some other name if we decide to provide this second form of modulus (we haven't seen any actual use cases yet). Maybe t1 == (t1 / unblock_int_div(t2)) * t2 + t1 % use_raw_mod(t2); |
Instead of `integer_quotient(a, b)`, we now always write `a / unblock_int_div(b)`. This gives us everything we used to have with `integer_quotient`, but with two big advantages: 1. The form for the units code becomes more similar to the non-units code that it replaces (i.e., we're using the `/` symbol). 2. We can now support _templated code_ that works for both integral and non-integral types: `a / unblock_int_div(b)` works equally well for, say, `b` with floating point rep. There's no longer any use case for `integer_quotient`. We therefore immediately deprecate it. We will keep it deprecated for the entirety of the 0.4.0 cycle, and remove it for 0.5.0. Fixes #253.
Instead of `integer_quotient(a, b)`, we now always write `a / unblock_int_div(b)`. This gives us everything we used to have with `integer_quotient`, but with two big advantages: 1. The form for the units code becomes more similar to the non-units code that it replaces (i.e., we're using the `/` symbol). 2. We can now support _templated code_ that works for both integral and non-integral types: `a / unblock_int_div(b)` works equally well for, say, `b` with floating point rep. There's no longer any use case for `integer_quotient`. We therefore immediately deprecate it. We will keep it deprecated for the entirety of the 0.4.0 cycle, and remove it for 0.5.0. Fixes #253.
The point of
integer_quotient()
is to make it extremely obvious that we are about to do a possibly-truncating integer division. We have seen that this is very error prone when the denominator has units, and when those units are not quantity-equivalent to the numerator's units. So it's good that we've put up this guard rail.However,
integer_quotient()
is very inflexible. For one thing, it's horrible for generic programming: there's no real way to write a division operation that sometimes uses integers (and you're OK with the truncation), but sometimes uses floating point. For another, it looks completely different to the division operation.I think we can get all of the safety benefits of
integer_quotient()
by introducing a wrapper type for the denominator. I don't know what the name should be yet, but it would be something like this:The text was updated successfully, but these errors were encountered: