192 lines
4.0 KiB
C++
192 lines
4.0 KiB
C++
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* Fixed.cpp :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2025/07/02 12:25:46 by lfirmin #+# #+# */
|
|
/* Updated: 2025/07/16 14:41:49 by lfirmin ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "Fixed.hpp"
|
|
|
|
const int Fixed::_number_fractional_bits = 8;
|
|
|
|
Fixed::Fixed() : _fixed_p_n(0)
|
|
{
|
|
// std::cout << "Default constructor called\n";
|
|
}
|
|
|
|
Fixed::Fixed(const int input)
|
|
{
|
|
// std::cout << "Fixed Int Constructor called" << std::endl;
|
|
this->_fixed_p_n = input << this->_number_fractional_bits;
|
|
}
|
|
|
|
Fixed::Fixed(const float input)
|
|
{
|
|
// std::cout << "Fixed Float Constructor called" << std::endl;
|
|
this->_fixed_p_n = roundf(input * (1 << this->_number_fractional_bits));
|
|
}
|
|
|
|
Fixed::Fixed(const Fixed ©)
|
|
{
|
|
// std::cout << "Copy constructor called\n";
|
|
*this = copy;
|
|
}
|
|
|
|
Fixed &Fixed::operator=(const Fixed ©)
|
|
{
|
|
// std::cout << "Copy assignment constructor called\n";
|
|
setRawBits(copy.getRawBits());
|
|
return (*this);
|
|
}
|
|
|
|
Fixed::~Fixed()
|
|
{
|
|
// std::cout << "Deconstructor called\n";
|
|
}
|
|
|
|
int Fixed::getRawBits(void) const
|
|
{
|
|
// std::cout << "getRawBits member function called\n";
|
|
return (_fixed_p_n);
|
|
}
|
|
|
|
void Fixed::setRawBits(int const raw)
|
|
{
|
|
_fixed_p_n = raw;
|
|
}
|
|
|
|
int Fixed::toInt(void)const
|
|
{
|
|
return (this->_fixed_p_n>> this->_number_fractional_bits);
|
|
}
|
|
|
|
float Fixed::toFloat(void)const
|
|
{
|
|
return ((float)this->_fixed_p_n/ (float)(1 << this->_number_fractional_bits));
|
|
}
|
|
|
|
std::ostream &operator<<(std::ostream &o, Fixed const &fixed)
|
|
{
|
|
o << fixed.toFloat();
|
|
return (o);
|
|
}
|
|
|
|
// Overloaded comparation Operators
|
|
bool Fixed::operator>(Fixed fixed) const
|
|
{
|
|
return (this->toFloat() > fixed.toFloat());
|
|
}
|
|
|
|
bool Fixed::operator<(Fixed fixed) const
|
|
{
|
|
return (this->toFloat() < fixed.toFloat());
|
|
}
|
|
|
|
bool Fixed::operator>=(Fixed fixed) const
|
|
{
|
|
return (this->toFloat() >= fixed.toFloat());
|
|
}
|
|
|
|
bool Fixed::operator<=(Fixed fixed) const
|
|
{
|
|
return (this->toFloat() <= fixed.toFloat());
|
|
}
|
|
|
|
bool Fixed::operator==(Fixed fixed) const
|
|
{
|
|
return (this->toFloat() == fixed.toFloat());
|
|
}
|
|
|
|
bool Fixed::operator!=(Fixed fixed) const
|
|
{
|
|
return (this->toFloat() != fixed.toFloat());
|
|
}
|
|
/////
|
|
|
|
// Overloaded Arithmetic Operators
|
|
float Fixed::operator+(Fixed fixed) const
|
|
{
|
|
return (this->toFloat() + fixed.toFloat());
|
|
}
|
|
|
|
float Fixed::operator-(Fixed fixed) const
|
|
{
|
|
return (this->toFloat() - fixed.toFloat());
|
|
}
|
|
|
|
float Fixed::operator*(Fixed fixed) const
|
|
{
|
|
return (this->toFloat() * fixed.toFloat());
|
|
}
|
|
|
|
float Fixed::operator/(Fixed fixed) const
|
|
{
|
|
return (this->toFloat() / fixed.toFloat());
|
|
}
|
|
/////
|
|
|
|
// Overloaded pre-increment Operators
|
|
Fixed Fixed::operator++()
|
|
{
|
|
this->_fixed_p_n++;
|
|
return (*this);
|
|
}
|
|
|
|
Fixed Fixed::operator--()
|
|
{
|
|
this->_fixed_p_n--;
|
|
return (*this);
|
|
}
|
|
|
|
Fixed Fixed::operator++(int)
|
|
{
|
|
Fixed tmp = *this;
|
|
++this->_fixed_p_n;
|
|
return (tmp);
|
|
}
|
|
|
|
Fixed Fixed::operator--(int)
|
|
{
|
|
Fixed tmp = *this;
|
|
--this->_fixed_p_n;
|
|
return (tmp);
|
|
}
|
|
//////
|
|
|
|
Fixed &Fixed::min(Fixed &first, Fixed &second)
|
|
{
|
|
if (first.toFloat() <= second.toFloat())
|
|
return (first);
|
|
else
|
|
return (second);
|
|
}
|
|
|
|
const Fixed &Fixed::min(const Fixed &first, const Fixed &second)
|
|
{
|
|
if (first.toFloat() <= second.toFloat())
|
|
return (first);
|
|
else
|
|
return (second);
|
|
}
|
|
|
|
Fixed &Fixed::max(Fixed &first, Fixed &second)
|
|
{
|
|
if (first.toFloat() >= second.toFloat())
|
|
return (first);
|
|
else
|
|
return (second);
|
|
}
|
|
|
|
const Fixed &Fixed::max(const Fixed &first, const Fixed &second)
|
|
{
|
|
if (first.toFloat() >= second.toFloat())
|
|
return (first);
|
|
else
|
|
return (second);
|
|
}
|