/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* Fixed.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: aortigos value = nb * (1 << this->fract); } // Constructor with constant floating-point number Fixed::Fixed(const float nb) { //std::cout << "Float constructor called" << std::endl; this->value = roundf(nb * (1 << this->fract)); } // Copy constructor Fixed::Fixed(const Fixed &other) { //std:: cout << "Copy constructor called" << std::endl; this->value = other.value; } // Copy assignment operator overload Fixed& Fixed::operator=(Fixed const &other) { //std::cout << "Copy assignment operator called" << std::endl; if (this != &other) this->value = other.getRawBits(); return (*this); } // Destructor Fixed::~Fixed() { //std::cout << "Destructor called" << std::endl; } // getRawBits() int Fixed::getRawBits( void ) const { //std::cout << "getRawBits member function called" << std::endl; return (this->value); } // setRawBits() void Fixed::setRawBits( int const raw ) { this->value = raw; } // toFloat float Fixed::toFloat( void ) const { return (this->value / (float)(1 << this->fract)); } // toInt int Fixed::toInt( void ) const { return (this->value >> this->fract); } // · Comparison operators bool Fixed::operator<(const Fixed &other) const { return (this->value < other.getRawBits()); } bool Fixed::operator<=(const Fixed &other) const { return (this->value <= other.getRawBits()); } bool Fixed::operator>(const Fixed &other) const { return (this->value > other.getRawBits()); } bool Fixed::operator>=(const Fixed &other) const { return (this->value >= other.getRawBits()); } bool Fixed::operator==(const Fixed &other) const { return (this->value == other.getRawBits()); } bool Fixed::operator!=(const Fixed &other) const { return (this->value != other.getRawBits()); } // · Arithmetic operators Fixed Fixed::operator+(Fixed const &other) const { Fixed res; res.setRawBits(this->value + other.getRawBits()); return (res); } Fixed Fixed::operator-(Fixed const &other) const { Fixed res; res.setRawBits(this->value - other.getRawBits()); return (res); } Fixed Fixed::operator*(Fixed const &other) const { Fixed res; long result; result = (long)this->value * (long)other.getRawBits(); result = result >> this->fract; res.setRawBits((int)result); return (res); } Fixed Fixed::operator/(Fixed const &other) const { if (other.getRawBits() == 0) { return Fixed(); } long result = (long)this->value << this->fract; result = result / other.getRawBits(); Fixed res; res.setRawBits((int)result); return (res); } // · Increments / decrements Fixed& Fixed::operator++() { this->value += 1; return (*this); } Fixed Fixed::operator++(int) { Fixed temp(*this); this->value += 1; return (temp); } Fixed& Fixed::operator--() { this->value -= 1; return (*this); } Fixed Fixed::operator--(int) { Fixed temp(*this); this->value -= 1; return (temp); } // · Max and min Fixed& Fixed::min(Fixed &a, Fixed &b) { if (a.getRawBits() < b.getRawBits()) return (a); return (b); } const Fixed& Fixed::min(const Fixed &a, const Fixed &b) { if (a.getRawBits() < b.getRawBits()) return (a); return (b); } Fixed& Fixed::max(Fixed &a, Fixed &b) { if (a.getRawBits() > b.getRawBits()) return (a); return (b); } const Fixed& Fixed::max(const Fixed &a, const Fixed &b) { if (a.getRawBits() > b.getRawBits()) return (a); return (b); } // << operator overload std::ostream& operator<<(std::ostream& os, Fixed const &other) { os << other.toFloat(); return (os); }