Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
Fixed16 Class Reference

Public Types

enum  FromDouble { FROM_DOUBLE }
 

Public Member Functions

constexpr Fixed16 (int16_t integer, uint16_t frac=0)
 Constructs a Fixed16.
 
constexpr Fixed16 (FromDouble, double value)
 Constructs a Fixed16.
 
 Fixed16 (const Fixed16 &o)=default
 
Fixed16operator= (const Fixed16 &o)=default
 
Fixed16operator+= (Fixed16 o)
 
template<class T >
Fixed16 operator+ (T o) const
 
Fixed16operator-= (Fixed16 o)
 
template<typename T >
Fixed16 operator- (T o) const
 
Fixed16operator*= (Fixed16 o)
 
template<typename T >
Fixed16 operator* (T o) const
 
Fixed16operator/= (Fixed16 o)
 
template<typename T >
Fixed16 operator/ (T o) const
 
bool operator< (Fixed16 o)
 Comparison operator.
 
bool operator<= (Fixed16 o)
 Comparison operator.
 
bool operator> (Fixed16 o)
 Comparison operator.
 
bool operator>= (Fixed16 o)
 Comparison operator.
 
bool operator== (Fixed16 o)
 Comparison operator.
 
bool operator!= (Fixed16 o)
 Comparison operator.
 
Fixed16mulpow2 (Fixed16 o)
 Multiplies *this with pow(2, o).
 
int16_t round () const
 
int16_t trunc () const
 
uint16_t frac () const
 
float to_float () const
 
bool is_positive () const
 
void negate ()
 
int32_t to_key ()
 Turns the value into a comparison key.
 

Private Member Functions

int32_t to_int () const
 Translates the current value to a signed fixed-point 32-bit integer.
 
void from_int (int32_t v)
 Overwrites the current value from a signed fixed-point 32-bit integer.
 

Private Attributes

uint32_t value_: 31
 
uint32_t sign_: 1
 

Detailed Description

Definition at line 42 of file Fixed16.hxx.

Member Enumeration Documentation

◆ FromDouble

enum Fixed16::FromDouble

Definition at line 57 of file Fixed16.hxx.

Constructor & Destructor Documentation

◆ Fixed16() [1/2]

constexpr Fixed16::Fixed16 ( int16_t  integer,
uint16_t  frac = 0 
)
inlineconstexpr

Constructs a Fixed16.

Parameters
integeris the integer part and the sign. Valid values are from -32767 to 32767.
fracis the fractional part. All uint16 values are valid. For positive integer the fractional part goes above the int value, for negative integers the fractional part goes below the int value.

Definition at line 51 of file Fixed16.hxx.

◆ Fixed16() [2/2]

constexpr Fixed16::Fixed16 ( FromDouble  ,
double  value 
)
inlineconstexpr

Constructs a Fixed16.

Parameters
valueis the value to store. Valid values are -32767.99999 to 32767.99999.

Definition at line 65 of file Fixed16.hxx.

Member Function Documentation

◆ frac()

uint16_t Fixed16::frac ( ) const
inline
Returns
the fractional part, as an uint16 value between 0 and 0xffff Note: the fractional part inherits the sign of the integer part, similarly to the decimal notation.

Definition at line 242 of file Fixed16.hxx.

◆ from_int()

void Fixed16::from_int ( int32_t  v)
inlineprivate

Overwrites the current value from a signed fixed-point 32-bit integer.

Definition at line 312 of file Fixed16.hxx.

◆ is_positive()

bool Fixed16::is_positive ( ) const
inline

Definition at line 282 of file Fixed16.hxx.

◆ mulpow2()

Fixed16 & Fixed16::mulpow2 ( Fixed16  o)
inline

Multiplies *this with pow(2, o).

This is effectively a generalized shift operation that works on fractional numbers too. The precision is limited.

Modifies *this.

Parameters
onumber of "bits" to shift with. May be positive or negative.
Returns
*this = *this * pow(2, o);

Definition at line 181 of file Fixed16.hxx.

◆ negate()

void Fixed16::negate ( )
inline

Definition at line 286 of file Fixed16.hxx.

◆ operator!=()

bool Fixed16::operator!= ( Fixed16  o)
inline

Comparison operator.

Definition at line 169 of file Fixed16.hxx.

◆ operator*()

template<typename T >
Fixed16 Fixed16::operator* ( o) const
inline

Definition at line 113 of file Fixed16.hxx.

◆ operator*=()

Fixed16 & Fixed16::operator*= ( Fixed16  o)
inline

Definition at line 103 of file Fixed16.hxx.

◆ operator+()

template<class T >
Fixed16 Fixed16::operator+ ( o) const
inline

Definition at line 83 of file Fixed16.hxx.

◆ operator+=()

Fixed16 & Fixed16::operator+= ( Fixed16  o)
inline

Definition at line 77 of file Fixed16.hxx.

◆ operator-()

template<typename T >
Fixed16 Fixed16::operator- ( o) const
inline

Definition at line 96 of file Fixed16.hxx.

◆ operator-=()

Fixed16 & Fixed16::operator-= ( Fixed16  o)
inline

Definition at line 90 of file Fixed16.hxx.

◆ operator/()

template<typename T >
Fixed16 Fixed16::operator/ ( o) const
inline

Definition at line 131 of file Fixed16.hxx.

◆ operator/=()

Fixed16 & Fixed16::operator/= ( Fixed16  o)
inline

Definition at line 120 of file Fixed16.hxx.

◆ operator<()

bool Fixed16::operator< ( Fixed16  o)
inline

Comparison operator.

Definition at line 139 of file Fixed16.hxx.

◆ operator<=()

bool Fixed16::operator<= ( Fixed16  o)
inline

Comparison operator.

Definition at line 145 of file Fixed16.hxx.

◆ operator==()

bool Fixed16::operator== ( Fixed16  o)
inline

Comparison operator.

Definition at line 163 of file Fixed16.hxx.

◆ operator>()

bool Fixed16::operator> ( Fixed16  o)
inline

Comparison operator.

Definition at line 151 of file Fixed16.hxx.

◆ operator>=()

bool Fixed16::operator>= ( Fixed16  o)
inline

Comparison operator.

Definition at line 157 of file Fixed16.hxx.

◆ round()

int16_t Fixed16::round ( ) const
inline
Returns
the value rounded to nearest integer.

Definition at line 225 of file Fixed16.hxx.

◆ to_float()

float Fixed16::to_float ( ) const
inline

Definition at line 247 of file Fixed16.hxx.

◆ to_int()

int32_t Fixed16::to_int ( ) const
inlineprivate

Translates the current value to a signed fixed-point 32-bit integer.

Definition at line 298 of file Fixed16.hxx.

◆ to_key()

int32_t Fixed16::to_key ( )
inline

Turns the value into a comparison key.

Definition at line 291 of file Fixed16.hxx.

◆ trunc()

int16_t Fixed16::trunc ( ) const
inline
Returns
the integer part, rounded towards zero.

Definition at line 232 of file Fixed16.hxx.

Member Data Documentation

◆ sign_

uint32_t Fixed16::sign_
private

Definition at line 323 of file Fixed16.hxx.

◆ value_

uint32_t Fixed16::value_
private

Definition at line 322 of file Fixed16.hxx.


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