1
0
This repository has been archived on 2025-01-26. You can view files and clone it, but cannot push or open issues or pull requests.
CPP_Module_02/ex02/Fixed.cpp
2024-11-20 12:47:39 +01:00

153 lines
3.6 KiB
C++

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Fixed.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/12 21:14:55 by adjoly #+# #+# */
/* Updated: 2024/11/20 12:46:18 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "Fixed.hpp"
#include <cmath>
#include <iostream>
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);
}