libfxd 0.2.dev
A fixed-point library for C++.
Loading...
Searching...
No Matches
fixed< Int, Frac, Raw > Struct Template Reference

The fixed-point class template. More...

#include <fixed.hpp>

Public Types

using raw_type = Raw
 The integral type that will hold the raw representation of the value.
 

Public Member Functions

constexpr fixed () noexcept=default
 Defaulted constructor.
 
template<std::integral I>
constexpr fixed (I i) noexcept
 Conversion from integer.
 
template<std::floating_point Flt>
constexpr fixed (Flt f) noexcept
 Conversion from floating-point.
 
template<int Int2, int Frac2, typename Raw2 >
requires (detail::is_safe_conversion_v<Int2, Frac2, Raw2, Int, Frac, Raw>)
constexpr fixed (const fixed< Int2, Frac2, Raw2 > &other) noexcept
 Conversion from another fixed-point.
 
constexpr operator bool () const noexcept
 Check if non-zero.
 
template<std::integral I>
constexpr operator I () const noexcept
 Conversion to integer.
 
template<std::floating_point F>
constexpr operator F () const noexcept
 Conversion to floating-point.
 
template<int Int2, int Frac2, typename Raw2 >
constexpr operator fixed< Int2, Frac2, Raw2 > () const noexcept
 Conversion to another fixed-point.
 

Static Public Member Functions

static constexpr fixed from_raw (raw_type val) noexcept
 Constructs a fixed from any raw bit representation (no conversion.)
 

Public Attributes

raw_type raw_value: bits
 The raw value, stored as a bitfield.
 

Static Public Attributes

static constexpr int int_bits = Int
 How many integral bits were requested.
 
static constexpr int frac_bits = Frac
 How many fractional bits were requested.
 
static constexpr int bits = int_bits + frac_bits
 How many bits were requested in total.
 
static constexpr int raw_bits
 How many bits are used for storage (can be larger than bits).
 

Detailed Description

template<int Int, int Frac, typename Raw = detail::select_int_t<Int + Frac>>
struct fxd::fixed< Int, Frac, Raw >

The fixed-point class template.

This class encapsulates an integer so it supports the arithmetic operations. It mimics floating-point types, but direct access to the internal integer can be done through the .raw_value member variable.

Template Parameters
IntHow many integral bits to use.
FracHow many fractional bits to use.
RawThe integral type that will be used to store the value. By default, it's a signed integer with at least Int + Frac bits.
See also
ufixed.
Examples
basic.cpp, closest-pair.cpp, matrix-mul.cpp, and square-root.cpp.

Definition at line 38 of file fixed.hpp.

Member Typedef Documentation

◆ raw_type

using raw_type = Raw

The integral type that will hold the raw representation of the value.

Definition at line 41 of file fixed.hpp.

Constructor & Destructor Documentation

◆ fixed() [1/4]

constexpr fixed ( )
constexprdefaultnoexcept

Defaulted constructor.

◆ fixed() [2/4]

constexpr fixed ( i)
constexprnoexcept

Conversion from integer.

The i value will be shifted (and rounded to zero, if negative) if needed.

Definition at line 36 of file constructors.hpp.

◆ fixed() [3/4]

constexpr fixed ( Flt  f)
constexprnoexcept

Conversion from floating-point.

Definition at line 66 of file constructors.hpp.

◆ fixed() [4/4]

constexpr fixed ( const fixed< Int2, Frac2, Raw2 > &  other)
constexprnoexcept

Conversion from another fixed-point.

Definition at line 80 of file constructors.hpp.

Member Function Documentation

◆ from_raw()

constexpr fixed< Int, Frac, Raw > from_raw ( raw_type  val)
staticconstexprnoexcept

Constructs a fixed from any raw bit representation (no conversion.)

All excess bits of val will be truncated. Use this when you know the exact bit representation needed for the fixed object.

Definition at line 97 of file constructors.hpp.

◆ operator bool()

constexpr operator bool
explicitconstexprnoexcept

Check if non-zero.

Definition at line 32 of file conversions.hpp.

◆ operator F()

constexpr operator F
explicitconstexprnoexcept

Conversion to floating-point.

Definition at line 125 of file conversions.hpp.

◆ operator fixed< Int2, Frac2, Raw2 >()

constexpr operator fixed< Int2, Frac2, Raw2 >
explicitconstexprnoexcept

Conversion to another fixed-point.

Definition at line 136 of file conversions.hpp.

◆ operator I()

constexpr operator I
explicitconstexprnoexcept

Conversion to integer.

Definition at line 86 of file conversions.hpp.

Member Data Documentation

◆ bits

constexpr int bits = int_bits + frac_bits
staticconstexpr

How many bits were requested in total.

Definition at line 52 of file fixed.hpp.

◆ frac_bits

constexpr int frac_bits = Frac
staticconstexpr

How many fractional bits were requested.

Definition at line 49 of file fixed.hpp.

◆ int_bits

constexpr int int_bits = Int
staticconstexpr

How many integral bits were requested.

Definition at line 46 of file fixed.hpp.

◆ raw_bits

constexpr int raw_bits
staticconstexpr
Initial value:
=
detail::type_width<raw_type>

How many bits are used for storage (can be larger than bits).

Definition at line 55 of file fixed.hpp.

◆ raw_value

raw_type raw_value

The raw value, stored as a bitfield.

Examples
basic.cpp.

Definition at line 65 of file fixed.hpp.


The documentation for this struct was generated from the following files: