Skip to content

Conversation

@chiphogg
Copy link
Member

@chiphogg chiphogg commented May 3, 2025

We add a new base, au::Negative. This always goes at the beginning of
the pack. We define rational powers such that Negative disappears
when 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 them
both, and delegate to the product of what remains.

This brings new magnitude traits: Abs for the absolute value, and
IsPositive to tell whether a magnitude is positive: formerly, these
would 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't
happen, 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. We
always 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.

We add a new base, `au::Negative`.  This always goes at the beginning of
the pack.  We define rational powers such that `Negative` disappears
when 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 them
both, and delegate to the product of what remains.

This brings new magnitude traits: `Abs` for the absolute value, and
`IsPositive` to tell whether a magnitude is positive: formerly, these
would 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't
happen, 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.  We
always 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.
@chiphogg chiphogg added the release notes: ✨ lib (enhancement) PR enhancing the library code label May 3, 2025
Comment on lines 766 to 768
// If M is (N/D), DenominatorPartT<M> is D; we want 1/D.
template <typename M>
using NegativePowers = MagInverseT<DenominatorPartT<M>>;
using NegativePowers = MagQuotientT<M, NumeratorPartT<M>>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we update the comment?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@chiphogg chiphogg merged commit 103aa79 into main May 4, 2025
15 checks passed
@chiphogg chiphogg deleted the chiphogg/enable-neg-mag#370 branch May 4, 2025 00:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release notes: ✨ lib (enhancement) PR enhancing the library code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants