Fixed-Point Arithmetic Class

Hard C++ Precision Asked at Analog Devices
Solve this problem

Implement a Q15.16 fixed-point type with correct multiplication and division for a control loop on an FPU-less core.

Constraints

  • Backed by int32_t, 16 fractional bits.
  • Multiplication must use a 64-bit intermediate to avoid overflow.
  • Division must round, not truncate toward zero.
  • Provide operator+ , operator- , operator* , operator/.
  • No floating point in the arithmetic paths.
  • Division by zero must saturate rather than trap.
Starter codeC++
1#include <cstdint>2 3class Fixed {4public:5    static constexpr int kFracBits = 16;6    // TODO7};