/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* Fixed.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/12 21:14:55 by adjoly #+# #+# */ /* Updated: 2024/11/20 12:46:18 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include "Fixed.hpp" #include #include Fixed::Fixed(void) : _number(0) { std::cout << "Default constructor called" << std::endl; } Fixed::Fixed(const int nbr) { std::cout << "Int constructor called" << std::endl; _number = nbr << _fractBit; } Fixed::Fixed(const float nbr) { std::cout << "Float constructor called" << std::endl; _number = roundf(nbr * (1 << _fractBit)); } Fixed::Fixed(const Fixed& cpy) { std::cout << "Copy constructor called" << std::endl; *this = cpy; } Fixed::~Fixed(void) { std::cout << "Destructor called" << std::endl; } int Fixed::toInt(void) const { return (_number >> _fractBit); } float Fixed::toFloat(void) const { return ((float)_number / (1 << _fractBit)); } int Fixed::getRawBits(void) const { return (_number); } void Fixed::setRawBits(int const raw) { _number = raw; } std::ostream &operator<<(std::ostream &file, Fixed const &fixed) { file << fixed.toFloat(); return (file); } Fixed &Fixed::operator=(const Fixed& cpy) { std::cout << "Copy assignment operator called" << std::endl; if (this != &cpy) _number = cpy.getRawBits(); return (*this); } float Fixed::operator+(const Fixed& cpy) { return (this->toFloat() + cpy.toFloat()); } float Fixed::operator-(const Fixed& cpy) { return (this->toFloat() - cpy.toFloat()); } float Fixed::operator*(const Fixed& cpy) { return (this->toFloat() * cpy.toFloat()); } float Fixed::operator/(const Fixed& cpy) { return (this->toFloat() / cpy.toFloat()); } Fixed& Fixed::operator++(void) { _number++; return (*this); } Fixed Fixed::operator++(int) { Fixed tmp = *this; _number++; return (tmp); } Fixed& Fixed::operator--(void) { _number--; return (*this); } Fixed Fixed::operator--(int) { Fixed tmp = *this; _number--; return (tmp); } bool Fixed::operator<(const Fixed& comp) const { return (getRawBits() < comp.getRawBits()); } bool Fixed::operator>(const Fixed& comp) const { return (getRawBits() > comp.getRawBits()); } bool Fixed::operator<=(const Fixed& comp) const { return (getRawBits() <= comp.getRawBits()); } bool Fixed::operator>=(const Fixed& comp) const { return (getRawBits() >= comp.getRawBits()); } bool Fixed::operator==(const Fixed& comp) const { return (getRawBits() == comp.getRawBits()); } bool Fixed::operator!=(const Fixed& comp) const { return (getRawBits() != comp.getRawBits()); } Fixed& max(Fixed& fst, Fixed &scnd) { if (fst < scnd) return (scnd); return (fst); } Fixed& min(Fixed& fst, Fixed &scnd) { if (fst > scnd) return (scnd); return (fst); } const Fixed& Fixed::max(const Fixed& fst, const Fixed &scnd) { if (fst < scnd) return (scnd); return (fst); } const Fixed& min(const Fixed& fst, const Fixed &scnd) { if (fst > scnd) return (scnd); return (fst); }