units
Use physical dimensions at compile-time or run-time.
Classes | Public Types | Public Member Functions | Static Public Member Functions | Private Types | Private Member Functions | List of all members
vnix::rat::rational< NB, DB > Class Template Reference

Model of a fixed-precision rational number. More...

#include <rational.hpp>

Inheritance diagram for vnix::rat::rational< NB, DB >:
[legend]
Collaboration diagram for vnix::rat::rational< NB, DB >:
[legend]

Classes

struct  dummy_arg
 

Public Types

using stype = typename int_types< BITS >::SS
 Signed type for encoding. More...
 
using utype = typename int_types< BITS >::US
 Unsigned type for encoding. More...
 
- Public Types inherited from vnix::rat::encoding< NB, DB >
enum  
 
enum  
 Mask for each of numerator and denominator. More...
 
using utype = typename int_types< BITS >::US
 Unsigned type for encoding. More...
 
using stype = typename int_types< BITS >::SS
 Signed type for encoding. More...
 
using uftype = typename int_types< BITS >::UF
 Unsigned fast type. More...
 
using sftype = typename int_types< BITS >::SF
 Signed fast type. More...
 

Public Member Functions

constexpr rational (stype n=0, stype d=1)
 Initialize from numerator and denominator. More...
 
template<unsigned ONB, unsigned ODB>
constexpr rational (rational< ONB, ODB > r)
 Initialize from other rational. More...
 
constexpr stype to_int () const
 Convert to (signed) integer. More...
 
constexpr bool to_bool () const
 Automatically convert to boolean. More...
 
constexpr float to_float () const
 Convert to float. More...
 
constexpr double to_double () const
 Convert to double. More...
 
template<unsigned ONB, unsigned ODB>
constexpr rationaloperator+= (rational< ONB, ODB > r)
 Modify this rational number by adding other rational number. More...
 
template<unsigned ONB, unsigned ODB>
constexpr rationaloperator-= (rational< ONB, ODB > r)
 Modify this rational number by subtracting other rational number. More...
 
constexpr rational reciprocal () const
 Reciprocal of this rational number. More...
 
template<unsigned ONB, unsigned ODB>
constexpr rationaloperator*= (rational< ONB, ODB > r)
 Modify this rational number by multiplying by other rational number. More...
 
template<unsigned ONB, unsigned ODB>
constexpr rationaloperator/= (rational< ONB, ODB > r)
 Modify this rational number by dividing by other rational number. More...
 
template<unsigned ONB, unsigned ODB>
constexpr rational< NB, DB > & operator+= (rational< ONB, ODB > r)
 
template<unsigned ONB, unsigned ODB>
constexpr rational< NB, DB > & operator-= (rational< ONB, ODB > r)
 
template<unsigned ONB, unsigned ODB>
constexpr rational< NB, DB > & operator*= (rational< ONB, ODB > r)
 
template<unsigned ONB, unsigned ODB>
constexpr rational< NB, DB > & operator/= (rational< ONB, ODB > r)
 
- Public Member Functions inherited from vnix::rat::encoding< NB, DB >
constexpr encoding (normalized_pair< NMR_BITS, DNM_BITS > p)
 Initialize from normalized numerator and denominator. More...
 
constexpr int_types< NMR_BITS >::SF n () const
 Normalized numerator. More...
 
constexpr int_types< DNM_BITS >::UF d () const
 Normalized denominator. More...
 

Static Public Member Functions

static constexpr utype encode (rational r)
 Encoding from rational number. More...
 
static constexpr rational decode (utype u)
 Rational number from encoding. More...
 

Private Types

using P = encoding< NB, DB >
 

Private Member Functions

constexpr rational (utype u, dummy_arg)
 Construct from encoding. More...
 

Additional Inherited Members

- Protected Member Functions inherited from vnix::rat::encoding< NB, DB >
constexpr encoding (utype c)
 Allow descendant to construct from code-word. More...
 
- Protected Attributes inherited from vnix::rat::encoding< NB, DB >
utype c_
 Unsigned word storing encoding. More...
 

Detailed Description

template<unsigned NB, unsigned DB>
class vnix::rat::rational< NB, DB >

Model of a fixed-precision rational number.

Numerator and denominator are encoded in same unsigned integer word. They form a partition of the bits in the word.

Template Parameters
NBNumber of bits for numerator.
DBNumber of bits for denominator.

Definition at line 27 of file rational.hpp.

Member Typedef Documentation

template<unsigned NB, unsigned DB>
using vnix::rat::rational< NB, DB >::P = encoding<NB, DB>
private

Definition at line 29 of file rational.hpp.

template<unsigned NB, unsigned DB>
using vnix::rat::encoding< NMR_BITS, DNM_BITS >::stype = typename int_types<BITS>::SS

Signed type for encoding.

Definition at line 23 of file encoding.hpp.

template<unsigned NB, unsigned DB>
using vnix::rat::encoding< NMR_BITS, DNM_BITS >::utype = typename int_types<BITS>::US

Unsigned type for encoding.

Definition at line 22 of file encoding.hpp.

Constructor & Destructor Documentation

template<unsigned NB, unsigned DB>
constexpr vnix::rat::rational< NB, DB >::rational ( utype  u,
dummy_arg   
)
inlineprivate

Construct from encoding.

Parameters
uEncoding of rational number.

Definition at line 38 of file rational.hpp.

38 : P(u) {}
encoding< NB, DB > P
Definition: rational.hpp:29
template<unsigned NB, unsigned DB>
constexpr vnix::rat::rational< NB, DB >::rational ( stype  n = 0,
stype  d = 1 
)
inline

Initialize from numerator and denominator.

Parameters
nNumerator (zero by default).
dDenominator (unity by default).

Definition at line 47 of file rational.hpp.

Numerator and denominator of a rational number as separate numbers, not encoded into the same word...
constexpr int_types< DNM_BITS >::UF d() const
Normalized denominator.
Definition: encoding.hpp:68
encoding< NB, DB > P
Definition: rational.hpp:29
constexpr int_types< NMR_BITS >::SF n() const
Normalized numerator.
Definition: encoding.hpp:55

Here is the caller graph for this function:

template<unsigned NB, unsigned DB>
template<unsigned ONB, unsigned ODB>
constexpr vnix::rat::rational< NB, DB >::rational ( rational< ONB, ODB >  r)
inline

Initialize from other rational.

Template Parameters
ONBNumber of numerator-bits in other type of rational.
ODBNumber of denominator-bits in other type of rational.
Parameters
rCopy of other rational.

Definition at line 55 of file rational.hpp.

56  : P(normalized_pair<NB, DB>(r.n(), r.d())) {}
Numerator and denominator of a rational number as separate numbers, not encoded into the same word...
encoding< NB, DB > P
Definition: rational.hpp:29

Member Function Documentation

template<unsigned NB, unsigned DB>
static constexpr rational vnix::rat::rational< NB, DB >::decode ( utype  u)
inlinestatic

Rational number from encoding.

Definition at line 116 of file rational.hpp.

116 { return {u, dummy_arg()}; }

Here is the caller graph for this function:

template<unsigned NB, unsigned DB>
static constexpr utype vnix::rat::rational< NB, DB >::encode ( rational< NB, DB >  r)
inlinestatic

Encoding from rational number.

Definition at line 113 of file rational.hpp.

113 { return r.c_; }

Here is the caller graph for this function:

template<unsigned NB, unsigned DB>
template<unsigned ONB, unsigned ODB>
constexpr rational& vnix::rat::rational< NB, DB >::operator*= ( rational< ONB, ODB >  r)

Modify this rational number by multiplying by other rational number.

Template Parameters
ONBNumber of numerator -bits in other rational.
ODBNumber of denominator-bits in other rational.
Parameters
rOther rational number.
Returns
Reference to this rational number (after multiplication).
template<unsigned NB, unsigned DB>
template<unsigned ONB, unsigned ODB>
constexpr rational<NB, DB>& vnix::rat::rational< NB, DB >::operator*= ( rational< ONB, ODB >  r)

Definition at line 323 of file rational.hpp.

323  {
324  return *this = (*this) * r;
325 }
template<unsigned NB, unsigned DB>
template<unsigned ONB, unsigned ODB>
constexpr rational& vnix::rat::rational< NB, DB >::operator+= ( rational< ONB, ODB >  r)

Modify this rational number by adding other rational number.

Template Parameters
ONBNumber of numerator -bits in other rational.
ODBNumber of denominator-bits in other rational.
Parameters
rOther rational number.
Returns
Reference to this rational number (after addition).
template<unsigned NB, unsigned DB>
template<unsigned ONB, unsigned ODB>
constexpr rational<NB, DB>& vnix::rat::rational< NB, DB >::operator+= ( rational< ONB, ODB >  r)

Definition at line 241 of file rational.hpp.

241  {
242  return *this = *this + r;
243 }
template<unsigned NB, unsigned DB>
template<unsigned ONB, unsigned ODB>
constexpr rational& vnix::rat::rational< NB, DB >::operator-= ( rational< ONB, ODB >  r)

Modify this rational number by subtracting other rational number.

Template Parameters
ONBNumber of numerator -bits in other rational.
ODBNumber of denominator-bits in other rational.
Parameters
rOther rational number.
Returns
Reference to this rational number (after subtraction).
template<unsigned NB, unsigned DB>
template<unsigned ONB, unsigned ODB>
constexpr rational<NB, DB>& vnix::rat::rational< NB, DB >::operator-= ( rational< ONB, ODB >  r)

Definition at line 264 of file rational.hpp.

264  {
265  return *this = *this - r;
266 }
template<unsigned NB, unsigned DB>
template<unsigned ONB, unsigned ODB>
constexpr rational& vnix::rat::rational< NB, DB >::operator/= ( rational< ONB, ODB >  r)

Modify this rational number by dividing by other rational number.

Template Parameters
ONBNumber of numerator -bits in other rational.
ODBNumber of denominator-bits in other rational.
Parameters
rOther rational number.
Returns
Reference to this rational number (after division).
template<unsigned NB, unsigned DB>
template<unsigned ONB, unsigned ODB>
constexpr rational<NB, DB>& vnix::rat::rational< NB, DB >::operator/= ( rational< ONB, ODB >  r)

Definition at line 332 of file rational.hpp.

332  {
333  return *this = (*this) / r;
334 }
template<unsigned NB, unsigned DB>
constexpr rational vnix::rat::rational< NB, DB >::reciprocal ( ) const
inline

Reciprocal of this rational number.

Definition at line 90 of file rational.hpp.

90  {
91  if (n() == 0) { throw "attempt to take reciprocal of zero"; }
92  if (n() < 0) { return rational(-d(), -n()); }
93  return rational(d(), n());
94  }
constexpr rational(utype u, dummy_arg)
Construct from encoding.
Definition: rational.hpp:38
constexpr int_types< DNM_BITS >::UF d() const
Normalized denominator.
Definition: encoding.hpp:68
constexpr int_types< NMR_BITS >::SF n() const
Normalized numerator.
Definition: encoding.hpp:55

Here is the caller graph for this function:

template<unsigned NB, unsigned DB>
constexpr bool vnix::rat::rational< NB, DB >::to_bool ( ) const
inline

Automatically convert to boolean.

Definition at line 65 of file rational.hpp.

65 { return n() != 0; }
constexpr int_types< NMR_BITS >::SF n() const
Normalized numerator.
Definition: encoding.hpp:55

Here is the caller graph for this function:

template<unsigned NB, unsigned DB>
constexpr double vnix::rat::rational< NB, DB >::to_double ( ) const
inline

Convert to double.

Definition at line 71 of file rational.hpp.

71 { return n() * 1.0 / d(); }
constexpr int_types< DNM_BITS >::UF d() const
Normalized denominator.
Definition: encoding.hpp:68
constexpr int_types< NMR_BITS >::SF n() const
Normalized numerator.
Definition: encoding.hpp:55

Here is the caller graph for this function:

template<unsigned NB, unsigned DB>
constexpr float vnix::rat::rational< NB, DB >::to_float ( ) const
inline

Convert to float.

Definition at line 68 of file rational.hpp.

68 { return n() * 1.0f / d(); }
constexpr int_types< DNM_BITS >::UF d() const
Normalized denominator.
Definition: encoding.hpp:68
constexpr int_types< NMR_BITS >::SF n() const
Normalized numerator.
Definition: encoding.hpp:55

Here is the caller graph for this function:

template<unsigned NB, unsigned DB>
constexpr stype vnix::rat::rational< NB, DB >::to_int ( ) const
inline

Convert to (signed) integer.

Definition at line 59 of file rational.hpp.

59  {
60  if (d() != 1) { throw "attempted conversion to integer from fraction"; }
61  return n();
62  }
constexpr int_types< DNM_BITS >::UF d() const
Normalized denominator.
Definition: encoding.hpp:68
constexpr int_types< NMR_BITS >::SF n() const
Normalized numerator.
Definition: encoding.hpp:55

Here is the caller graph for this function:


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