Exact rationals with an IEEE sign #
A rational number has no negative zero, but every IEEE binary format does. Converting a value
through Rat therefore forgets whether a zero was -0 or +0. A cast or mixed-format
operation cannot recover that sign from the rational value alone.
SignedRat is a rational together with the sign bit an IEEE format would store for it. For a
nonzero value the sign bit is determined by the value, and the structure carries that fact as a
proof field, so two SignedRats are equal exactly when their values and sign bits agree. Only a
zero has a free sign bit.
The arithmetic operations implement the IEEE sign rules for exact results. A product or quotient
has the exclusive-or of the operand signs. An exact zero sum is negative only when both operands
are negative, which is the round-to-nearest rule. Callers use addWithCancellationSign to choose
the sign of an exact cancellation for directed rounding.
Executable conversion uses this type as the exact domain of every binary destination. Rational
sources such as posits and fixed point enter through ofRat, and binary sources decode through
ofDyadic, which keeps the dyadic sign.
An exact rational together with the IEEE sign bit of its zero.
negative is the sign bit. For a nonzero value it is forced to decide (value < 0); for zero it
distinguishes -0 from +0.
- value : ℚ
The exact rational value; zero for both signed zeros.
- negative : Bool
The IEEE sign bit.
A nonzero value determines its sign bit.
Instances For
Instances For
Numerical equality, ignoring the sign attached to zero.
Ordinary == remains structural, just like =. Use sameValue only when positive and negative
zero should represent the same rational number.
Instances For
A negative zero prints as -0; every other value prints as its rational value.
Instances For
Constructors #
The signed rational of a rational; zero receives the positive sign.
Instances For
Negative zero, the value a signed rational adds to Rat.
Instances For
Negative zero has value zero.
The zero of SignedRat has value zero.
Both signed-zero representations are mathematical zero for checked exact arithmetic.
The generic executable zero test recognizes exactly the zero-valued signed rationals.
Failing the generic zero test means that the rational value is nonzero.
The signed rational of an exact dyadic, keeping the dyadic sign.
Dyadic.toRat forgets the sign of a zero dyadic; this constructor is the sign-preserving
replacement used when a binary format is decoded.
Instances For
Arithmetic #
Negation flips the sign bit, including the sign of zero.
Instances For
Exact addition with the round-to-nearest sign rule for an exact zero sum.
A nonzero sum takes the sign of its value. A zero sum is negative only when both operands are
negative, so -0 + -0 = -0 while x + -x = +0 and -0 + +0 = +0.
Instances For
Exact addition with a caller-selected sign for cancellation between opposite signs.
Set negativeCancellation for rounding toward negative infinity. Same-sign zeros keep their
sign in either mode: +0 + +0 = +0 and -0 + -0 = -0. A nonzero sum always takes the sign of
its rational value.
Instances For
Choosing a cancellation sign leaves the exact rational sum unchanged.
Positive cancellation uses the ordinary round-to-nearest signed-rational addition.
Subtraction is addition of the negation, as in IEEE 754.
Instances For
Exact multiplication; the sign bit is the exclusive-or of the operand sign bits.
Instances For
Exact division; the sign bit is the exclusive-or of the operand sign bits.
Division by a zero value returns a zero value, following Rat; callers reject zero divisors
before quantizing.