42_CPP02/ex01/src/Fixed.cpp
Camille Chauvet 9fafccf65f init to 0
2023-08-03 14:18:05 +02:00

69 lines
1.3 KiB
C++

#include "Fixed.hpp"
#include <iostream>
#include <cmath>
#include <ostream>
Fixed::~Fixed()
{
std::cout << "Destructor called" << std::endl;
}
Fixed::Fixed()
{
this->_value = 0;
std::cout << "Default constructor called" << std::endl;
}
Fixed::Fixed(const Fixed& src)
{
std::cout << "Copy constructor called" << std::endl;
this->_value = src.getRawBits();
}
Fixed::Fixed(const int& src)
{
std::cout << "Copy constructor called" << std::endl;
this->_value = src << this->_nb_bits;
}
Fixed::Fixed(const float& src)
{
std::cout << "Copy constructor called" << std::endl;
this->_value = (int) roundf(src * (1 << this->_nb_bits));
}
Fixed& Fixed::operator=(const Fixed& src)
{
std::cout << "Copy assignment operator called" << std::endl;
this->_value = src.getRawBits();
return (*this);
}
int Fixed::getRawBits() const
{
std::cout << "getRawBits member function called" << std::endl;
return (this->_value);
}
void Fixed::setRawBits(const int raw)
{
std::cout << "setRawBits member function called" << std::endl;
this->_value = raw;
}
float Fixed::toFloat(void) const
{
return ((float) this->_value / (1 << this->_nb_bits));
}
int Fixed::toInt() const
{
return ((int) (roundf(this->toFloat())));
}
std::ostream& operator<<(std::ostream &out, const Fixed& fixed)
{
out << (fixed.toFloat());
return (out);
}