-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support mixed
Constant
-Quantity
math functions
Specifically, we add support for `min`, `max`, `clamp`, and `%`. It turns out that the most economical way to do this is via hidden friends. The downside is that this change is invasive to `Quantity`, whereas we'd generally rather add functionality from the outside. But the upsides are that we get to remove a fair bit of extra special casing that we had done for `Zero` overloads, and even some disambiguating overloads. Not only that, but we can now support some combinations that we hadn't added before, simply because it would have been too much work! Here's how it works. The hidden friend approach covers us whenever somebody calls a function with two exactly-identical types, _or_ whenever _one_ of the types is an exact match, and the _other_ can _implicitly convert_ to it. This lets us cover all "shapeshifter types" --- `Zero`, `Constant` --- at one stroke. It even automatically covers _new shapeshifter types we don't know about_: anything implicitly convertible to `Quantity` will work! For the `min` and `max` implementation, I went with the Walter Brown approach where `min` prefers to return `a`, and `max` prefers `b`. This is the most general and correct approach w.r.t. how it handles "ties", although in our specific case this doesn't matter because we're not returning a reference. Still, I'm glad to put one more example of the Right Approach out in the wild, and I prefer it to a call to `std::min` because it doesn't force us to take a direct dependency on `<cmath>`. We have two "disambiguating" overloads remaining in `math.hh`, both applying to `QuantityPoint`: one for `min`, one for `max`. I decided not to add hidden friends there, because the cost of an invasive change, plus the cost of moving these implementations far from the other overloads in `math.hh`, outweighs the smaller benefits we would obtain in this case. Helps #90. At this point, the `Constant` _implementation_ is feature complete, and all we need to do is add concrete examples of `Constant` to our library, updating the single-file package script and documentation!
- Loading branch information
Showing
4 changed files
with
58 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters