units
Use physical dimensions at compile-time or run-time.
Classes | Functions
vnix::rat Namespace Reference

Classes and functions supporting a model of a fixed-precision rational number. More...

Classes

struct  common_denom_params
 Least common denominator (LCD) and corresponding numerators for the comparison of a pair of rational numbers. More...
 
class  encoding
 Encoding of numerator and denominator into unsigned word. More...
 
class  normalized_pair
 Numerator and denominator of a rational number as separate numbers, not encoded into the same word. More...
 
class  rational
 Model of a fixed-precision rational number. More...
 

Functions

template<unsigned NB1, unsigned DB1, unsigned NB2, unsigned DB2>
constexpr bool operator== (rational< NB1, DB1 > r1, rational< NB2, DB2 > r2)
 Compare rationals for equality. More...
 
template<unsigned NB1, unsigned DB1, unsigned NB2, unsigned DB2>
constexpr bool operator!= (rational< NB1, DB1 > r1, rational< NB2, DB2 > r2)
 Compare rationals for inequality. More...
 
template<unsigned NB1, unsigned DB1, unsigned NB2, unsigned DB2>
constexpr bool operator<= (rational< NB1, DB1 > r1, rational< NB2, DB2 > r2)
 Compare for less-than-or-equal ordering with another rational. More...
 
template<unsigned NB1, unsigned DB1, unsigned NB2, unsigned DB2>
constexpr bool operator< (rational< NB1, DB1 > r1, rational< NB2, DB2 > r2)
 Compare for less-than ordering with another rational. More...
 
template<unsigned NB1, unsigned DB1, unsigned NB2, unsigned DB2>
constexpr bool operator>= (rational< NB1, DB1 > r1, rational< NB2, DB2 > r2)
 Compare for greater-than-or-equal ordering with another rational. More...
 
template<unsigned NB1, unsigned DB1, unsigned NB2, unsigned DB2>
constexpr bool operator> (rational< NB1, DB1 > r1, rational< NB2, DB2 > r2)
 Compare for greater-than ordering with another rational. More...
 
template<unsigned NB, unsigned DB>
constexpr auto operator+ (rational< NB, DB > r)
 Copy rational number by way of unary operator+. More...
 
template<unsigned NB, unsigned DB>
constexpr auto operator- (rational< NB, DB > r)
 Negate rational number. More...
 
template<unsigned NB1, unsigned DB1, unsigned NB2, unsigned DB2>
constexpr auto operator+ (rational< NB1, DB1 > r1, rational< NB2, DB2 > r2)
 Sum of two rational numbers. More...
 
template<unsigned NB1, unsigned DB1, unsigned NB2, unsigned DB2>
constexpr auto operator- (rational< NB1, DB1 > r1, rational< NB2, DB2 > r2)
 Difference between two rational numbers. More...
 
template<unsigned NB1, unsigned DB1, unsigned NB2, unsigned DB2>
constexpr auto operator* (rational< NB1, DB1 > r1, rational< NB2, DB2 > r2)
 Product of two rational numbers. More...
 
template<unsigned NB1, unsigned DB1, unsigned NB2, unsigned DB2>
constexpr auto operator/ (rational< NB1, DB1 > r1, rational< NB2, DB2 > r2)
 Quotient of two rational numbers. More...
 
template<unsigned NB1, unsigned DB1>
constexpr auto operator/ (rational< NB1, DB1 > r1, typename rational< NB1, DB1 >::stype r2)
 Quotient of two rational numbers. More...
 
template<unsigned NB, unsigned DB>
std::ostream & operator<< (std::ostream &s, rational< NB, DB > r)
 Print rational number to output-stream. More...
 
template<unsigned NB1, unsigned DB1, unsigned NB2, unsigned DB2>
constexpr common_denom_params< NB1, DB1, NB2, DB2 > common_denom (rational< NB1, DB1 > r1, rational< NB2, DB2 > r2)
 Compute least common denominator and corresponding numerators for the comparison of a pair of rational numbers. More...
 

Detailed Description

Classes and functions supporting a model of a fixed-precision rational number.

Function Documentation

template<unsigned NB1, unsigned DB1, unsigned NB2, unsigned DB2>
constexpr common_denom_params<NB1, DB1, NB2, DB2> vnix::rat::common_denom ( rational< NB1, DB1 >  r1,
rational< NB2, DB2 >  r2 
)

Compute least common denominator and corresponding numerators for the comparison of a pair of rational numbers.

Allow common_denom() to call private constructor.

Template Parameters
NB1Number of bits for numerator of first rational.
DB1Number of bits for denominator of first rational.
NB2Number of bits for numerator of second rational.
DB2Number of bits for denominator of second rational.
Parameters
r1First input rational number.
r2Second input rational number.
Returns
Instance of common_denom_pairs.

Definition at line 35 of file common-denom.hpp.

35  {
36  using cdp = common_denom_params<NB1, DB1, NB2, DB2>;
37  using UF1 = typename int_types<DB1>::UF;
38  using UF2 = typename int_types<DB2>::UF;
39  using gcd_t = gcd_promoted<UF1, UF2>;
40  gcd_t const g = gcd(r1.d(), r2.d()); // GCD of input denominators.
41  UF1 const d1g = r1.d() / g; // First input denominator divided by g.
42  UF2 const d2g = r2.d() / g; // Second input denominator divided by g.
43  return cdp(d1g * r2.d(), r1.n() * d2g, r2.n() * d1g);
44 };
constexpr gcd_promoted< A, B > gcd(A a, B b)
Greatest common divisor of two numbers; only absolute values are used.
Definition: gcd.hpp:40
constexpr auto g
Constant-expression symbol for g.
Definition: units.hpp:1759

Here is the caller graph for this function:

template<unsigned NB1, unsigned DB1, unsigned NB2, unsigned DB2>
constexpr bool vnix::rat::operator!= ( rational< NB1, DB1 >  r1,
rational< NB2, DB2 >  r2 
)

Compare rationals for inequality.

Template Parameters
NB1Number of numerator -bits in left -hand rational.
DB1Number of denominator-bits in left -hand rational.
NB2Number of numerator -bits in right-hand rational.
DB2Number of denominator-bits in right-hand rational.
Parameters
r1Left -hand operand.
r2Right-hand operand.

Definition at line 141 of file rational.hpp.

141  {
142  return !(r1 == r2);
143 }

Here is the caller graph for this function:

template<unsigned NB1, unsigned DB1, unsigned NB2, unsigned DB2>
constexpr auto vnix::rat::operator* ( rational< NB1, DB1 >  r1,
rational< NB2, DB2 >  r2 
)

Product of two rational numbers.

Template Parameters
NB1Number of numerator -bits in left -hand rational.
DB1Number of denominator-bits in left -hand rational.
NB2Number of numerator -bits in right-hand rational.
DB2Number of denominator-bits in right-hand rational.
Parameters
r1Multiplicand.
r2Multiplier.
Returns
Product.

Definition at line 278 of file rational.hpp.

278  {
279  auto const n1 = r1.n();
280  auto const n2 = r2.n();
281  auto const d1 = r1.d();
282  auto const d2 = r2.d();
283  auto const ga = gcd(n1, d2);
284  auto const gb = gcd(n2, d1);
285  enum { NB = (NB1 > NB2 ? NB1 : NB2), DB = (DB1 > DB2 ? DB1 : DB2) };
286  using S = typename int_types<NB>::SF;
287  using U = typename int_types<DB>::UF;
288  return rational<NB, DB>(S(n1) / ga * n2 / gb, U(d1) / gb * d2 / ga);
289 }
constexpr gcd_promoted< A, B > gcd(A a, B b)
Greatest common divisor of two numbers; only absolute values are used.
Definition: gcd.hpp:40

Here is the caller graph for this function:

template<unsigned NB, unsigned DB>
constexpr auto vnix::rat::operator+ ( rational< NB, DB >  r)

Copy rational number by way of unary operator+.

Template Parameters
NBNumber of bits for numerator.
DBNumber of bits for denominator.
Parameters
rNumber to copy.
Returns
Copy of number.

Definition at line 206 of file rational.hpp.

206  {
207  return r;
208 }

Here is the caller graph for this function:

template<unsigned NB1, unsigned DB1, unsigned NB2, unsigned DB2>
constexpr auto vnix::rat::operator+ ( rational< NB1, DB1 >  r1,
rational< NB2, DB2 >  r2 
)

Sum of two rational numbers.

Template Parameters
NB1Number of numerator -bits in left -hand rational.
DB1Number of denominator-bits in left -hand rational.
NB2Number of numerator -bits in right-hand rational.
DB2Number of denominator-bits in right-hand rational.
Parameters
r1Addend.
r2Adder.
Returns
Sum.

Definition at line 231 of file rational.hpp.

231  {
232  auto const c = common_denom(r1, r2);
233  return rational<c.NMR_BITS, c.LCD_BITS>(c.n1 + c.n2, c.lcd);
234 }
constexpr common_denom_params< NB1, DB1, NB2, DB2 > common_denom(rational< NB1, DB1 > r1, rational< NB2, DB2 > r2)
Compute least common denominator and corresponding numerators for the comparison of a pair of rationa...
rat::rational< NB, DB > rational
Bring class rational into vnix namespace.
Definition: rat.hpp:17

Here is the caller graph for this function:

template<unsigned NB, unsigned DB>
constexpr auto vnix::rat::operator- ( rational< NB, DB >  r)

Negate rational number.

Template Parameters
NBNumber of bits for numerator.
DBNumber of bits for denominator.
Parameters
rNumber to negate.
Returns
Negative of number.

Definition at line 217 of file rational.hpp.

217  {
218  return rational<NB, DB>(-r.n(), r.d());
219 }

Here is the caller graph for this function:

template<unsigned NB1, unsigned DB1, unsigned NB2, unsigned DB2>
constexpr auto vnix::rat::operator- ( rational< NB1, DB1 >  r1,
rational< NB2, DB2 >  r2 
)

Difference between two rational numbers.

Template Parameters
NB1Number of numerator -bits in left -hand rational.
DB1Number of denominator-bits in left -hand rational.
NB2Number of numerator -bits in right-hand rational.
DB2Number of denominator-bits in right-hand rational.
Parameters
r1Minuend.
r2Subtrahend.
Returns
Difference.

Definition at line 255 of file rational.hpp.

255  {
256  return -r2 + r1;
257 }

Here is the caller graph for this function:

template<unsigned NB1, unsigned DB1, unsigned NB2, unsigned DB2>
constexpr auto vnix::rat::operator/ ( rational< NB1, DB1 >  r1,
rational< NB2, DB2 >  r2 
)

Quotient of two rational numbers.

Template Parameters
NB1Number of numerator -bits in left -hand rational.
DB1Number of denominator-bits in left -hand rational.
NB2Number of numerator -bits in right-hand rational.
DB2Number of denominator-bits in right-hand rational.
Parameters
r1Dividend.
r2Divisor.
Returns
Quotient.

Definition at line 301 of file rational.hpp.

301  {
302  return r1 * r2.reciprocal();
303 }

Here is the caller graph for this function:

template<unsigned NB1, unsigned DB1>
constexpr auto vnix::rat::operator/ ( rational< NB1, DB1 >  r1,
typename rational< NB1, DB1 >::stype  r2 
)

Quotient of two rational numbers.

Template Parameters
NB1Number of numerator -bits in left -hand rational.
DB1Number of denominator-bits in left -hand rational.
Parameters
r1Dividend.
r2Divisor.
Returns
Quotient.

Definition at line 313 of file rational.hpp.

314  {
315  return r1 * rational<NB1, DB1>(r2).reciprocal();
316 }

Here is the caller graph for this function:

template<unsigned NB1, unsigned DB1, unsigned NB2, unsigned DB2>
constexpr bool vnix::rat::operator< ( rational< NB1, DB1 >  r1,
rational< NB2, DB2 >  r2 
)

Compare for less-than ordering with another rational.

Template Parameters
NB1Number of numerator -bits in left -hand rational.
DB1Number of denominator-bits in left -hand rational.
NB2Number of numerator -bits in right-hand rational.
DB2Number of denominator-bits in right-hand rational.
Parameters
r1Left -hand operand.
r2Right-hand operand.

Definition at line 168 of file rational.hpp.

168  {
169  auto const c = common_denom(r1, r2);
170  return c.n1 < c.n2;
171 }
constexpr common_denom_params< NB1, DB1, NB2, DB2 > common_denom(rational< NB1, DB1 > r1, rational< NB2, DB2 > r2)
Compute least common denominator and corresponding numerators for the comparison of a pair of rationa...

Here is the caller graph for this function:

template<unsigned NB, unsigned DB>
std::ostream& vnix::rat::operator<< ( std::ostream &  s,
rational< NB, DB >  r 
)

Print rational number to output-stream.

Template Parameters
NBNumber of bits for numerator of rational.
DBNumber of bits for denominator of rational.
Parameters
sReference to output-stream.
rRational number.
Returns
Reference to modified output-stream.

Definition at line 344 of file rational.hpp.

344  {
345  s << int_fast64_t(r.n());
346  if (r.d() != 1) { s << '/' << uint_fast64_t(r.d()); }
347  return s;
348 }
constexpr auto s
Constant-expression symbol for s.
Definition: units.hpp:1825
template<unsigned NB1, unsigned DB1, unsigned NB2, unsigned DB2>
constexpr bool vnix::rat::operator<= ( rational< NB1, DB1 >  r1,
rational< NB2, DB2 >  r2 
)

Compare for less-than-or-equal ordering with another rational.

Template Parameters
NB1Number of numerator -bits in left -hand rational.
DB1Number of denominator-bits in left -hand rational.
NB2Number of numerator -bits in right-hand rational.
DB2Number of denominator-bits in right-hand rational.
Parameters
r1Left -hand operand.
r2Right-hand operand.

Definition at line 154 of file rational.hpp.

154  {
155  auto const c = common_denom(r1, r2);
156  return c.n1 <= c.n2;
157 }
constexpr common_denom_params< NB1, DB1, NB2, DB2 > common_denom(rational< NB1, DB1 > r1, rational< NB2, DB2 > r2)
Compute least common denominator and corresponding numerators for the comparison of a pair of rationa...

Here is the caller graph for this function:

template<unsigned NB1, unsigned DB1, unsigned NB2, unsigned DB2>
constexpr bool vnix::rat::operator== ( rational< NB1, DB1 >  r1,
rational< NB2, DB2 >  r2 
)

Compare rationals for equality.

Template Parameters
NB1Number of numerator -bits in left -hand rational.
DB1Number of denominator-bits in left -hand rational.
NB2Number of numerator -bits in right-hand rational.
DB2Number of denominator-bits in right-hand rational.
Parameters
r1Left -hand operand.
r2Right-hand operand.

Definition at line 128 of file rational.hpp.

128  {
129  return r1.n() == r2.n() && r1.d() == r2.d();
130 }

Here is the caller graph for this function:

template<unsigned NB1, unsigned DB1, unsigned NB2, unsigned DB2>
constexpr bool vnix::rat::operator> ( rational< NB1, DB1 >  r1,
rational< NB2, DB2 >  r2 
)

Compare for greater-than ordering with another rational.

Template Parameters
NB1Number of numerator -bits in left -hand rational.
DB1Number of denominator-bits in left -hand rational.
NB2Number of numerator -bits in right-hand rational.
DB2Number of denominator-bits in right-hand rational.
Parameters
r1Left -hand operand.
r2Right-hand operand.

Definition at line 195 of file rational.hpp.

195  {
196  return !(r1 <= r2);
197 }

Here is the caller graph for this function:

template<unsigned NB1, unsigned DB1, unsigned NB2, unsigned DB2>
constexpr bool vnix::rat::operator>= ( rational< NB1, DB1 >  r1,
rational< NB2, DB2 >  r2 
)

Compare for greater-than-or-equal ordering with another rational.

Template Parameters
NB1Number of numerator -bits in left -hand rational.
DB1Number of denominator-bits in left -hand rational.
NB2Number of numerator -bits in right-hand rational.
DB2Number of denominator-bits in right-hand rational.
Parameters
r1Left -hand operand.
r2Right-hand operand.

Definition at line 182 of file rational.hpp.

182  {
183  return !(r1 < r2);
184 }

Here is the caller graph for this function: