Fixed Point Classes#
- group Fixed Point
Typedefs
Enums
-
enum scale_type#
The scale type for fixed_point.
Values:
-
enum class Radix : int32_t#
Scoped enumerator to use when constructing
fixed_pointExamples:
using decimal32 = fixed_point<int32_t, Radix::BASE_10>; using binary64 = fixed_point<int64_t, Radix::BASE_2>;
Values:
-
enumerator BASE_2#
-
enumerator BASE_10#
-
enumerator BASE_2#
Functions
-
template<typename T>
inline constexpr auto is_supported_representation_type()# Returns
trueif the representation type is supported byfixed_point- Template Parameters:
T – The representation type
- Returns:
trueif the type is supported byfixed_pointimplementation
-
template<typename Rep, typename T>
inline auto addition_overflow(T lhs, T rhs)# Function for identifying integer overflow when adding.
- Template Parameters:
Rep – Type of integer to check for overflow on
T – Types of lhs and rhs (ensures they are the same type)
- Parameters:
lhs – Left hand side of addition
rhs – Right hand side of addition
- Returns:
true if addition causes overflow, false otherwise
-
template<typename Rep, typename T>
inline auto subtraction_overflow(T lhs, T rhs)# Function for identifying integer overflow when subtracting.
- Template Parameters:
Rep – Type of integer to check for overflow on
T – Types of lhs and rhs (ensures they are the same type)
- Parameters:
lhs – Left hand side of subtraction
rhs – Right hand side of subtraction
- Returns:
true if subtraction causes overflow, false otherwise
-
template<typename Rep, typename T>
inline auto division_overflow(T lhs, T rhs)# Function for identifying integer overflow when dividing.
- Template Parameters:
Rep – Type of integer to check for overflow on
T – Types of lhs and rhs (ensures they are the same type)
- Parameters:
lhs – Left hand side of division
rhs – Right hand side of division
- Returns:
true if division causes overflow, false otherwise
-
template<typename Rep, typename T>
inline auto multiplication_overflow(T lhs, T rhs)# Function for identifying integer overflow when multiplying.
- Template Parameters:
Rep – Type of integer to check for overflow on
T – Types of lhs and rhs (ensures they are the same type)
- Parameters:
lhs – Left hand side of multiplication
rhs – Right hand side of multiplication
- Returns:
true if multiplication causes overflow, false otherwise
-
template<typename Rep1, Radix Rad1>
inline fixed_point<Rep1, Rad1> operator+(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# If
_scales are equal,_values are added. If_scales are not equal, the number with the larger_scaleis shifted to the smaller_scale, and then the_values are added.- Template Parameters:
Rep1 – Representation type of the operand
lhsandrhsRad1 – Radix (base) type of the operand
lhsandrhs
- Parameters:
lhs – The left hand side operand
rhs – The right hand side operand
- Returns:
The resulting
fixed_pointsum
-
template<typename Rep1, Radix Rad1>
inline fixed_point<Rep1, Rad1> operator-(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# If
_scales are equal,_values are subtracted. If_scales are not equal, the number with the larger_scaleis shifted to the smaller_scale, and then the_values are subtracted.- Template Parameters:
Rep1 – Representation type of the operand
lhsandrhsRad1 – Radix (base) type of the operand
lhsandrhs
- Parameters:
lhs – The left hand side operand
rhs – The right hand side operand
- Returns:
The resulting
fixed_pointdifference
-
template<typename Rep1, Radix Rad1>
inline fixed_point<Rep1, Rad1> operator*(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# _scales are added and_values are multiplied.- Template Parameters:
Rep1 – Representation type of the operand
lhsandrhsRad1 – Radix (base) type of the operand
lhsandrhs
- Parameters:
lhs – The left hand side operand
rhs – The right hand side operand
- Returns:
The resulting
fixed_pointproduct
-
template<typename Rep1, Radix Rad1>
inline fixed_point<Rep1, Rad1> operator/(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# _scales are subtracted and_values are divided.- Template Parameters:
Rep1 – Representation type of the operand
lhsandrhsRad1 – Radix (base) type of the operand
lhsandrhs
- Parameters:
lhs – The left hand side operand
rhs – The right hand side operand
- Returns:
The resulting
fixed_pointquotient
-
template<typename Rep1, Radix Rad1>
inline bool operator==(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# If
_scales are equal,_values are compared. If_scales are not equal, the number with the larger_scaleis shifted to the smaller_scale, and then the_values are compared.- Template Parameters:
Rep1 – Representation type of the operand
lhsandrhsRad1 – Radix (base) type of the operand
lhsandrhs
- Parameters:
lhs – The left hand side operand
rhs – The right hand side operand
- Returns:
true if
lhsandrhsare equal, false if not
-
template<typename Rep1, Radix Rad1>
inline bool operator!=(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# If
_scales are equal,_values are compared. If_scales are not equal, the number with the larger_scaleis shifted to the smaller_scale, and then the_values are compared.- Template Parameters:
Rep1 – Representation type of the operand
lhsandrhsRad1 – Radix (base) type of the operand
lhsandrhs
- Parameters:
lhs – The left hand side operand
rhs – The right hand side operand
- Returns:
true if
lhsandrhsare not equal, false if not
-
template<typename Rep1, Radix Rad1>
inline bool operator<=(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# If
_scales are equal,_values are compared. If_scales are not equal, the number with the larger_scaleis shifted to the smaller_scale, and then the_values are compared.- Template Parameters:
Rep1 – Representation type of the operand
lhsandrhsRad1 – Radix (base) type of the operand
lhsandrhs
- Parameters:
lhs – The left hand side operand
rhs – The right hand side operand
- Returns:
true if
lhsless than or equal torhs, false if not
-
template<typename Rep1, Radix Rad1>
inline bool operator>=(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# If
_scales are equal,_values are compared. If_scales are not equal, the number with the larger_scaleis shifted to the smaller_scale, and then the_values are compared.- Template Parameters:
Rep1 – Representation type of the operand
lhsandrhsRad1 – Radix (base) type of the operand
lhsandrhs
- Parameters:
lhs – The left hand side operand
rhs – The right hand side operand
- Returns:
true if
lhsgreater than or equal torhs, false if not
-
template<typename Rep1, Radix Rad1>
inline bool operator<(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# If
_scales are equal,_values are compared. If_scales are not equal, the number with the larger_scaleis shifted to the smaller_scale, and then the_values are compared.- Template Parameters:
Rep1 – Representation type of the operand
lhsandrhsRad1 – Radix (base) type of the operand
lhsandrhs
- Parameters:
lhs – The left hand side operand
rhs – The right hand side operand
- Returns:
true if
lhsless thanrhs, false if not
-
template<typename Rep1, Radix Rad1>
inline bool operator>(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# If
_scales are equal,_values are compared. If_scales are not equal, the number with the larger_scaleis shifted to the smaller_scale, and then the_values are compared.- Template Parameters:
Rep1 – Representation type of the operand
lhsandrhsRad1 – Radix (base) type of the operand
lhsandrhs
- Parameters:
lhs – The left hand side operand
rhs – The right hand side operand
- Returns:
true if
lhsgreater thanrhs, false if not
-
template<typename Rep1, Radix Rad1>
inline fixed_point<Rep1, Rad1> operator%(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# If
_scales are equal, the modulus is computed directly. If_scales are not equal, the number with larger_scaleis shifted to the smaller_scale, and then the modulus is computed.- Template Parameters:
Rep1 – Representation type of the operand
lhsandrhsRad1 – Radix (base) type of the operand
lhsandrhs
- Parameters:
lhs – The left hand side operand
rhs – The right hand side operand
- Returns:
The resulting
fixed_pointnumber
-
template<typename Rep, typename cuda::std::enable_if_t<is_supported_representation_type<Rep>()>* = nullptr>
struct scaled_integer# - #include <fixed_point.hpp>
Helper struct for constructing
fixed_pointwhen value is already shifted.Example:
using decimal32 = fixed_point<int32_t, Radix::BASE_10>; auto n = decimal32{scaled_integer{1001, 3}}; // n = 1.001
- Template Parameters:
Rep – The representation type (either
int32_torint64_t)
Public Functions
-
inline explicit scaled_integer(Rep v, scale_type s)#
Constructor for
scaled_integer- Parameters:
v – The value of the fixed point number
s – The scale of the value
Public Members
-
scale_type scale#
The scale of the value.
-
template<typename Rep, Radix Rad>
class fixed_point# - #include <fixed_point.hpp>
A type for representing a number with a fixed amount of precision.
Currently, only binary and decimal
fixed_pointnumbers are supported. Binary operations can only be performed with otherfixed_pointnumbers- Template Parameters:
Rep – The representation type (either
int32_torint64_t)Rad – The radix/base (either
Radix::BASE_2orRadix::BASE_10)
Public Functions
-
template<typename T>
inline explicit fixed_point(T const &value, scale_type const &scale)# Constructor that will perform shifting to store value appropriately (from integral types)
- Template Parameters:
T – The integral type that you are constructing from
- Parameters:
value – The value that will be constructed from
scale – The exponent that is applied to Rad to perform shifting
-
inline explicit fixed_point(scaled_integer<Rep> s)#
Constructor that will not perform shifting (assumes value already shifted)
- Parameters:
s – scaled_integer that contains scale and already shifted value
-
template<typename T>
inline fixed_point(T const &value)# “Scale-less” constructor that constructs
fixed_pointnumber with a specified value and scale of zero- Template Parameters:
T – The value type being constructing from
- Parameters:
value – The value that will be constructed from
-
inline fixed_point()#
Default constructor that constructs
fixed_pointnumber with a value and scale of zero.
-
template<typename U>
inline explicit constexpr operator U() const# Explicit conversion operator for casting to integral types.
- Template Parameters:
U – The integral type that is being explicitly converted to
- Returns:
The
fixed_pointnumber in base 10 (aka human readable format)
-
inline operator scaled_integer<Rep>() const#
Converts the
fixed_pointnumber to ascaled_integer- Returns:
The
scaled_integerrepresentation of thefixed_pointnumber
-
inline rep value() const#
Method that returns the underlying value of the
fixed_pointnumber.- Returns:
The underlying value of the
fixed_pointnumber
-
inline scale_type scale() const#
Method that returns the scale of the
fixed_pointnumber.- Returns:
The scale of the
fixed_pointnumber
-
inline explicit constexpr operator bool() const#
Explicit conversion operator to
bool- Returns:
The
fixed_pointvalue as a boolean (zero isfalse, nonzero istrue)
-
template<typename Rep1, Radix Rad1>
inline fixed_point<Rep1, Rad1> &operator+=(fixed_point<Rep1, Rad1> const &rhs)# operator +=
- Template Parameters:
Rep1 – Representation type of the operand
rhsRad1 – Radix (base) type of the operand
rhs
- Parameters:
rhs – The number being added to
this- Returns:
The sum
-
template<typename Rep1, Radix Rad1>
inline fixed_point<Rep1, Rad1> &operator*=(fixed_point<Rep1, Rad1> const &rhs)# operator *=
- Template Parameters:
Rep1 – Representation type of the operand
rhsRad1 – Radix (base) type of the operand
rhs
- Parameters:
rhs – The number being multiplied to
this- Returns:
The product
-
template<typename Rep1, Radix Rad1>
inline fixed_point<Rep1, Rad1> &operator-=(fixed_point<Rep1, Rad1> const &rhs)# operator -=
- Template Parameters:
Rep1 – Representation type of the operand
rhsRad1 – Radix (base) type of the operand
rhs
- Parameters:
rhs – The number being subtracted from
this- Returns:
The difference
-
template<typename Rep1, Radix Rad1>
inline fixed_point<Rep1, Rad1> &operator/=(fixed_point<Rep1, Rad1> const &rhs)# operator /=
- Template Parameters:
Rep1 – Representation type of the operand
rhsRad1 – Radix (base) type of the operand
rhs
- Parameters:
rhs – The number being divided from
this- Returns:
The quotient
-
inline fixed_point<Rep, Rad> &operator++()#
operator ++ (post-increment)
- Returns:
The incremented result
-
inline fixed_point<Rep, Rad> rescaled(scale_type scale) const#
Method for creating a
fixed_pointnumber with a newscaleThe
fixed_pointnumber returned will have the same value, underlying representation and radix asthis, the only thing changed is the scale.- Parameters:
scale – The
scaleof the returnedfixed_pointnumber- Returns:
fixed_pointnumber with a newscale
-
inline explicit operator std::string() const#
Returns a string representation of the fixed_point value.
Friends
-
template<typename Rep1, Radix Rad1>
inline friend fixed_point<Rep1, Rad1> operator+(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# operator + (for adding two
fixed_pointnumbers)If
_scales are equal,_values are added. If_scales are not equal, the number with the larger_scaleis shifted to the smaller_scale, and then the_values are added.- Template Parameters:
Rep1 – Representation type of the operand
lhsandrhsRad1 – Radix (base) type of the operand
lhsandrhs
- Parameters:
lhs – The left hand side operand
rhs – The right hand side operand
- Returns:
The resulting
fixed_pointsum
-
template<typename Rep1, Radix Rad1>
inline friend fixed_point<Rep1, Rad1> operator-(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# operator - (for subtracting two
fixed_pointnumbers)If
_scales are equal,_values are subtracted. If_scales are not equal, the number with the larger_scaleis shifted to the smaller_scale, and then the_values are subtracted.- Template Parameters:
Rep1 – Representation type of the operand
lhsandrhsRad1 – Radix (base) type of the operand
lhsandrhs
- Parameters:
lhs – The left hand side operand
rhs – The right hand side operand
- Returns:
The resulting
fixed_pointdifference
-
template<typename Rep1, Radix Rad1>
inline friend fixed_point<Rep1, Rad1> operator*(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# operator * (for multiplying two
fixed_pointnumbers)_scales are added and_values are multiplied.- Template Parameters:
Rep1 – Representation type of the operand
lhsandrhsRad1 – Radix (base) type of the operand
lhsandrhs
- Parameters:
lhs – The left hand side operand
rhs – The right hand side operand
- Returns:
The resulting
fixed_pointproduct
-
template<typename Rep1, Radix Rad1>
inline friend fixed_point<Rep1, Rad1> operator/(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# operator / (for dividing two
fixed_pointnumbers)_scales are subtracted and_values are divided.- Template Parameters:
Rep1 – Representation type of the operand
lhsandrhsRad1 – Radix (base) type of the operand
lhsandrhs
- Parameters:
lhs – The left hand side operand
rhs – The right hand side operand
- Returns:
The resulting
fixed_pointquotient
-
template<typename Rep1, Radix Rad1>
inline friend fixed_point<Rep1, Rad1> operator%(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# operator % (for computing the modulo operation of two
fixed_pointnumbers)If
_scales are equal, the modulus is computed directly. If_scales are not equal, the number with larger_scaleis shifted to the smaller_scale, and then the modulus is computed.- Template Parameters:
Rep1 – Representation type of the operand
lhsandrhsRad1 – Radix (base) type of the operand
lhsandrhs
- Parameters:
lhs – The left hand side operand
rhs – The right hand side operand
- Returns:
The resulting
fixed_pointnumber
-
template<typename Rep1, Radix Rad1>
inline friend bool operator==(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# operator == (for comparing two
fixed_pointnumbers)If
_scales are equal,_values are compared. If_scales are not equal, the number with the larger_scaleis shifted to the smaller_scale, and then the_values are compared.- Template Parameters:
Rep1 – Representation type of the operand
lhsandrhsRad1 – Radix (base) type of the operand
lhsandrhs
- Parameters:
lhs – The left hand side operand
rhs – The right hand side operand
- Returns:
true if
lhsandrhsare equal, false if not
-
template<typename Rep1, Radix Rad1>
inline friend bool operator!=(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# operator != (for comparing two
fixed_pointnumbers)If
_scales are equal,_values are compared. If_scales are not equal, the number with the larger_scaleis shifted to the smaller_scale, and then the_values are compared.- Template Parameters:
Rep1 – Representation type of the operand
lhsandrhsRad1 – Radix (base) type of the operand
lhsandrhs
- Parameters:
lhs – The left hand side operand
rhs – The right hand side operand
- Returns:
true if
lhsandrhsare not equal, false if not
-
template<typename Rep1, Radix Rad1>
inline friend bool operator<=(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# operator <= (for comparing two
fixed_pointnumbers)If
_scales are equal,_values are compared. If_scales are not equal, the number with the larger_scaleis shifted to the smaller_scale, and then the_values are compared.- Template Parameters:
Rep1 – Representation type of the operand
lhsandrhsRad1 – Radix (base) type of the operand
lhsandrhs
- Parameters:
lhs – The left hand side operand
rhs – The right hand side operand
- Returns:
true if
lhsless than or equal torhs, false if not
-
template<typename Rep1, Radix Rad1>
inline friend bool operator>=(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# operator >= (for comparing two
fixed_pointnumbers)If
_scales are equal,_values are compared. If_scales are not equal, the number with the larger_scaleis shifted to the smaller_scale, and then the_values are compared.- Template Parameters:
Rep1 – Representation type of the operand
lhsandrhsRad1 – Radix (base) type of the operand
lhsandrhs
- Parameters:
lhs – The left hand side operand
rhs – The right hand side operand
- Returns:
true if
lhsgreater than or equal torhs, false if not
-
template<typename Rep1, Radix Rad1>
inline friend bool operator<(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# operator < (for comparing two
fixed_pointnumbers)If
_scales are equal,_values are compared. If_scales are not equal, the number with the larger_scaleis shifted to the smaller_scale, and then the_values are compared.- Template Parameters:
Rep1 – Representation type of the operand
lhsandrhsRad1 – Radix (base) type of the operand
lhsandrhs
- Parameters:
lhs – The left hand side operand
rhs – The right hand side operand
- Returns:
true if
lhsless thanrhs, false if not
-
template<typename Rep1, Radix Rad1>
inline friend bool operator>(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# operator > (for comparing two
fixed_pointnumbers)If
_scales are equal,_values are compared. If_scales are not equal, the number with the larger_scaleis shifted to the smaller_scale, and then the_values are compared.- Template Parameters:
Rep1 – Representation type of the operand
lhsandrhsRad1 – Radix (base) type of the operand
lhsandrhs
- Parameters:
lhs – The left hand side operand
rhs – The right hand side operand
- Returns:
true if
lhsgreater thanrhs, false if not
-
enum scale_type#