ToyGine2 26.2.0
Game Engine for retro consoles
Loading...
Searching...
No Matches
toy::math::fixed< Base, Intermediate, Fraction, Rounding > Class Template Reference

Fixed-point numeric type with configurable storage and fractional precision. More...

Classes

class  RawConstructorTag
 Internal tag for constructing from raw storage. More...

Public Member Functions

constexpr fixed () noexcept=default
 Default constructor.
template<integral T>
constexpr fixed (const T &value) noexcept
 Constructs from an integral value.
template<floating_point T>
constexpr fixed (const T &value) noexcept
 Constructs from a floating-point value.
template<typename OtherBase, typename OtherIntermediate, unsigned OtherFraction, bool OtherRounding>
constexpr fixed (const fixed< OtherBase, OtherIntermediate, OtherFraction, OtherRounding > &value) noexcept
 Constructs by converting from another toy::math::fixed type.
template<integral T>
constexpr operator T () const noexcept
 Converts to an integral type.
template<floating_point T>
constexpr operator T () const noexcept
 Converts to a floating-point type.
constexpr Base rawValue () const noexcept
 Returns raw fixed-point storage value.
template<bool OtherRounding>
constexpr fixedoperator= (const fixed< Base, Intermediate, Fraction, OtherRounding > &other) noexcept
 Assigns from another toy::math::fixed with the same Base, Intermediate, and Fraction.
template<integral T>
constexpr fixedoperator= (const T &other) noexcept
 Assigns from an integral value.
template<bool OtherRounding>
constexpr fixedoperator+= (const fixed< Base, Intermediate, Fraction, OtherRounding > &other) noexcept
 Adds another toy::math::fixed value in place.
template<integral T>
constexpr fixedoperator+= (const T &other) noexcept
 Adds an integral value in place.
template<bool OtherRounding>
constexpr fixedoperator-= (const fixed< Base, Intermediate, Fraction, OtherRounding > &other) noexcept
 Subtracts another toy::math::fixed value in place.
template<integral T>
constexpr fixedoperator-= (const T &other) noexcept
 Subtracts an integral value in place.
template<bool OtherRounding>
constexpr fixedoperator*= (const fixed< Base, Intermediate, Fraction, OtherRounding > &other) noexcept
 Multiplies by another toy::math::fixed value in place.
template<integral T>
constexpr fixedoperator*= (const T &other) noexcept
 Multiplies by an integral value in place.
template<bool OtherRounding>
constexpr fixedoperator/= (const fixed< Base, Intermediate, Fraction, OtherRounding > &other) noexcept
 Divides by another toy::math::fixed value in place.
template<integral T>
constexpr fixedoperator/= (const T &other) noexcept
 Divides by an integral value in place.
template<unsigned NumFractionBits, integral T>
constexpr fixed< Base, Intermediate, Fraction, Rounding > fromFixedPoint (const T &value) noexcept
template<bool OtherRounding>
constexpr fixed< Base, Intermediate, Fraction, Rounding > & operator= (const fixed< Base, Intermediate, Fraction, OtherRounding > &other) noexcept
template<integral T>
constexpr fixed< Base, Intermediate, Fraction, Rounding > & operator= (const T &other) noexcept
template<bool OtherRounding>
constexpr fixed< Base, Intermediate, Fraction, Rounding > & operator+= (const fixed< Base, Intermediate, Fraction, OtherRounding > &other) noexcept
template<integral T>
constexpr fixed< Base, Intermediate, Fraction, Rounding > & operator+= (const T &other) noexcept
template<bool OtherRounding>
constexpr fixed< Base, Intermediate, Fraction, Rounding > & operator-= (const fixed< Base, Intermediate, Fraction, OtherRounding > &other) noexcept
template<integral T>
constexpr fixed< Base, Intermediate, Fraction, Rounding > & operator-= (const T &other) noexcept
template<bool OtherRounding>
constexpr fixed< Base, Intermediate, Fraction, Rounding > & operator*= (const fixed< Base, Intermediate, Fraction, OtherRounding > &other) noexcept
template<integral T>
constexpr fixed< Base, Intermediate, Fraction, Rounding > & operator*= (const T &other) noexcept
template<bool OtherRounding>
constexpr fixed< Base, Intermediate, Fraction, Rounding > & operator/= (const fixed< Base, Intermediate, Fraction, OtherRounding > &other) noexcept
template<integral T>
constexpr fixed< Base, Intermediate, Fraction, Rounding > & operator/= (const T &other) noexcept

Static Public Member Functions

static constexpr fixed fromRawValue (const Base &value) noexcept
 Creates a fixed-point value from raw storage bits.
template<unsigned NumFractionBits, integral T>
static constexpr fixed fromFixedPoint (const T &value) noexcept
 Builds a toy::math::fixed from a raw value with a different number of fraction bits.
template<unsigned NumFractionBits, integral T>
static constexpr fixed fromFixedPoint (const T &value) noexcept
 Builds a toy::math::fixed from a raw value with fewer or equal fraction bits.

Private Member Functions

constexpr fixed (const Base &val, RawConstructorTag) noexcept
 Constructs directly from raw storage value.

Static Private Member Functions

static consteval Intermediate _fractionMult () noexcept
 Compile-time scaling factor 2^Fraction.

Private Attributes

Base _value
 Raw fixed-point storage value (scaled by 2^Fraction).

Detailed Description

template<typename Base, typename Intermediate, unsigned Fraction, bool Rounding = true>
requires ValidFixedPointTypes<Base, Intermediate, Fraction>
class toy::math::fixed< Base, Intermediate, Fraction, Rounding >

Fixed-point numeric type with configurable storage and fractional precision.

Stores values as scaled integers using Fraction fractional bits. Conversions from integral and floating-point types are explicit.

Template Parameters
BaseStorage type for raw fixed-point value.
IntermediateWider type used for intermediate calculations.
FractionNumber of fractional bits.
RoundingIf true, floating-point construction rounds to nearest value.

Key Features

  • Constexpr: All operations and constructors are constexpr; usable at compile time.
  • Configurable precision: Base and Fraction choose storage size and fractional bits.
  • No exceptions: All operations are noexcept; no dynamic allocation.
  • Type safety: toy::math::ValidFixedPointTypes concept constrains template parameters.
  • Rounding policy: Optional rounding when converting from floating-point or scaling down fraction bits.

Usage Example

#include "math.hpp"
constexpr fixed_type a(2);
constexpr fixed_type b(3);
constexpr auto sum = a + b;
constexpr auto raw = fixed_type::fromRawValue(256);
Fixed-point numeric type with configurable storage and fractional precision.
Definition fixed.hpp:110
Umbrella header for the math module.

Performance Characteristics

  • Construction, conversion, arithmetic: O(1). All operations are integer-based.
  • Memory: sizeof(Base); no heap allocation.

Safety Guarantees

  • Contracts: Division by zero is checked in debug builds via assert_message; undefined behavior if divisor is zero in release.
  • Exception safety: All operations are noexcept; no exceptions thrown.
  • Type safety: Explicit conversions only; no implicit narrowing.

Compatibility

  • C++ standard: C++20 (concepts, constexpr, consteval).
  • Embedded: No heap allocation; suitable for constrained environments.
See also
toy::math::ValidFixedPointTypes

Constructor & Destructor Documentation

◆ fixed() [1/5]

template<typename Base, typename Intermediate, unsigned Fraction, bool Rounding = true>
toy::math::fixed< Base, Intermediate, Fraction, Rounding >::fixed ( )
constexprdefaultnoexcept

Default constructor.

Leaves storage uninitialized. Use explicit construction or assignment before use.

◆ fixed() [2/5]

template<typename Base, typename Intermediate, unsigned Fraction, bool Rounding>
template<floating_point T>
toy::math::fixed< Base, Intermediate, Fraction, Rounding >::fixed ( const T & value)
explicitconstexprnoexcept

Constructs from an integral value.

Parameters
valueSource integral value.

◆ fixed() [3/5]

template<typename Base, typename Intermediate, unsigned Fraction, bool Rounding = true>
template<floating_point T>
toy::math::fixed< Base, Intermediate, Fraction, Rounding >::fixed ( const T & value)
explicitconstexprnoexcept

Constructs from a floating-point value.

Parameters
valueSource floating-point value.

◆ fixed() [4/5]

template<typename Base, typename Intermediate, unsigned Fraction, bool Rounding>
template<typename OtherBase, typename OtherIntermediate, unsigned OtherFraction, bool OtherRounding>
toy::math::fixed< Base, Intermediate, Fraction, Rounding >::fixed ( const fixed< OtherBase, OtherIntermediate, OtherFraction, OtherRounding > & value)
explicitconstexprnoexcept

Constructs by converting from another toy::math::fixed type.

The source may have different Base, Intermediate, fraction bits, or rounding policy. The value is converted to this instance's Fraction using fromFixedPoint; when the source has more fraction bits, rounding is applied if Rounding is true.

Template Parameters
OtherBaseStorage type of the source fixed.
OtherIntermediateIntermediate type of the source fixed.
OtherFractionNumber of fraction bits of the source fixed.
OtherRoundingRounding policy of the source fixed.
Parameters
valueSource fixed-point value.

◆ fixed() [5/5]

template<typename Base, typename Intermediate, unsigned Fraction, bool Rounding>
toy::math::fixed< Base, Intermediate, Fraction, Rounding >::fixed ( const Base & val,
RawConstructorTag  )
constexprprivatenoexcept

Constructs directly from raw storage value.

Parameters
valRaw fixed-point storage value.

Member Function Documentation

◆ _fractionMult()

template<typename Base, typename Intermediate, unsigned Fraction, bool Rounding>
Intermediate toy::math::fixed< Base, Intermediate, Fraction, Rounding >::_fractionMult ( )
staticconstevalprivatenoexcept

Compile-time scaling factor 2^Fraction.

◆ fromFixedPoint() [1/3]

template<typename Base, typename Intermediate, unsigned Fraction, bool Rounding = true>
template<unsigned NumFractionBits, integral T>
constexpr fixed toy::math::fixed< Base, Intermediate, Fraction, Rounding >::fromFixedPoint ( const T & value)
staticnodiscardconstexprnoexcept

Builds a toy::math::fixed from a raw value with fewer or equal fraction bits.

When NumFractionBits is less than Fraction, the value is scaled up (left-shifted); no rounding is needed. When NumFractionBits equals Fraction, the raw value is used as-is.

Template Parameters
NumFractionBitsNumber of fraction bits of the source raw value.
TIntegral type of the source raw value.
Parameters
valueRaw fixed-point value in NumFractionBits fractional bits.
Returns
Fixed-point value with this instance's Fraction and Rounding.

◆ fromFixedPoint() [2/3]

template<typename Base, typename Intermediate, unsigned Fraction, bool Rounding = true>
template<unsigned NumFractionBits, integral T>
constexpr fixed toy::math::fixed< Base, Intermediate, Fraction, Rounding >::fromFixedPoint ( const T & value)
staticnodiscardconstexprnoexcept

Builds a toy::math::fixed from a raw value with a different number of fraction bits.

When NumFractionBits is greater than Fraction, the value is scaled down (right-shifted). If Rounding is true, the result is rounded to the nearest representable value, otherwise the result is truncated.

Template Parameters
NumFractionBitsNumber of fraction bits of the source raw value.
TIntegral type of the source raw value.
Parameters
valueRaw fixed-point value in NumFractionBits fractional bits.
Returns
Fixed-point value with this instance's Fraction and Rounding.

◆ fromFixedPoint() [3/3]

template<typename Base, typename Intermediate, unsigned Fraction, bool Rounding>
template<unsigned NumFractionBits, integral T>
fixed< Base, Intermediate, Fraction, Rounding > toy::math::fixed< Base, Intermediate, Fraction, Rounding >::fromFixedPoint ( const T & value)
constexprnoexcept

◆ fromRawValue()

template<typename Base, typename Intermediate, unsigned Fraction, bool Rounding>
fixed< Base, Intermediate, Fraction, Rounding > toy::math::fixed< Base, Intermediate, Fraction, Rounding >::fromRawValue ( const Base & value)
staticnodiscardconstexprnoexcept

Creates a fixed-point value from raw storage bits.

Parameters
valueRaw fixed-point value.
Returns
Fixed-point value with raw storage set to value.

◆ operator T() [1/2]

template<typename Base, typename Intermediate, unsigned Fraction, bool Rounding = true>
template<floating_point T>
toy::math::fixed< Base, Intermediate, Fraction, Rounding >::operator T ( ) const
explicitconstexprnoexcept

Converts to a floating-point type.

Returns
Value converted to T.

◆ operator T() [2/2]

template<typename Base, typename Intermediate, unsigned Fraction, bool Rounding>
template<floating_point T>
toy::math::fixed< Base, Intermediate, Fraction, Rounding >::operator T ( ) const
explicitconstexprnoexcept

Converts to an integral type.

Returns
Value converted to T.

◆ operator*=() [1/4]

template<typename Base, typename Intermediate, unsigned Fraction, bool Rounding = true>
template<bool OtherRounding>
fixed & toy::math::fixed< Base, Intermediate, Fraction, Rounding >::operator*= ( const fixed< Base, Intermediate, Fraction, OtherRounding > & other)
constexprnoexcept

Multiplies by another toy::math::fixed value in place.

Accepts any toy::math::fixed with the same Base, Intermediate, and Fraction, regardless of Rounding. Uses raw storage; when Rounding is true, the result is rounded to the nearest representable value.

Template Parameters
OtherRoundingRounding of the right-hand side type (may differ from this instance).
Parameters
otherValue to multiply by.
Returns
Reference to *this.

◆ operator*=() [2/4]

template<typename Base, typename Intermediate, unsigned Fraction, bool Rounding = true>
template<bool OtherRounding>
fixed< Base, Intermediate, Fraction, Rounding > & toy::math::fixed< Base, Intermediate, Fraction, Rounding >::operator*= ( const fixed< Base, Intermediate, Fraction, OtherRounding > & other)
constexprnoexcept

◆ operator*=() [3/4]

template<typename Base, typename Intermediate, unsigned Fraction, bool Rounding = true>
template<integral T>
fixed & toy::math::fixed< Base, Intermediate, Fraction, Rounding >::operator*= ( const T & other)
constexprnoexcept

Multiplies by an integral value in place.

other scales the fixed-point value (whole units). Raw storage is multiplied by other directly.

Template Parameters
TIntegral type.
Parameters
otherMultiplier (fixed-point whole units).
Returns
Reference to *this.

◆ operator*=() [4/4]

template<typename Base, typename Intermediate, unsigned Fraction, bool Rounding = true>
template<integral T>
fixed< Base, Intermediate, Fraction, Rounding > & toy::math::fixed< Base, Intermediate, Fraction, Rounding >::operator*= ( const T & other)
constexprnoexcept

◆ operator+=() [1/4]

template<typename Base, typename Intermediate, unsigned Fraction, bool Rounding = true>
template<bool OtherRounding>
fixed & toy::math::fixed< Base, Intermediate, Fraction, Rounding >::operator+= ( const fixed< Base, Intermediate, Fraction, OtherRounding > & other)
constexprnoexcept

Adds another toy::math::fixed value in place.

Accepts any toy::math::fixed with the same Base, Intermediate, and Fraction, regardless of Rounding. The operation uses raw storage; rounding policy of other does not affect the result.

Template Parameters
OtherRoundingRounding of the right-hand side type (may differ from this instance).
Parameters
otherValue to add.
Returns
Reference to *this.

◆ operator+=() [2/4]

template<typename Base, typename Intermediate, unsigned Fraction, bool Rounding = true>
template<bool OtherRounding>
fixed< Base, Intermediate, Fraction, Rounding > & toy::math::fixed< Base, Intermediate, Fraction, Rounding >::operator+= ( const fixed< Base, Intermediate, Fraction, OtherRounding > & other)
constexprnoexcept

◆ operator+=() [3/4]

template<typename Base, typename Intermediate, unsigned Fraction, bool Rounding = true>
template<integral T>
fixed & toy::math::fixed< Base, Intermediate, Fraction, Rounding >::operator+= ( const T & other)
constexprnoexcept

Adds an integral value in place.

other is scaled by 2^Fraction and added to the raw storage.

Template Parameters
TIntegral type.
Parameters
otherValue to add (interpreted as fixed-point whole units).
Returns
Reference to *this.

◆ operator+=() [4/4]

template<typename Base, typename Intermediate, unsigned Fraction, bool Rounding = true>
template<integral T>
fixed< Base, Intermediate, Fraction, Rounding > & toy::math::fixed< Base, Intermediate, Fraction, Rounding >::operator+= ( const T & other)
constexprnoexcept

◆ operator-=() [1/4]

template<typename Base, typename Intermediate, unsigned Fraction, bool Rounding = true>
template<bool OtherRounding>
fixed & toy::math::fixed< Base, Intermediate, Fraction, Rounding >::operator-= ( const fixed< Base, Intermediate, Fraction, OtherRounding > & other)
constexprnoexcept

Subtracts another toy::math::fixed value in place.

Accepts any toy::math::fixed with the same Base, Intermediate, and Fraction, regardless of Rounding. The operation uses raw storage; rounding policy of other does not affect the result.

Template Parameters
OtherRoundingRounding of the right-hand side type (may differ from this instance).
Parameters
otherValue to subtract.
Returns
Reference to *this.

◆ operator-=() [2/4]

template<typename Base, typename Intermediate, unsigned Fraction, bool Rounding = true>
template<bool OtherRounding>
fixed< Base, Intermediate, Fraction, Rounding > & toy::math::fixed< Base, Intermediate, Fraction, Rounding >::operator-= ( const fixed< Base, Intermediate, Fraction, OtherRounding > & other)
constexprnoexcept

◆ operator-=() [3/4]

template<typename Base, typename Intermediate, unsigned Fraction, bool Rounding = true>
template<integral T>
fixed & toy::math::fixed< Base, Intermediate, Fraction, Rounding >::operator-= ( const T & other)
constexprnoexcept

Subtracts an integral value in place.

other is scaled by 2^Fraction and subtracted from the raw storage.

Template Parameters
TIntegral type.
Parameters
otherValue to subtract (interpreted as fixed-point whole units).
Returns
Reference to *this.

◆ operator-=() [4/4]

template<typename Base, typename Intermediate, unsigned Fraction, bool Rounding = true>
template<integral T>
fixed< Base, Intermediate, Fraction, Rounding > & toy::math::fixed< Base, Intermediate, Fraction, Rounding >::operator-= ( const T & other)
constexprnoexcept

◆ operator/=() [1/4]

template<typename Base, typename Intermediate, unsigned Fraction, bool Rounding = true>
template<bool OtherRounding>
fixed & toy::math::fixed< Base, Intermediate, Fraction, Rounding >::operator/= ( const fixed< Base, Intermediate, Fraction, OtherRounding > & other)
constexprnoexcept

Divides by another toy::math::fixed value in place.

Accepts any toy::math::fixed with the same Base, Intermediate, and Fraction, regardless of Rounding. Uses raw storage; rounding policy of other does not affect the result. Behavior is undefined if other is zero.

Template Parameters
OtherRoundingRounding of the right-hand side type (may differ from this instance).
Parameters
otherDivisor (must not be zero).
Returns
Reference to *this.

◆ operator/=() [2/4]

template<typename Base, typename Intermediate, unsigned Fraction, bool Rounding = true>
template<bool OtherRounding>
fixed< Base, Intermediate, Fraction, Rounding > & toy::math::fixed< Base, Intermediate, Fraction, Rounding >::operator/= ( const fixed< Base, Intermediate, Fraction, OtherRounding > & other)
constexprnoexcept

◆ operator/=() [3/4]

template<typename Base, typename Intermediate, unsigned Fraction, bool Rounding = true>
template<integral T>
fixed & toy::math::fixed< Base, Intermediate, Fraction, Rounding >::operator/= ( const T & other)
constexprnoexcept

Divides by an integral value in place.

other is the divisor (fixed-point whole units). Behavior is undefined if other is zero.

Template Parameters
TIntegral type.
Parameters
otherDivisor (must not be zero).
Returns
Reference to *this.

◆ operator/=() [4/4]

template<typename Base, typename Intermediate, unsigned Fraction, bool Rounding = true>
template<integral T>
fixed< Base, Intermediate, Fraction, Rounding > & toy::math::fixed< Base, Intermediate, Fraction, Rounding >::operator/= ( const T & other)
constexprnoexcept

◆ operator=() [1/4]

template<typename Base, typename Intermediate, unsigned Fraction, bool Rounding = true>
template<bool OtherRounding>
fixed & toy::math::fixed< Base, Intermediate, Fraction, Rounding >::operator= ( const fixed< Base, Intermediate, Fraction, OtherRounding > & other)
constexprnoexcept

Assigns from another toy::math::fixed with the same Base, Intermediate, and Fraction.

Copies raw storage from other; Rounding of other does not affect the result. Accepts any fixed with the same representation (including different OtherRounding).

Template Parameters
OtherRoundingRounding policy of the source (may differ from this instance).
Parameters
otherSource value.
Returns
Reference to *this.

◆ operator=() [2/4]

template<typename Base, typename Intermediate, unsigned Fraction, bool Rounding = true>
template<bool OtherRounding>
fixed< Base, Intermediate, Fraction, Rounding > & toy::math::fixed< Base, Intermediate, Fraction, Rounding >::operator= ( const fixed< Base, Intermediate, Fraction, OtherRounding > & other)
constexprnoexcept

◆ operator=() [3/4]

template<typename Base, typename Intermediate, unsigned Fraction, bool Rounding = true>
template<integral T>
fixed & toy::math::fixed< Base, Intermediate, Fraction, Rounding >::operator= ( const T & other)
constexprnoexcept

Assigns from an integral value.

other is scaled by 2^Fraction and stored as raw value (same as constructing from other).

Template Parameters
TIntegral type; must satisfy integral.
Parameters
otherIntegral value in whole units.
Returns
Reference to *this.

◆ operator=() [4/4]

template<typename Base, typename Intermediate, unsigned Fraction, bool Rounding = true>
template<integral T>
fixed< Base, Intermediate, Fraction, Rounding > & toy::math::fixed< Base, Intermediate, Fraction, Rounding >::operator= ( const T & other)
constexprnoexcept

◆ rawValue()

template<typename Base, typename Intermediate, unsigned Fraction, bool Rounding>
Base toy::math::fixed< Base, Intermediate, Fraction, Rounding >::rawValue ( ) const
nodiscardconstexprnoexcept

Returns raw fixed-point storage value.

Returns
Raw scaled integer (storage value).

Member Data Documentation

◆ _value

template<typename Base, typename Intermediate, unsigned Fraction, bool Rounding = true>
Base toy::math::fixed< Base, Intermediate, Fraction, Rounding >::_value
private

Raw fixed-point storage value (scaled by 2^Fraction).


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