units
Use physical dimensions at compile-time or run-time.
Public Member Functions | Private Types | Static Private Member Functions | Private Attributes | List of all members
vnix::rat::normalized_pair< NMR_BITS, DNM_BITS > Class Template Reference

Numerator and denominator of a rational number as separate numbers, not encoded into the same word. More...

#include <normalized-pair.hpp>

Public Member Functions

constexpr normalized_pair (S nn, S dd)
 Initialize normalized numerator and denominator for encoding of rational number. More...
 
constexpr S n () const
 Normalized numerator. More...
 
constexpr U d () const
 Normalized denominator. More...
 

Private Types

using types = int_types< NMR_BITS+DNM_BITS >
 Working integer-types. More...
 
using S = typename types::SF
 Fastest large-enough signed type. More...
 
using U = typename types::UF
 Fastest large-enough unsigned type. More...
 

Static Private Member Functions

static constexpr std::pair< S, Upair (S n, S d)
 Normalize numerator and denominator. More...
 

Private Attributes

std::pair< S, Upair_
 Normalized numerator and denominator. More...
 

Detailed Description

template<unsigned NMR_BITS, unsigned DNM_BITS>
class vnix::rat::normalized_pair< NMR_BITS, DNM_BITS >

Numerator and denominator of a rational number as separate numbers, not encoded into the same word.

These separate numbers are guaranteed to be normalized so that they can be encoded into a word of type rational_base<NMR_BITS, DNM_BITS>::US.

Template Parameters
NMR_BITSNumber of bits for numerator.
DNM_BITSNumber of bits for denominator.

Definition at line 25 of file normalized-pair.hpp.

Member Typedef Documentation

template<unsigned NMR_BITS, unsigned DNM_BITS>
using vnix::rat::normalized_pair< NMR_BITS, DNM_BITS >::S = typename types::SF
private

Fastest large-enough signed type.

Definition at line 31 of file normalized-pair.hpp.

template<unsigned NMR_BITS, unsigned DNM_BITS>
using vnix::rat::normalized_pair< NMR_BITS, DNM_BITS >::types = int_types<NMR_BITS + DNM_BITS>
private

Working integer-types.

Definition at line 30 of file normalized-pair.hpp.

template<unsigned NMR_BITS, unsigned DNM_BITS>
using vnix::rat::normalized_pair< NMR_BITS, DNM_BITS >::U = typename types::UF
private

Fastest large-enough unsigned type.

Definition at line 32 of file normalized-pair.hpp.

Constructor & Destructor Documentation

template<unsigned NMR_BITS, unsigned DNM_BITS>
constexpr vnix::rat::normalized_pair< NMR_BITS, DNM_BITS >::normalized_pair ( S  nn,
S  dd 
)
inline

Initialize normalized numerator and denominator for encoding of rational number.

Normalization includes making sure that the denominator is positive, that the numerator and denominator are relatively prime, and that each fits in its allocation of bits within the word.

Parameters
nnInitial numerator.
ddInitial denominator.

Definition at line 56 of file normalized-pair.hpp.

56  : pair_(pair(nn, dd)) {
57  enum {
58  NMAX = U(1) << (NMR_BITS - 1), // maximum magnitude of numerator
59  DMAX = U(1) << (DNM_BITS) // maximum value of denominator
60  };
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"; }
65  }
constexpr S n() const
Normalized numerator.
static constexpr std::pair< S, U > pair(S n, S d)
Normalize numerator and denominator.
typename types::UF U
Fastest large-enough unsigned type.
std::pair< S, U > pair_
Normalized numerator and denominator.
constexpr U d() const
Normalized denominator.

Here is the call graph for this function:

Here is the caller graph for this function:

Member Function Documentation

template<unsigned NMR_BITS, unsigned DNM_BITS>
constexpr U vnix::rat::normalized_pair< NMR_BITS, DNM_BITS >::d ( ) const
inline

Normalized denominator.

Definition at line 68 of file normalized-pair.hpp.

Here is the caller graph for this function:

template<unsigned NMR_BITS, unsigned DNM_BITS>
constexpr S vnix::rat::normalized_pair< NMR_BITS, DNM_BITS >::n ( ) const
inline

Normalized numerator.

Definition at line 67 of file normalized-pair.hpp.

Here is the caller graph for this function:

template<unsigned NMR_BITS, unsigned DNM_BITS>
static constexpr std::pair<S, U> vnix::rat::normalized_pair< NMR_BITS, DNM_BITS >::pair ( S  n,
S  d 
)
inlinestaticprivate

Normalize numerator and denominator.

Parameters
nInput numerator.
dInput denominator.
Returns
Normalized numerator and denominator.

Definition at line 40 of file normalized-pair.hpp.

40  {
41  U const g = gcd(n, d);
42  if (d < 0) { return {-n / g, -d / g}; }
43  return {n / g, d / g};
44  }
constexpr S n() const
Normalized numerator.
typename types::UF U
Fastest large-enough unsigned type.
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
constexpr U d() const
Normalized denominator.

Member Data Documentation

template<unsigned NMR_BITS, unsigned DNM_BITS>
std::pair<S, U> vnix::rat::normalized_pair< NMR_BITS, DNM_BITS >::pair_
private

Normalized numerator and denominator.

Definition at line 34 of file normalized-pair.hpp.


The documentation for this class was generated from the following file: