Fixed.hpp
1 #pragma once 2 3 #include <iostream> 4 5 class Fixed { 6 private: 7 int _value; 8 static const int _fractionalBits = 8; 9 10 public: 11 Fixed(); 12 Fixed(const Fixed &other); 13 Fixed(const int &other); 14 Fixed(const float &other); 15 16 // Define FIXED_DEBUG to enable constructor/destructor debug prints. 17 // Example: add -DFIXED_DEBUG to CXXFLAGS in Makefile to enable. 18 19 friend std::ostream &operator<<(std::ostream &os, const Fixed &obj); 20 float toFloat() const; 21 22 // Accessors to extract the raw fixed-point integer and fractional bits 23 int getRawBits() const; 24 static int getFractionalBits(); 25 26 // Comparison operators 27 bool operator>(const Fixed &other) const; 28 bool operator<(const Fixed &other) const; 29 bool operator>=(const Fixed &other) const; 30 bool operator<=(const Fixed &other) const; 31 bool operator==(const Fixed &other) const; 32 bool operator!=(const Fixed &other) const; 33 34 // Arithmetic operators 35 Fixed operator+(const Fixed &other) const; 36 Fixed operator-(const Fixed &other) const; 37 Fixed operator*(const Fixed &other) const; 38 Fixed operator/(const Fixed &other) const; 39 40 // Increment and decrement operators 41 Fixed &operator++(); // Prefix increment 42 Fixed operator++(int); // Postfix increment 43 Fixed &operator--(); // Prefix decrement 44 Fixed operator--(int); // Postfix decrement 45 46 // Min and max functions 47 static Fixed &min(Fixed &a, Fixed &b); 48 static const Fixed &min(const Fixed &a, const Fixed &b); 49 static Fixed &max(Fixed &a, Fixed &b); 50 static const Fixed &max(const Fixed &a, const Fixed &b); 51 52 Fixed &operator=(const Fixed &other); 53 ~Fixed(); 54 };