libfxd 0.2.dev
A fixed-point library for C++.
Loading...
Searching...
No Matches
fxd Namespace Reference

This is the namespace where the entire library is defined. More...

Namespaces

namespace  down
 Round down.
 
namespace  except
 Throw on overflow.
 
namespace  expect
 Return fxd::expect::maybe<...> result.
 
namespace  saturate
 Clamp on overflow.
 
namespace  up
 Round up.
 
namespace  zero
 Round to zero.
 

Classes

struct  fixed
 The fixed-point class template. More...
 
struct  is_fixed_point< fixed< Int, Frac, Raw > >
 Test if a type is fxd::fixed. More...
 
struct  is_signed_fixed_point< fixed< Int, Frac, Raw > >
 Test if a type is signed fxd::fixed. More...
 
class  uniform_real_distribution
 Analogous to std::uniform_real_distribution. More...
 

Concepts

concept  fixed_point
 Concept to match any fxd::fixed
 
concept  unsigned_fixed_point
 Concept to match only unsigned fxd::fixed
 
concept  signed_fixed_point
 Concept to match only signed fxd::fixed
 

Typedefs

template<int Int, int Frac>
using ufixed = fixed< Int, Frac, detail::select_uint_t< Int+Frac > >
 Alias for creating unsigned fxd::fixed.
 

Enumerations

enum class  error { underflow , overflow , not_a_number }
 Error values reported by fxd::expect function. More...
 

Functions

template<fixed_point Dst, fixed_point Src>
constexpr Dst fixed_cast (Src src) noexcept
 Convert a fixed point to a different type of fixed point.
 
template<int Int, int Frac, typename Raw = detail::select_int_t<Int + Frac>, fixed_point Src>
constexpr fixed< Int, Frac, Raw > fixed_cast (Src src) noexcept
 Convert a fixed point to a different type of fixed point.
 
template<int Int, int Frac, typename Raw = detail::select_uint_t<Int + Frac>, fixed_point Src>
constexpr fixed< Int, Frac, Raw > ufixed_cast (Src src) noexcept
 Convert a fixed point to a different type of fixed point (unsigned version.)
 
template<fixed_point Fxd>
constexpr bool operator== (Fxd a, Fxd b) noexcept
 Equality check for two similar fxd::fixed.
 
template<fixed_point A, fixed_point B>
constexpr bool operator== (A a, B b) noexcept
 Equality check for dissimilar fxd::fixed.
 
template<fixed_point Fxd, std::integral I>
constexpr bool operator== (Fxd a, I b) noexcept
 fxd::fixed == integer.
 
template<fixed_point Fxd, std::floating_point Flt>
requires (requires (Fxd fxd) {to_float(fxd);})
constexpr bool operator== (Fxd a, Flt b) noexcept
 fxd::fixed == floating-point
 
template<fixed_point Fxd>
constexpr std::strong_ordering operator<=> (Fxd a, Fxd b) noexcept
 
template<fixed_point A, fixed_point B>
constexpr std::strong_ordering operator<=> (A a, B b) noexcept
 
template<fixed_point Fxd, std::integral I>
constexpr std::strong_ordering operator<=> (Fxd a, I b) noexcept
 
template<fixed_point Fxd, std::floating_point Flt>
requires (requires (Fxd a) {to_float(a);})
constexpr std::partial_ordering operator<=> (Fxd a, Flt b) noexcept
 
template<std::integral I, fixed_point Fxd>
constexpr I to_int (Fxd f) noexcept
 Convert to integer.
 
template<fixed_point Fxd>
requires detail::has_int_for<Fxd::int_bits, typename Fxd::raw_type>
constexpr detail::select_int_for< Fxd::int_bits, typename Fxd::raw_type > to_int (Fxd f) noexcept
 Convert to the "natural integer" for this fixed-point type.
 
template<std::floating_point Flt, fixed_point Fxd>
constexpr Flt to_float (Fxd f) noexcept
 Convert a fixed-point to a floating-point type, rounds to zero.
 
template<fixed_point Fxd>
constexpr std::numeric_limits< Fxd >::float_type to_float (Fxd f) noexcept
 Converts a fixed-point to its natural floating-point type (with no losses/rounding).
 
template<fixed_point Fxd>
constexpr Fxd abs (Fxd f) noexcept
 Same as std::abs().
 
template<fixed_point Fxd>
constexpr Fxd fdim (Fxd a, Fxd b) noexcept
 Same as std::fdim().
 
template<fixed_point Fxd>
constexpr Fxd fma (Fxd a, Fxd b, Fxd c) noexcept
 Same as std::fma().
 
template<fixed_point Fxd>
constexpr int ilogb (Fxd x) noexcept
 Same as std::ilogb().
 
template<fixed_point Fxd>
constexpr Fxd midpoint (Fxd a, Fxd b) noexcept
 Same as std::midpoint().
 
template<fixed_point Fxd>
constexpr Fxd nextafter (Fxd from, Fxd to) noexcept
 Same as std::nextafter().
 
template<unsigned_fixed_point Fxd>
requires (Fxd::int_bits > 0 && Fxd::frac_bits >= 0)
constexpr Fxd sqrt (Fxd x) noexcept
 Same as std::sqrt().
 
template<signed_fixed_point Fxd>
requires (Fxd::int_bits > 1 && Fxd::frac_bits >= 0)
constexpr Fxd sqrt (Fxd x) noexcept
 
template<unsigned_fixed_point Fxd>
requires (Fxd::int_bits > 0 && Fxd::frac_bits >= 0)
Fxd sqrt_bin (Fxd x) noexcept
 
template<signed_fixed_point Fxd>
requires (Fxd::int_bits > 1 && Fxd::frac_bits >= 0)
Fxd sqrt_bin (Fxd x) noexcept
 
template<fixed_point Fxd, std::convertible_to< Fxd > T>
constexpr Fxd & operator+= (Fxd &a, T b) noexcept
 In-place addition.
 
template<fixed_point Fxd, std::convertible_to< Fxd > T>
constexpr Fxd & operator-= (Fxd &a, T b) noexcept
 In-place subtraction.
 
template<fixed_point Fxd, std::convertible_to< Fxd > T>
requires (!std::integral<T>)
constexpr Fxd & operator*= (Fxd &a, T b) noexcept
 In-place multiplication.
 
template<fixed_point Fxd, std::integral I>
constexpr Fxd & operator*= (Fxd &a, I b) noexcept
 In-place multiplication by integer.
 
template<fixed_point Fxd, std::convertible_to< Fxd > T>
requires (!std::integral<T>)
constexpr Fxd & operator/= (Fxd &a, T b) noexcept
 In-place division.
 
template<fixed_point Fxd, std::integral I>
constexpr Fxd & operator/= (Fxd &a, I b) noexcept
 In-place division by integer.
 
template<fixed_point Fxd>
constexpr Fxd & operator++ (Fxd &a) noexcept
 Pre-increment.
 
template<fixed_point Fxd>
constexpr Fxd & operator-- (Fxd &a) noexcept
 Pre-decrement.
 
template<fixed_point Fxd>
constexpr Fxd operator++ (Fxd &a, int) noexcept
 Post-increment.
 
template<fixed_point Fxd>
constexpr Fxd operator-- (Fxd &a, int) noexcept
 Post-decrement.
 
template<fixed_point Fxd>
constexpr Fxd operator+ (Fxd a) noexcept
 Unary +.
 
template<fixed_point Fxd>
constexpr Fxd operator- (Fxd a) noexcept
 Negation.
 
template<typename A , typename B >
requires (fixed_point<A> || fixed_point<B>)
constexpr std::common_type_t< A, B > operator+ (A a, B b) noexcept
 Addition.
 
template<typename A , typename B >
requires (fixed_point<A> || fixed_point<B>)
constexpr std::common_type_t< A, B > operator- (A a, B b) noexcept
 Subtraction.
 
template<fixed_point A, fixed_point B>
constexpr std::common_type_t< A, B > operator* (A a, B b) noexcept
 Multiply: fxd::fixed * fxd::fixed
 
template<fixed_point Fxd, std::integral I>
constexpr Fxd operator* (Fxd a, I b) noexcept
 Multiply: fxd::fixed * integer.
 
template<std::integral I, fixed_point Fxd>
constexpr Fxd operator* (I a, Fxd b) noexcept
 Multiply: integer * fxd::fixed
 
template<fixed_point Fxd, std::floating_point Flt>
constexpr Fxd operator* (Fxd a, Flt b) noexcept
 Multiply: fxd::fixed * floating-point
 
template<std::floating_point Flt, fixed_point Fxd>
constexpr Fxd operator* (Flt a, Fxd b) noexcept
 Multiply: floating-point * fxd::fixed
 
template<fixed_point A, fixed_point B>
constexpr std::common_type_t< A, B > operator/ (A a, B b) noexcept
 Divide: fxd::fixed / fxd::fixed
 
template<fixed_point Fxd, std::integral I>
constexpr Fxd operator/ (Fxd a, I b) noexcept
 Divide: fxd::fixed / integer.
 
template<std::integral I, fixed_point Fxd>
constexpr Fxd operator/ (I a, Fxd b) noexcept
 Divide: integer / fxd::fixed
 
template<fixed_point Fxd, std::floating_point Flt>
constexpr Fxd operator/ (Fxd a, Flt b) noexcept
 Divide: fxd::fixed / floating-point
 
template<std::floating_point Flt, fixed_point Fxd>
constexpr Fxd operator/ (Flt a, Fxd b) noexcept
 Divide: floating-point / fxd::fixed.
 
template<typename CharT , typename Traits , fixed_point Fxd>
std::basic_ostream< CharT, Traits > & operator<< (std::basic_ostream< CharT, Traits > &out, Fxd f)
 Output (by converting to a floating-point value).
 
template<typename CharT , typename Traits , fixed_point Fxd>
std::basic_istream< CharT, Traits > & operator>> (std::basic_istream< CharT, Traits > &in, Fxd &f)
 Input (by converting from a floatiung-point value).
 

Variables

template<typename T >
constexpr bool is_fixed_point_v = is_fixed_point<T>::value
 Helper template variable for fxd::is_fixed_point
 
template<typename T >
constexpr bool is_signed_fixed_point_v = is_signed_fixed_point<T>::value
 Helper template variable for fxd::is_signed_fixed_point
 

Detailed Description

This is the namespace where the entire library is defined.

Typedef Documentation

◆ ufixed

using ufixed = fixed<Int, Frac, detail::select_uint_t<Int + Frac> >

Alias for creating unsigned fxd::fixed.

Definition at line 131 of file fixed.hpp.

Enumeration Type Documentation

◆ error

enum class error
strong

Error values reported by fxd::expect function.

Enumerator
underflow 
overflow 
not_a_number 

Definition at line 14 of file error.hpp.

Function Documentation

◆ abs()

constexpr Fxd abs ( Fxd  f)
constexprnoexcept

Same as std::abs().

Definition at line 38 of file math.hpp.

◆ fdim()

constexpr Fxd fdim ( Fxd  a,
Fxd  b 
)
constexprnoexcept

Same as std::fdim().

Definition at line 49 of file math.hpp.

◆ fixed_cast() [1/2]

constexpr Dst fixed_cast ( Src  src)
constexprnoexcept

Convert a fixed point to a different type of fixed point.

Definition at line 28 of file casting.hpp.

◆ fixed_cast() [2/2]

constexpr fixed< Int, Frac, Raw > fixed_cast ( Src  src)
constexprnoexcept

Convert a fixed point to a different type of fixed point.

Definition at line 68 of file casting.hpp.

◆ fma()

constexpr Fxd fma ( Fxd  a,
Fxd  b,
Fxd  c 
)
constexprnoexcept

Same as std::fma().

Definition at line 64 of file math.hpp.

◆ ilogb()

constexpr int ilogb ( Fxd  x)
constexprnoexcept

Same as std::ilogb().

Definition at line 77 of file math.hpp.

◆ midpoint()

constexpr Fxd midpoint ( Fxd  a,
Fxd  b 
)
constexprnoexcept

Same as std::midpoint().

Definition at line 158 of file math.hpp.

◆ nextafter()

constexpr Fxd nextafter ( Fxd  from,
Fxd  to 
)
constexprnoexcept

Same as std::nextafter().

Definition at line 171 of file math.hpp.

◆ operator*() [1/5]

constexpr std::common_type_t< A, B > operator* ( a,
b 
)
constexprnoexcept

Multiply: fxd::fixed * fxd::fixed

Definition at line 251 of file operators.hpp.

◆ operator*() [2/5]

constexpr Fxd operator* ( Flt  a,
Fxd  b 
)
constexprnoexcept

Multiply: floating-point * fxd::fixed

Definition at line 304 of file operators.hpp.

◆ operator*() [3/5]

constexpr Fxd operator* ( Fxd  a,
Flt  b 
)
constexprnoexcept

Multiply: fxd::fixed * floating-point

Definition at line 290 of file operators.hpp.

◆ operator*() [4/5]

constexpr Fxd operator* ( Fxd  a,
b 
)
constexprnoexcept

Multiply: fxd::fixed * integer.

Definition at line 265 of file operators.hpp.

◆ operator*() [5/5]

constexpr Fxd operator* ( a,
Fxd  b 
)
constexprnoexcept

Multiply: integer * fxd::fixed

Definition at line 278 of file operators.hpp.

◆ operator*=() [1/2]

constexpr Fxd & operator*= ( Fxd &  a,
b 
)
constexprnoexcept

In-place multiplication by integer.

Definition at line 92 of file operators.hpp.

◆ operator*=() [2/2]

constexpr Fxd & operator*= ( Fxd &  a,
b 
)
constexprnoexcept

In-place multiplication.

Definition at line 79 of file operators.hpp.

◆ operator+() [1/2]

constexpr std::common_type_t< A, B > operator+ ( a,
b 
)
constexprnoexcept

Addition.

Definition at line 218 of file operators.hpp.

◆ operator+() [2/2]

constexpr Fxd operator+ ( Fxd  a)
constexprnoexcept

Unary +.

Definition at line 194 of file operators.hpp.

◆ operator++() [1/2]

constexpr Fxd & operator++ ( Fxd &  a)
constexprnoexcept

Pre-increment.

Definition at line 138 of file operators.hpp.

◆ operator++() [2/2]

constexpr Fxd operator++ ( Fxd &  a,
int   
)
constexprnoexcept

Post-increment.

Definition at line 162 of file operators.hpp.

◆ operator+=()

constexpr Fxd & operator+= ( Fxd &  a,
b 
)
constexprnoexcept

In-place addition.

Definition at line 50 of file operators.hpp.

◆ operator-() [1/2]

constexpr std::common_type_t< A, B > operator- ( a,
b 
)
constexprnoexcept

Subtraction.

Definition at line 235 of file operators.hpp.

◆ operator-() [2/2]

constexpr Fxd operator- ( Fxd  a)
constexprnoexcept

Negation.

Definition at line 205 of file operators.hpp.

◆ operator--() [1/2]

constexpr Fxd & operator-- ( Fxd &  a)
constexprnoexcept

Pre-decrement.

Definition at line 150 of file operators.hpp.

◆ operator--() [2/2]

constexpr Fxd operator-- ( Fxd &  a,
int   
)
constexprnoexcept

Post-decrement.

Definition at line 175 of file operators.hpp.

◆ operator-=()

constexpr Fxd & operator-= ( Fxd &  a,
b 
)
constexprnoexcept

In-place subtraction.

Definition at line 64 of file operators.hpp.

◆ operator/() [1/5]

constexpr std::common_type_t< A, B > operator/ ( a,
b 
)
constexprnoexcept

Divide: fxd::fixed / fxd::fixed

Definition at line 319 of file operators.hpp.

◆ operator/() [2/5]

constexpr Fxd operator/ ( Flt  a,
Fxd  b 
)
constexprnoexcept

Divide: floating-point / fxd::fixed.

Definition at line 372 of file operators.hpp.

◆ operator/() [3/5]

constexpr Fxd operator/ ( Fxd  a,
Flt  b 
)
constexprnoexcept

Divide: fxd::fixed / floating-point

Definition at line 359 of file operators.hpp.

◆ operator/() [4/5]

constexpr Fxd operator/ ( Fxd  a,
b 
)
constexprnoexcept

Divide: fxd::fixed / integer.

Definition at line 333 of file operators.hpp.

◆ operator/() [5/5]

constexpr Fxd operator/ ( a,
Fxd  b 
)
constexprnoexcept

Divide: integer / fxd::fixed

Definition at line 346 of file operators.hpp.

◆ operator/=() [1/2]

constexpr Fxd & operator/= ( Fxd &  a,
b 
)
constexprnoexcept

In-place division by integer.

Definition at line 120 of file operators.hpp.

◆ operator/=() [2/2]

constexpr Fxd & operator/= ( Fxd &  a,
b 
)
constexprnoexcept

In-place division.

Definition at line 107 of file operators.hpp.

◆ operator<<()

std::basic_ostream< CharT, Traits > & operator<< ( std::basic_ostream< CharT, Traits > &  out,
Fxd  f 
)

Output (by converting to a floating-point value).

Definition at line 372 of file operators.hpp.

◆ operator<=>() [1/4]

constexpr std::strong_ordering operator<=> ( a,
b 
)
constexprnoexcept

Definition at line 133 of file compare.hpp.

◆ operator<=>() [2/4]

constexpr std::partial_ordering operator<=> ( Fxd  a,
Flt  b 
)
constexprnoexcept

Definition at line 215 of file compare.hpp.

◆ operator<=>() [3/4]

constexpr std::strong_ordering operator<=> ( Fxd  a,
Fxd  b 
)
constexprnoexcept

Definition at line 116 of file compare.hpp.

◆ operator<=>() [4/4]

constexpr std::strong_ordering operator<=> ( Fxd  a,
b 
)
constexprnoexcept

Definition at line 188 of file compare.hpp.

◆ operator==() [1/4]

constexpr bool operator== ( a,
b 
)
constexprnoexcept

Equality check for dissimilar fxd::fixed.

Definition at line 39 of file compare.hpp.

◆ operator==() [2/4]

constexpr bool operator== ( Fxd  a,
Flt  b 
)
constexprnoexcept

fxd::fixed == floating-point

Definition at line 104 of file compare.hpp.

◆ operator==() [3/4]

constexpr bool operator== ( Fxd  a,
Fxd  b 
)
constexprnoexcept

Equality check for two similar fxd::fixed.

Definition at line 25 of file compare.hpp.

◆ operator==() [4/4]

constexpr bool operator== ( Fxd  a,
b 
)
constexprnoexcept

fxd::fixed == integer.

Definition at line 79 of file compare.hpp.

◆ operator>>()

std::basic_istream< CharT, Traits > & operator>> ( std::basic_istream< CharT, Traits > &  in,
Fxd &  f 
)

Input (by converting from a floatiung-point value).

Definition at line 402 of file operators.hpp.

◆ sqrt() [1/2]

constexpr Fxd sqrt ( Fxd  x)
constexprnoexcept

Same as std::sqrt().

Note
If the argument x is negative, errno is set to EDOM.
Examples
square-root.cpp.

Definition at line 194 of file math.hpp.

◆ sqrt() [2/2]

constexpr Fxd sqrt ( Fxd  x)
constexprnoexcept

Definition at line 244 of file math.hpp.

◆ sqrt_bin() [1/2]

Fxd sqrt_bin ( Fxd  x)
noexcept
Examples
square-root.cpp.

Definition at line 261 of file math.hpp.

◆ sqrt_bin() [2/2]

Fxd sqrt_bin ( Fxd  x)
noexcept

Definition at line 352 of file math.hpp.

◆ to_float() [1/2]

constexpr Flt to_float ( Fxd  f)
constexprnoexcept

Convert a fixed-point to a floating-point type, rounds to zero.

Definition at line 98 of file conversions.hpp.

◆ to_float() [2/2]

constexpr std::numeric_limits< Fxd >::float_type to_float ( Fxd  f)
constexprnoexcept

Converts a fixed-point to its natural floating-point type (with no losses/rounding).

Definition at line 112 of file conversions.hpp.

◆ to_int() [1/2]

constexpr I to_int ( Fxd  f)
constexprnoexcept

Convert to integer.

Definition at line 45 of file conversions.hpp.

◆ to_int() [2/2]

constexpr detail::select_int_for< Fxd::int_bits, typename Fxd::raw_type > to_int ( Fxd  f)
constexprnoexcept

Convert to the "natural integer" for this fixed-point type.

Definition at line 72 of file conversions.hpp.

◆ ufixed_cast()

constexpr fixed< Int, Frac, Raw > ufixed_cast ( Src  src)
constexprnoexcept

Convert a fixed point to a different type of fixed point (unsigned version.)

Definition at line 83 of file casting.hpp.

Variable Documentation

◆ is_fixed_point_v

constexpr bool is_fixed_point_v = is_fixed_point<T>::value
inlineconstexpr

Helper template variable for fxd::is_fixed_point

Definition at line 35 of file traits.hpp.

◆ is_signed_fixed_point_v

constexpr bool is_signed_fixed_point_v = is_signed_fixed_point<T>::value
inlineconstexpr

Helper template variable for fxd::is_signed_fixed_point

Definition at line 51 of file traits.hpp.