|
morph 0.1.0
Asynchronous bridge between GUI thread and business models
|
Exact rational number with a runtime decimal-precision tag. More...
#include <rational.hpp>
Classes | |
| struct | Wire |
| Flat JSON representation used by the Glaze codec below. More... | |
Public Member Functions | |
| constexpr | Rational () noexcept=default |
| Default-constructs the canonical zero (0/1) at precision 1. | |
| constexpr | Rational (std::int64_t whole, DecimalPlaces wantedPrecision) noexcept |
| Constructs from a whole integer at the given precision. | |
| constexpr | Rational (Numerator wantedNumerator, Denominator wantedDenominator, DecimalPlaces wantedPrecision) noexcept |
| Constructs from explicit numerator/denominator, then canonicalises. | |
| constexpr DecimalPlaces | getDecimalPlaces () const noexcept |
| The value's current decimal precision. | |
| constexpr bool | isZero () const noexcept |
| Whether the value equals 0/1. | |
| constexpr bool | isInteger () const noexcept |
| Whether the value is an integer. | |
| constexpr bool | isNegative () const noexcept |
| Whether the value is strictly less than zero. | |
| double | toDouble () const noexcept |
| Converts to double, rounded to this value's decimalPlaces. | |
| double | toDouble (std::uint32_t requestedDecimalPlaces) const noexcept |
Converts to double, rounded to requestedDecimalPlaces. | |
| constexpr Rational | operator- () const noexcept |
| Negates. | |
| constexpr std::expected< Rational, RationalError > | reciprocal () const noexcept |
| Multiplicative inverse. | |
| constexpr std::strong_ordering | operator<=> (const Rational &other) const noexcept |
| Three-way comparison. Value-only: ignores decimalPlaces. | |
| constexpr bool | operator== (const Rational &other) const noexcept |
| Equality. Value-only: ignores decimalPlaces. | |
| constexpr Rational & | operator+= (const Rational &rhs) noexcept |
| In-place addition. Result precision becomes max of the two. Uses reduce-before-multiply to extend the safe int64 range (Knuth 4.5.1). | |
| constexpr Rational & | operator-= (const Rational &rhs) noexcept |
| In-place subtraction. Result precision becomes max of the two. | |
| constexpr Rational & | operator*= (const Rational &rhs) noexcept |
| In-place multiplication. Result precision becomes max of the two. Cross-cancels common factors before multiplying. | |
| constexpr std::expected< Rational, RationalError > | dividedBy (const Rational &rhs) const noexcept |
| Non-throwing division. Result precision becomes max of the two. | |
| void | setWire (Wire wire) noexcept |
| Wire-codec entry (Glaze read side): rebuilds through the canonicalising constructor, clamping what it cannot represent. | |
| Wire | getWire () const noexcept |
| Wire-codec exit (Glaze write side). | |
| constexpr void | addAssignChecked (const Rational &rhs) noexcept |
| Applies +='s arithmetic, having already proven it cannot overflow. | |
| constexpr void | subAssignChecked (const Rational &rhs) noexcept |
| Applies -='s arithmetic, having already proven it cannot overflow. | |
| constexpr void | mulAssignChecked (const Rational &rhs) noexcept |
| Applies *='s arithmetic, having already proven it cannot overflow. | |
| constexpr bool | addWouldOverflow (const Rational &rhs) const noexcept |
| Whether *this + rhs would overflow any intermediate or the result. | |
| constexpr bool | subWouldOverflow (const Rational &rhs) const noexcept |
| Whether *this - rhs would overflow any intermediate or the result. | |
| constexpr bool | mulWouldOverflow (const Rational &rhs) const noexcept |
| Whether *this * rhs would overflow, after cross-cancelling. | |
Static Public Member Functions | |
| static constexpr std::expected< Rational, RationalError > | from (Numerator wantedNumerator, Denominator wantedDenominator, DecimalPlaces wantedPrecision) noexcept |
| Validating factory. | |
| static std::expected< Rational, RationalError > | fromFloat (double value, DecimalPlaces wantedPrecision) noexcept |
Converts a double to a Rational scaled to wantedPrecision. | |
| static std::expected< Rational, RationalError > | fromFloat (float value, DecimalPlaces wantedPrecision) noexcept |
Converts a float to a Rational scaled to wantedPrecision. | |
| static std::expected< Rational, RationalError > | fromFloat (long double value, DecimalPlaces wantedPrecision) noexcept |
Converts a long double to a Rational scaled to wantedPrecision. | |
| static constexpr Rational | zero (DecimalPlaces wantedPrecision) noexcept |
| Canonical zero (0/1) at the given precision. | |
| static constexpr Rational | one (DecimalPlaces wantedPrecision) noexcept |
| Canonical one (1/1) at the given precision. | |
Public Attributes | |
| std::int64_t | numerator {0} |
| Signed numerator. Carries the sign of the rational value. | |
| std::int64_t | denominator {1} |
| Strictly positive denominator. Never zero, never negative. | |
| DecimalPlaces | decimalPlaces {1} |
| Decimal-precision tag. Invariant: 0 <= value <= kMaxDecimalPlaces. | |
Exact rational number with a runtime decimal-precision tag.
Definition at line 375 of file rational.hpp.
|
inlineconstexprnoexcept |
Constructs from a whole integer at the given precision.
| whole | The integer value; stored as whole/1. |
| wantedPrecision | Decimal precision; clamped to [0, kMaxDecimalPlaces]. |
Definition at line 391 of file rational.hpp.
|
inlineconstexprnoexcept |
Constructs from explicit numerator/denominator, then canonicalises.
A negative wantedDenominator flips the sign of wantedNumerator, the pair is reduced by gcd, and a denominator of 0 is clamped to 1. Use from for explicit zero-denominator detection.
| wantedNumerator | Signed numerator. |
| wantedDenominator | Denominator. May be negative or zero on input. |
| wantedPrecision | Decimal precision; clamped to [0, kMaxDecimalPlaces]. |
Definition at line 403 of file rational.hpp.
|
inlinestaticnodiscardconstexprnoexcept |
Validating factory.
| wantedNumerator | Signed numerator. |
| wantedDenominator | Denominator; 0 is rejected instead of clamped. |
| wantedPrecision | Decimal precision; clamped to [0, kMaxDecimalPlaces]. |
wantedDenominator is 0. Definition at line 416 of file rational.hpp.
|
inlinestaticnodiscardnoexcept |
Converts a double to a Rational scaled to wantedPrecision.
| value | Source value. |
| wantedPrecision | Decimal precision to scale to. |
Definition at line 1420 of file rational.hpp.
|
inlinestaticnodiscardnoexcept |
Converts a float to a Rational scaled to wantedPrecision.
| value | Source value. |
| wantedPrecision | Decimal precision to scale to. |
Definition at line 1425 of file rational.hpp.
|
inlinestaticnodiscardnoexcept |
Converts a long double to a Rational scaled to wantedPrecision.
| value | Source value. |
| wantedPrecision | Decimal precision to scale to. |
Definition at line 1430 of file rational.hpp.
|
inlinestaticnodiscardconstexprnoexcept |
Canonical zero (0/1) at the given precision.
| wantedPrecision | Decimal precision of the returned value. |
wantedPrecision. Definition at line 449 of file rational.hpp.
|
inlinestaticnodiscardconstexprnoexcept |
Canonical one (1/1) at the given precision.
| wantedPrecision | Decimal precision of the returned value. |
wantedPrecision. Definition at line 456 of file rational.hpp.
|
inlinenodiscardconstexprnoexcept |
The value's current decimal precision.
Definition at line 462 of file rational.hpp.
|
inlinenodiscardconstexprnoexcept |
Whether the value equals 0/1.
Definition at line 466 of file rational.hpp.
|
inlinenodiscardconstexprnoexcept |
Whether the value is an integer.
Definition at line 470 of file rational.hpp.
|
inlinenodiscardconstexprnoexcept |
Whether the value is strictly less than zero.
Definition at line 474 of file rational.hpp.
|
inlinenodiscardnoexcept |
Converts to double, rounded to this value's decimalPlaces.
Definition at line 478 of file rational.hpp.
|
inlinenodiscardnoexcept |
Converts to double, rounded to requestedDecimalPlaces.
| requestedDecimalPlaces | Number of decimal digits to keep. Values > 18 fall back to unrounded conversion. |
Definition at line 1435 of file rational.hpp.
|
inlinenodiscardconstexprnoexcept |
Negates.
Definition at line 488 of file rational.hpp.
|
inlinenodiscardconstexprnoexcept |
Multiplicative inverse.
Definition at line 494 of file rational.hpp.
|
inlinenodiscardconstexprnoexcept |
Three-way comparison. Value-only: ignores decimalPlaces.
Exact for the full int64 range: cross-products are computed in 128 bits, so the ordering is always consistent with operator==.
| other | Value to compare against. |
Definition at line 510 of file rational.hpp.
|
inlinenodiscardconstexprnoexcept |
Equality. Value-only: ignores decimalPlaces.
| other | Value to compare against. |
Definition at line 540 of file rational.hpp.
In-place addition. Result precision becomes max of the two. Uses reduce-before-multiply to extend the safe int64 range (Knuth 4.5.1).
Saturates rather than overflowing. If the exact sum – or any intermediate cross-term needed to reach it – is not representable, this logs at error and clamps to the largest magnitude of the correct sign instead of committing signed-overflow undefined behaviour. See saturateToward for why a defined wrong answer beats UB here.
| rhs | Value to add. |
Definition at line 554 of file rational.hpp.
In-place subtraction. Result precision becomes max of the two.
Saturates rather than overflowing; see operator+=.
| rhs | Value to subtract. |
Definition at line 569 of file rational.hpp.
In-place multiplication. Result precision becomes max of the two. Cross-cancels common factors before multiplying.
| rhs | Value to multiply by. |
Saturates rather than overflowing; see operator+=.
Definition at line 585 of file rational.hpp.
|
inlinenodiscardconstexprnoexcept |
Non-throwing division. Result precision becomes max of the two.
Reports a zero divisor; saturates on overflow. The expected says only whether rhs was zero. Division is multiplication by the divisor's reciprocal, and that product saturates exactly like operator*=: a quotient too large for int64_t clamps to ±INT64_MAX/1, logs at error, and is still returned as a successful expected. INT64_MAX / (1/1000000) is such a case. A caller that must not absorb a clamped quotient uses checkedDiv, which routes both failure modes through the one channel.
| rhs | Divisor. |
rhs is zero. Definition at line 603 of file rational.hpp.
|
inlinenoexcept |
Wire-codec entry (Glaze read side): rebuilds through the canonicalising constructor, clamping what it cannot represent.
den == 0, an out-of-range dp, or a component whose magnitude does not fit are clamped rather than rejected, so this never fails — which means {"num":5,"den":0,"dp":2} decodes to a perfectly plausible 5/1, and nothing downstream can tell the value was altered.
Rejecting here would be the wrong layer's call. Whether a clamped value is a protocol violation or a harmless normalisation depends on where the bytes came from, and this function cannot know. It records the fact instead, for a WireClampScope around the decode to act on.
| wire | Raw values decoded from JSON. |
Definition at line 661 of file rational.hpp.
|
inlinenodiscardnoexcept |
Wire-codec exit (Glaze write side).
Definition at line 674 of file rational.hpp.
|
inlineconstexprnoexcept |
Applies +='s arithmetic, having already proven it cannot overflow.
For checkedAdd, which has just run addWouldOverflow and must not re-enter operator+='s saturating path. Calling this without that check is undefined on overflow – the check is the precondition.
| rhs | Value to add. |
Definition at line 852 of file rational.hpp.
|
inlineconstexprnoexcept |
Applies -='s arithmetic, having already proven it cannot overflow.
| rhs | Value to subtract. |
Definition at line 856 of file rational.hpp.
|
inlineconstexprnoexcept |
Applies *='s arithmetic, having already proven it cannot overflow.
| rhs | Value to multiply by. |
Definition at line 860 of file rational.hpp.
|
inlinenodiscardconstexprnoexcept |
Whether *this + rhs would overflow any intermediate or the result.
| rhs | The addend. |
Definition at line 865 of file rational.hpp.
|
inlinenodiscardconstexprnoexcept |
Whether *this - rhs would overflow any intermediate or the result.
| rhs | The subtrahend. |
Definition at line 878 of file rational.hpp.
|
inlinenodiscardconstexprnoexcept |
Whether *this * rhs would overflow, after cross-cancelling.
Checks the cross-cancelled factors operator*= actually multiplies: cross-cancelling is what keeps most products in range, so checking the raw operands would reject pairs that multiply perfectly well.
| rhs | The factor. |
Definition at line 895 of file rational.hpp.
| std::int64_t morph::math::Rational::numerator {0} |
Signed numerator. Carries the sign of the rational value.
Definition at line 377 of file rational.hpp.
| std::int64_t morph::math::Rational::denominator {1} |
Strictly positive denominator. Never zero, never negative.
Definition at line 380 of file rational.hpp.
| DecimalPlaces morph::math::Rational::decimalPlaces {1} |
Decimal-precision tag. Invariant: 0 <= value <= kMaxDecimalPlaces.
Definition at line 383 of file rational.hpp.