Fixed Point Classes#
- group fixed_point_classes
Typedefs
Enums
-
enum scale_type#
The scale type for fixed_point.
Values:
-
enum class Radix : int32_t#
Scoped enumerator to use when constructing
fixed_point
Examples:
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
true
if the representation type is supported byfixed_point
- Template Parameters:
T – The representation type
- Returns:
true
if the type is supported byfixed_point
implementation
-
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
_scale
s are equal,_value
s are added. If_scale
s are not equal, the number with the larger_scale
is shifted to the smaller_scale
, and then the_value
s are added.- Template Parameters:
Rep1 – Representation type of the operand
lhs
andrhs
Rad1 – Radix (base) type of the operand
lhs
andrhs
- Parameters:
lhs – The left hand side operand
rhs – The right hand side operand
- Returns:
The resulting
fixed_point
sum
-
template<typename Rep1, Radix Rad1>
inline fixed_point<Rep1, Rad1> operator-(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# If
_scale
s are equal,_value
s are subtracted. If_scale
s are not equal, the number with the larger_scale
is shifted to the smaller_scale
, and then the_value
s are subtracted.- Template Parameters:
Rep1 – Representation type of the operand
lhs
andrhs
Rad1 – Radix (base) type of the operand
lhs
andrhs
- Parameters:
lhs – The left hand side operand
rhs – The right hand side operand
- Returns:
The resulting
fixed_point
difference
-
template<typename Rep1, Radix Rad1>
inline fixed_point<Rep1, Rad1> operator*(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# _scale
s are added and_value
s are multiplied.- Template Parameters:
Rep1 – Representation type of the operand
lhs
andrhs
Rad1 – Radix (base) type of the operand
lhs
andrhs
- Parameters:
lhs – The left hand side operand
rhs – The right hand side operand
- Returns:
The resulting
fixed_point
product
-
template<typename Rep1, Radix Rad1>
inline fixed_point<Rep1, Rad1> operator/(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# _scale
s are subtracted and_value
s are divided.- Template Parameters:
Rep1 – Representation type of the operand
lhs
andrhs
Rad1 – Radix (base) type of the operand
lhs
andrhs
- Parameters:
lhs – The left hand side operand
rhs – The right hand side operand
- Returns:
The resulting
fixed_point
quotient
-
template<typename Rep1, Radix Rad1>
inline bool operator==(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# If
_scale
s are equal,_value
s are compared. If_scale
s are not equal, the number with the larger_scale
is shifted to the smaller_scale
, and then the_value
s are compared.- Template Parameters:
Rep1 – Representation type of the operand
lhs
andrhs
Rad1 – Radix (base) type of the operand
lhs
andrhs
- Parameters:
lhs – The left hand side operand
rhs – The right hand side operand
- Returns:
true if
lhs
andrhs
are 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
_scale
s are equal,_value
s are compared. If_scale
s are not equal, the number with the larger_scale
is shifted to the smaller_scale
, and then the_value
s are compared.- Template Parameters:
Rep1 – Representation type of the operand
lhs
andrhs
Rad1 – Radix (base) type of the operand
lhs
andrhs
- Parameters:
lhs – The left hand side operand
rhs – The right hand side operand
- Returns:
true if
lhs
andrhs
are 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
_scale
s are equal,_value
s are compared. If_scale
s are not equal, the number with the larger_scale
is shifted to the smaller_scale
, and then the_value
s are compared.- Template Parameters:
Rep1 – Representation type of the operand
lhs
andrhs
Rad1 – Radix (base) type of the operand
lhs
andrhs
- Parameters:
lhs – The left hand side operand
rhs – The right hand side operand
- Returns:
true if
lhs
less 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
_scale
s are equal,_value
s are compared. If_scale
s are not equal, the number with the larger_scale
is shifted to the smaller_scale
, and then the_value
s are compared.- Template Parameters:
Rep1 – Representation type of the operand
lhs
andrhs
Rad1 – Radix (base) type of the operand
lhs
andrhs
- Parameters:
lhs – The left hand side operand
rhs – The right hand side operand
- Returns:
true if
lhs
greater 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
_scale
s are equal,_value
s are compared. If_scale
s are not equal, the number with the larger_scale
is shifted to the smaller_scale
, and then the_value
s are compared.- Template Parameters:
Rep1 – Representation type of the operand
lhs
andrhs
Rad1 – Radix (base) type of the operand
lhs
andrhs
- Parameters:
lhs – The left hand side operand
rhs – The right hand side operand
- Returns:
true if
lhs
less 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
_scale
s are equal,_value
s are compared. If_scale
s are not equal, the number with the larger_scale
is shifted to the smaller_scale
, and then the_value
s are compared.- Template Parameters:
Rep1 – Representation type of the operand
lhs
andrhs
Rad1 – Radix (base) type of the operand
lhs
andrhs
- Parameters:
lhs – The left hand side operand
rhs – The right hand side operand
- Returns:
true if
lhs
greater 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
_scale
s are equal, the modulus is computed directly. If_scale
s are not equal, the number with larger_scale
is shifted to the smaller_scale
, and then the modulus is computed.- Template Parameters:
Rep1 – Representation type of the operand
lhs
andrhs
Rad1 – Radix (base) type of the operand
lhs
andrhs
- Parameters:
lhs – The left hand side operand
rhs – The right hand side operand
- Returns:
The resulting
fixed_point
number
-
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_point
when 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_t
orint64_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_point
numbers are supported. Binary operations can only be performed with otherfixed_point
numbers- Template Parameters:
Rep – The representation type (either
int32_t
orint64_t
)Rad – The radix/base (either
Radix::BASE_2
orRadix::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_point
number 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_point
number 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_point
number in base 10 (aka human readable format)
-
inline operator scaled_integer<Rep>() const#
Converts the
fixed_point
number to ascaled_integer
- Returns:
The
scaled_integer
representation of thefixed_point
number
-
inline rep value() const#
Method that returns the underlying value of the
fixed_point
number.- Returns:
The underlying value of the
fixed_point
number
-
inline scale_type scale() const#
Method that returns the scale of the
fixed_point
number.- Returns:
The scale of the
fixed_point
number
-
inline explicit constexpr operator bool() const#
Explicit conversion operator to
bool
- Returns:
The
fixed_point
value 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
rhs
Rad1 – 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
rhs
Rad1 – 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
rhs
Rad1 – 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
rhs
Rad1 – 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_point
number with a newscale
The
fixed_point
number returned will have the same value, underlying representation and radix asthis
, the only thing changed is the scale.- Parameters:
scale – The
scale
of the returnedfixed_point
number- Returns:
fixed_point
number 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_point
numbers)If
_scale
s are equal,_value
s are added. If_scale
s are not equal, the number with the larger_scale
is shifted to the smaller_scale
, and then the_value
s are added.- Template Parameters:
Rep1 – Representation type of the operand
lhs
andrhs
Rad1 – Radix (base) type of the operand
lhs
andrhs
- Parameters:
lhs – The left hand side operand
rhs – The right hand side operand
- Returns:
The resulting
fixed_point
sum
-
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_point
numbers)If
_scale
s are equal,_value
s are subtracted. If_scale
s are not equal, the number with the larger_scale
is shifted to the smaller_scale
, and then the_value
s are subtracted.- Template Parameters:
Rep1 – Representation type of the operand
lhs
andrhs
Rad1 – Radix (base) type of the operand
lhs
andrhs
- Parameters:
lhs – The left hand side operand
rhs – The right hand side operand
- Returns:
The resulting
fixed_point
difference
-
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_point
numbers)_scale
s are added and_value
s are multiplied.- Template Parameters:
Rep1 – Representation type of the operand
lhs
andrhs
Rad1 – Radix (base) type of the operand
lhs
andrhs
- Parameters:
lhs – The left hand side operand
rhs – The right hand side operand
- Returns:
The resulting
fixed_point
product
-
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_point
numbers)_scale
s are subtracted and_value
s are divided.- Template Parameters:
Rep1 – Representation type of the operand
lhs
andrhs
Rad1 – Radix (base) type of the operand
lhs
andrhs
- Parameters:
lhs – The left hand side operand
rhs – The right hand side operand
- Returns:
The resulting
fixed_point
quotient
-
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_point
numbers)If
_scale
s are equal, the modulus is computed directly. If_scale
s are not equal, the number with larger_scale
is shifted to the smaller_scale
, and then the modulus is computed.- Template Parameters:
Rep1 – Representation type of the operand
lhs
andrhs
Rad1 – Radix (base) type of the operand
lhs
andrhs
- Parameters:
lhs – The left hand side operand
rhs – The right hand side operand
- Returns:
The resulting
fixed_point
number
-
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_point
numbers)If
_scale
s are equal,_value
s are compared. If_scale
s are not equal, the number with the larger_scale
is shifted to the smaller_scale
, and then the_value
s are compared.- Template Parameters:
Rep1 – Representation type of the operand
lhs
andrhs
Rad1 – Radix (base) type of the operand
lhs
andrhs
- Parameters:
lhs – The left hand side operand
rhs – The right hand side operand
- Returns:
true if
lhs
andrhs
are 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_point
numbers)If
_scale
s are equal,_value
s are compared. If_scale
s are not equal, the number with the larger_scale
is shifted to the smaller_scale
, and then the_value
s are compared.- Template Parameters:
Rep1 – Representation type of the operand
lhs
andrhs
Rad1 – Radix (base) type of the operand
lhs
andrhs
- Parameters:
lhs – The left hand side operand
rhs – The right hand side operand
- Returns:
true if
lhs
andrhs
are 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_point
numbers)If
_scale
s are equal,_value
s are compared. If_scale
s are not equal, the number with the larger_scale
is shifted to the smaller_scale
, and then the_value
s are compared.- Template Parameters:
Rep1 – Representation type of the operand
lhs
andrhs
Rad1 – Radix (base) type of the operand
lhs
andrhs
- Parameters:
lhs – The left hand side operand
rhs – The right hand side operand
- Returns:
true if
lhs
less 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_point
numbers)If
_scale
s are equal,_value
s are compared. If_scale
s are not equal, the number with the larger_scale
is shifted to the smaller_scale
, and then the_value
s are compared.- Template Parameters:
Rep1 – Representation type of the operand
lhs
andrhs
Rad1 – Radix (base) type of the operand
lhs
andrhs
- Parameters:
lhs – The left hand side operand
rhs – The right hand side operand
- Returns:
true if
lhs
greater 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_point
numbers)If
_scale
s are equal,_value
s are compared. If_scale
s are not equal, the number with the larger_scale
is shifted to the smaller_scale
, and then the_value
s are compared.- Template Parameters:
Rep1 – Representation type of the operand
lhs
andrhs
Rad1 – Radix (base) type of the operand
lhs
andrhs
- Parameters:
lhs – The left hand side operand
rhs – The right hand side operand
- Returns:
true if
lhs
less 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_point
numbers)If
_scale
s are equal,_value
s are compared. If_scale
s are not equal, the number with the larger_scale
is shifted to the smaller_scale
, and then the_value
s are compared.- Template Parameters:
Rep1 – Representation type of the operand
lhs
andrhs
Rad1 – Radix (base) type of the operand
lhs
andrhs
- Parameters:
lhs – The left hand side operand
rhs – The right hand side operand
- Returns:
true if
lhs
greater thanrhs
, false if not
-
enum scale_type#