6 #ifndef VNIX_RAT_NORMALIZED_PAIR_HPP 7 #define VNIX_RAT_NORMALIZED_PAIR_HPP 10 #include <vnix/gcd.hpp> 11 #include <vnix/int-types.hpp> 31 using S =
typename types::SF;
32 using U =
typename types::UF;
40 constexpr static std::pair<S, U> pair(S n, S d) {
41 U
const g = gcd(n, d);
42 if (d < 0) {
return {-n / g, -d / g}; }
43 return {n / g, d / g};
58 NMAX = U(1) << (NMR_BITS - 1),
59 DMAX = U(1) << (DNM_BITS)
61 if (dd == 0) {
throw "null denominator (division by zero)"; }
62 if (
n() >= NMAX) {
throw "numerator too large and positive"; }
63 if (
n() < -NMAX) {
throw "numerator too large and negative"; }
64 if (
d() > +DMAX) {
throw "denominator too large"; }
67 constexpr S
n()
const {
return pair_.first; }
68 constexpr U
d()
const {
return pair_.second; }
constexpr S n() const
Normalized numerator.
Numerator and denominator of a rational number as separate numbers, not encoded into the same word...
constexpr normalized_pair(S nn, S dd)
Initialize normalized numerator and denominator for encoding of rational number.
Smallest integer types for holding certain number of bits.
std::pair< S, U > pair_
Normalized numerator and denominator.
Thomas E. Vaughan's public software.
constexpr U d() const
Normalized denominator.
Classes and functions supporting a model of a fixed-precision rational number.