Fixed Point Classes#

group fixed_point_classes

Typedefs

using decimal32 = fixed_point<int32_t, Radix::BASE_10>#

32-bit decimal fixed point

using decimal64 = fixed_point<int64_t, Radix::BASE_10>#

64-bit decimal fixed point

using decimal128 = fixed_point<__int128_t, Radix::BASE_10>#

128-bit decimal fixed point

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#

Functions

template<typename T>
inline constexpr auto is_supported_representation_type()#

Returns true if the representation type is supported by fixed_point

Template Parameters:

T – The representation type

Returns:

true if the type is supported by fixed_point implementation

template<typename T>
inline constexpr auto is_supported_construction_value_type()#

Returns true if the value type is supported for constructing a fixed_point

Template Parameters:

T – The construction value type

Returns:

true if the value type is supported to construct a fixed_point type

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 _scale is shifted to the smaller _scale, and then the _values are added.

Template Parameters:
  • Rep1 – Representation type of the operand lhs and rhs

  • Rad1 – Radix (base) type of the operand lhs and rhs

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 _scales are equal, _values are subtracted. If _scales are not equal, the number with the larger _scale is shifted to the smaller _scale, and then the _values are subtracted.

Template Parameters:
  • Rep1 – Representation type of the operand lhs and rhs

  • Rad1 – Radix (base) type of the operand lhs and rhs

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)#

_scales are added and _values are multiplied.

Template Parameters:
  • Rep1 – Representation type of the operand lhs and rhs

  • Rad1 – Radix (base) type of the operand lhs and rhs

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)#

_scales are subtracted and _values are divided.

Template Parameters:
  • Rep1 – Representation type of the operand lhs and rhs

  • Rad1 – Radix (base) type of the operand lhs and rhs

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 _scales are equal, _values are compared. If _scales are not equal, the number with the larger _scale is shifted to the smaller _scale, and then the _values are compared.

Template Parameters:
  • Rep1 – Representation type of the operand lhs and rhs

  • Rad1 – Radix (base) type of the operand lhs and rhs

Parameters:
  • lhs – The left hand side operand

  • rhs – The right hand side operand

Returns:

true if lhs and rhs 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 _scales are equal, _values are compared. If _scales are not equal, the number with the larger _scale is shifted to the smaller _scale, and then the _values are compared.

Template Parameters:
  • Rep1 – Representation type of the operand lhs and rhs

  • Rad1 – Radix (base) type of the operand lhs and rhs

Parameters:
  • lhs – The left hand side operand

  • rhs – The right hand side operand

Returns:

true if lhs and rhs 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 _scales are equal, _values are compared. If _scales are not equal, the number with the larger _scale is shifted to the smaller _scale, and then the _values are compared.

Template Parameters:
  • Rep1 – Representation type of the operand lhs and rhs

  • Rad1 – Radix (base) type of the operand lhs and rhs

Parameters:
  • lhs – The left hand side operand

  • rhs – The right hand side operand

Returns:

true if lhs less than or equal to rhs, 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 _scale is shifted to the smaller _scale, and then the _values are compared.

Template Parameters:
  • Rep1 – Representation type of the operand lhs and rhs

  • Rad1 – Radix (base) type of the operand lhs and rhs

Parameters:
  • lhs – The left hand side operand

  • rhs – The right hand side operand

Returns:

true if lhs greater than or equal to rhs, 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 _scale is shifted to the smaller _scale, and then the _values are compared.

Template Parameters:
  • Rep1 – Representation type of the operand lhs and rhs

  • Rad1 – Radix (base) type of the operand lhs and rhs

Parameters:
  • lhs – The left hand side operand

  • rhs – The right hand side operand

Returns:

true if lhs less than rhs, 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 _scale is shifted to the smaller _scale, and then the _values are compared.

Template Parameters:
  • Rep1 – Representation type of the operand lhs and rhs

  • Rad1 – Radix (base) type of the operand lhs and rhs

Parameters:
  • lhs – The left hand side operand

  • rhs – The right hand side operand

Returns:

true if lhs greater than rhs, 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 _scale is shifted to the smaller _scale, and then the modulus is computed.

Template Parameters:
  • Rep1 – Representation type of the operand lhs and rhs

  • Rad1 – Radix (base) type of the operand lhs and rhs

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 or int64_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

Rep value#

The value of the fixed point number.

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 other fixed_point numbers

Template Parameters:
  • Rep – The representation type (either int32_t or int64_t)

  • Rad – The radix/base (either Radix::BASE_2 or Radix::BASE_10)

Public Types

using rep = Rep#

The representation type.

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 floating point types)

Template Parameters:

T – The floating point 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:

sscaled_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 floating point types.

Template Parameters:

U – The floating point 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 a scaled_integer

Returns:

The scaled_integer representation of the fixed_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 is false, nonzero is true)

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 new scale

The fixed_point number returned will have the same value, underlying representation and radix as this, the only thing changed is the scale.

Parameters:

scale – The scale of the returned fixed_point number

Returns:

fixed_point number with a new scale

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 _scales are equal, _values are added. If _scales are not equal, the number with the larger _scale is shifted to the smaller _scale, and then the _values are added.

Template Parameters:
  • Rep1 – Representation type of the operand lhs and rhs

  • Rad1 – Radix (base) type of the operand lhs and rhs

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 _scales are equal, _values are subtracted. If _scales are not equal, the number with the larger _scale is shifted to the smaller _scale, and then the _values are subtracted.

Template Parameters:
  • Rep1 – Representation type of the operand lhs and rhs

  • Rad1 – Radix (base) type of the operand lhs and rhs

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)

_scales are added and _values are multiplied.

Template Parameters:
  • Rep1 – Representation type of the operand lhs and rhs

  • Rad1 – Radix (base) type of the operand lhs and rhs

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)

_scales are subtracted and _values are divided.

Template Parameters:
  • Rep1 – Representation type of the operand lhs and rhs

  • Rad1 – Radix (base) type of the operand lhs and rhs

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 _scales are equal, the modulus is computed directly. If _scales 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 and rhs

  • Rad1 – Radix (base) type of the operand lhs and rhs

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 _scales are equal, _values are compared. If _scales are not equal, the number with the larger _scale is shifted to the smaller _scale, and then the _values are compared.

Template Parameters:
  • Rep1 – Representation type of the operand lhs and rhs

  • Rad1 – Radix (base) type of the operand lhs and rhs

Parameters:
  • lhs – The left hand side operand

  • rhs – The right hand side operand

Returns:

true if lhs and rhs 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 _scales are equal, _values are compared. If _scales are not equal, the number with the larger _scale is shifted to the smaller _scale, and then the _values are compared.

Template Parameters:
  • Rep1 – Representation type of the operand lhs and rhs

  • Rad1 – Radix (base) type of the operand lhs and rhs

Parameters:
  • lhs – The left hand side operand

  • rhs – The right hand side operand

Returns:

true if lhs and rhs 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 _scales are equal, _values are compared. If _scales are not equal, the number with the larger _scale is shifted to the smaller _scale, and then the _values are compared.

Template Parameters:
  • Rep1 – Representation type of the operand lhs and rhs

  • Rad1 – Radix (base) type of the operand lhs and rhs

Parameters:
  • lhs – The left hand side operand

  • rhs – The right hand side operand

Returns:

true if lhs less than or equal to rhs, 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 _scales are equal, _values are compared. If _scales are not equal, the number with the larger _scale is shifted to the smaller _scale, and then the _values are compared.

Template Parameters:
  • Rep1 – Representation type of the operand lhs and rhs

  • Rad1 – Radix (base) type of the operand lhs and rhs

Parameters:
  • lhs – The left hand side operand

  • rhs – The right hand side operand

Returns:

true if lhs greater than or equal to rhs, 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 _scales are equal, _values are compared. If _scales are not equal, the number with the larger _scale is shifted to the smaller _scale, and then the _values are compared.

Template Parameters:
  • Rep1 – Representation type of the operand lhs and rhs

  • Rad1 – Radix (base) type of the operand lhs and rhs

Parameters:
  • lhs – The left hand side operand

  • rhs – The right hand side operand

Returns:

true if lhs less than rhs, 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 _scales are equal, _values are compared. If _scales are not equal, the number with the larger _scale is shifted to the smaller _scale, and then the _values are compared.

Template Parameters:
  • Rep1 – Representation type of the operand lhs and rhs

  • Rad1 – Radix (base) type of the operand lhs and rhs

Parameters:
  • lhs – The left hand side operand

  • rhs – The right hand side operand

Returns:

true if lhs greater than rhs, false if not