ex02 done

This commit is contained in:
2025-09-12 13:08:26 +02:00
parent 80b0e4b146
commit 25147d5d2d
3 changed files with 145 additions and 28 deletions

View File

@@ -6,43 +6,43 @@
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */ /* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/10 00:54:50 by tigos #+# #+# */ /* 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" #include "Fixed.hpp"
// Default constructor // 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 // Constructor with constant integer
Fixed::Fixed(const int nb) 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); this->value = nb * (1 << this->fract);
} }
// Constructor with constant floating-point number // Constructor with constant floating-point number
Fixed::Fixed(const float nb) 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)); this->value = roundf(nb * (1 << this->fract));
} }
// Copy constructor // Copy constructor
Fixed::Fixed(const Fixed &other) Fixed::Fixed(const Fixed &other)
{ {
std:: cout << "Copy constructor called" << std::endl; //std:: cout << "Copy constructor called" << std::endl;
this->value = other.value; this->value = other.value;
} }
// Copy assignment operator overload // Copy assignment operator overload
Fixed& Fixed::operator=(Fixed const &other) 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) if (this != &other)
this->value = other.getRawBits(); this->value = other.getRawBits();
return (*this); return (*this);
@@ -51,13 +51,13 @@ Fixed& Fixed::operator=(Fixed const &other)
// Destructor // Destructor
Fixed::~Fixed() Fixed::~Fixed()
{ {
std::cout << "Destructor called" << std::endl; //std::cout << "Destructor called" << std::endl;
} }
// getRawBits() // getRawBits()
int Fixed::getRawBits( void ) const int Fixed::getRawBits( void ) const
{ {
std::cout << "getRawBits member function called" << std::endl; //std::cout << "getRawBits member function called" << std::endl;
return (this->value); return (this->value);
} }
@@ -80,34 +80,137 @@ int Fixed::toInt( void ) const
} }
// · Comparison operators // · 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 // << operator overload

View File

@@ -55,19 +55,33 @@ class Fixed
int toInt( void ) const; int toInt( void ) const;
// · Comparison operators // · Comparison operators
bool Fixed::operator<(const Fixed &other); bool operator<(const Fixed &other) const;
bool Fixed::operator<=(const Fixed &other); bool operator<=(const Fixed &other) const;
bool Fixed::operator>(const Fixed &other); bool operator>(const Fixed &other) const;
bool Fixed::operator>=(const Fixed &other); bool operator>=(const Fixed &other) const;
bool Fixed::operator==(const Fixed &other); bool operator==(const Fixed &other) const;
bool Fixed::operator!=(const Fixed &other); bool operator!=(const Fixed &other) const;
// · Arithmetic operators // · 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 // · Increments / decrements
// · Max and min 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);
}; };

View File

@@ -6,7 +6,7 @@
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */ /* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/10 21:57:38 by aortigos #+# #+# */ /* 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */