ex02 done
This commit is contained in:
@@ -6,43 +6,43 @@
|
||||
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/09/10 00:54:50 by tigos #+# #+# */
|
||||
/* Updated: 2025/09/10 22:31:58 by aortigos ### ########.fr */
|
||||
/* Updated: 2025/09/12 13:08:05 by aortigos ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Fixed.hpp"
|
||||
|
||||
// Default constructor
|
||||
Fixed::Fixed()
|
||||
Fixed::Fixed() : value(0)
|
||||
{
|
||||
std::cout << "Default constructor called" << std::endl;
|
||||
//std::cout << "Default constructor called" << std::endl;
|
||||
}
|
||||
|
||||
// Constructor with constant integer
|
||||
Fixed::Fixed(const int nb)
|
||||
{
|
||||
std::cout << "Int constructor called" << std::endl;
|
||||
//std::cout << "Int constructor called" << std::endl;
|
||||
this->value = nb * (1 << this->fract);
|
||||
}
|
||||
|
||||
// Constructor with constant floating-point number
|
||||
Fixed::Fixed(const float nb)
|
||||
{
|
||||
std::cout << "Float constructor called" << std::endl;
|
||||
//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;
|
||||
//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;
|
||||
//std::cout << "Copy assignment operator called" << std::endl;
|
||||
if (this != &other)
|
||||
this->value = other.getRawBits();
|
||||
return (*this);
|
||||
@@ -51,13 +51,13 @@ Fixed& Fixed::operator=(Fixed const &other)
|
||||
// Destructor
|
||||
Fixed::~Fixed()
|
||||
{
|
||||
std::cout << "Destructor called" << std::endl;
|
||||
//std::cout << "Destructor called" << std::endl;
|
||||
}
|
||||
|
||||
// getRawBits()
|
||||
int Fixed::getRawBits( void ) const
|
||||
{
|
||||
std::cout << "getRawBits member function called" << std::endl;
|
||||
//std::cout << "getRawBits member function called" << std::endl;
|
||||
return (this->value);
|
||||
}
|
||||
|
||||
@@ -80,34 +80,137 @@ int Fixed::toInt( void ) const
|
||||
}
|
||||
|
||||
// · Comparison operators
|
||||
bool Fixed::operator<(const Fixed &other)
|
||||
bool Fixed::operator<(const Fixed &other) const
|
||||
{
|
||||
return (this->value < other->value);
|
||||
return (this->value < other.getRawBits());
|
||||
}
|
||||
|
||||
bool Fixed::operator<=(const Fixed &other)
|
||||
bool Fixed::operator<=(const Fixed &other) const
|
||||
{
|
||||
return (this->value <= other->value);
|
||||
return (this->value <= other.getRawBits());
|
||||
}
|
||||
|
||||
bool Fixed::operator>(const Fixed &other)
|
||||
bool Fixed::operator>(const Fixed &other) const
|
||||
{
|
||||
return (this->value > other->value);
|
||||
return (this->value > other.getRawBits());
|
||||
}
|
||||
|
||||
bool Fixed::operator>=(const Fixed &other)
|
||||
bool Fixed::operator>=(const Fixed &other) const
|
||||
{
|
||||
return (this->value >= other->value);
|
||||
return (this->value >= other.getRawBits());
|
||||
}
|
||||
|
||||
bool Fixed::operator==(const Fixed &other)
|
||||
bool Fixed::operator==(const Fixed &other) const
|
||||
{
|
||||
return (this->value == other->value);
|
||||
return (this->value == other.getRawBits());
|
||||
}
|
||||
|
||||
bool Fixed::operator!=(const Fixed &other)
|
||||
bool Fixed::operator!=(const Fixed &other) const
|
||||
{
|
||||
return (this->value != other->value);
|
||||
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
|
||||
|
||||
@@ -55,19 +55,33 @@ class Fixed
|
||||
int toInt( void ) const;
|
||||
|
||||
// · Comparison operators
|
||||
bool Fixed::operator<(const Fixed &other);
|
||||
bool Fixed::operator<=(const Fixed &other);
|
||||
bool Fixed::operator>(const Fixed &other);
|
||||
bool Fixed::operator>=(const Fixed &other);
|
||||
bool Fixed::operator==(const Fixed &other);
|
||||
bool Fixed::operator!=(const Fixed &other);
|
||||
bool operator<(const Fixed &other) const;
|
||||
bool operator<=(const Fixed &other) const;
|
||||
bool operator>(const Fixed &other) const;
|
||||
bool operator>=(const Fixed &other) const;
|
||||
bool operator==(const Fixed &other) const;
|
||||
bool operator!=(const Fixed &other) const;
|
||||
|
||||
// · Arithmetic operators
|
||||
Fixed operator+(Fixed const &other) const;
|
||||
Fixed operator-(Fixed const &other) const;
|
||||
Fixed operator*(Fixed const &other) const;
|
||||
Fixed operator/(Fixed const &other) const;
|
||||
|
||||
// · Increments / decrements
|
||||
|
||||
Fixed& operator++();
|
||||
Fixed operator++(int);
|
||||
|
||||
Fixed& operator--();
|
||||
Fixed operator--(int);
|
||||
|
||||
// · Max and min
|
||||
static Fixed& min(Fixed &a, Fixed &b);
|
||||
static const Fixed& min(const Fixed &a, const Fixed &b);
|
||||
|
||||
static Fixed& max(Fixed &a, Fixed &b);
|
||||
static const Fixed& max(const Fixed &a, const Fixed &b);
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/09/10 21:57:38 by aortigos #+# #+# */
|
||||
/* Updated: 2025/09/10 22:26:59 by aortigos ### ########.fr */
|
||||
/* Updated: 2025/09/12 12:57:21 by aortigos ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
||||
Reference in New Issue
Block a user