Support negative magnitudes #402
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
We add a new base,
au::Negative. This always goes at the beginning ofthe pack. We define rational powers such that
Negativedisappearswhen the numerator (expressed in lowest terms) is even; otherwise, it
remains if the denominator is odd, and we produce a hard compiler error
if the denominator is even. For products, the usual machinery works in
most cases, but if both inputs have
Negative, then we remove themboth, and delegate to the product of what remains.
This brings new magnitude traits:
Absfor the absolute value, andIsPositiveto tell whether a magnitude is positive: formerly, thesewould have been the identity, and trivially true, respectively.
For obtaining magnitude representations, we add a new error state
outcome,
ERR_NEGATIVE_NUMBER_IN_UNSIGNED_TYPE. If this doesn'thappen, we simply delegate to computing the magnitude of the absolute
value, and negate the result. This does mean we won't produce an answer
for the minimum value of signed integral types, but we can complete that
later.
Finally, we support magnitude labels by prepending a
-sign. Wealways prepend this sign, even for unlabeled magnitudes, because it's
important to convey sign information.
Note that this change enables users to create broken units and
quantities. If they use negative magnitudes with units, it will not
produce correct results. But given that (a) nobody has ever been able
to form these magnitudes before, (b) the functionality is undocumented,
and (c) we will follow up very soon with changes that restore this
functionality, this does not seem like a serious concern.
Helps #370.