fixed_math

Conversion between Fixed and Arithmetic Types in FixedMath

The FixedMath library provides seamless conversion mechanisms between fixed-point numbers (fixed_t) and standard arithmetic types (integers and floating-point numbers). These conversions ensure that fixed-point arithmetic integrates well with the broader C++ type system, allowing for efficient and type-safe operations across different numeric types.

Conversion from fixed_t to Arithmetic Types

Conversion from Arithmetic Types to fixed_t

Examples of Use

Conversion from fixed_t to arithmetic types:

fixed_t fixedValue = 2.5_fix; // Initialize with some fixed-point value

// Convert to an integer
int intValue = fixed_to_arithmetic<int>(fixedValue);

// Convert to a floating-point
double doubleValue = fixedValue; // Implicitly uses fixed_t::operator double() const

Conversion from arithmetic types to fixed_t:

int intInput = 5;
// Convert from integer to fixed_t
fixed_t fixedFromInt = arithmetic_to_fixed(intInput);

double doubleInput = 2.5;
// Convert from floating-point to fixed_t
fixed_t fixedFromDouble = arithmetic_to_fixed(doubleInput);

fixedFromDouble = static_cast<double>(arithmetic_to_fixed(doubleInput));

These conversion functions and operators ensure that working with fixed-point numbers in a mixed arithmetic context is both natural and efficient, maintaining type safety and precision across conversions.