|
|
|
/* ************************************************************************** */
|
|
|
|
/* */
|
|
|
|
/* ::: :::::::: */
|
|
|
|
/* Fixed.cpp :+: :+: :+: */
|
|
|
|
/* +:+ +:+ +:+ */
|
|
|
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
|
|
|
|
/* +#+#+#+#+#+ +#+ */
|
|
|
|
/* Created: 2022/06/20 10:07:44 by narnaud #+# #+# */
|
|
|
|
/* Updated: 2022/07/18 14:00:06 by narnaud ### ########.fr */
|
|
|
|
/* */
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
|
|
|
#include "Fixed.hpp"
|
|
|
|
|
|
|
|
const int Fixed::_fract_bits = 8;
|
|
|
|
|
|
|
|
Fixed::Fixed (void) {
|
|
|
|
|
|
|
|
std::cout << "Fixed default constructor called " << std::endl;
|
|
|
|
_raw_bits = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Fixed::Fixed (const int init_int) {
|
|
|
|
|
|
|
|
std::cout << "Integer constructor called " << std::endl;
|
|
|
|
_raw_bits = init_int * (1 << _fract_bits);
|
|
|
|
}
|
|
|
|
|
|
|
|
Fixed::Fixed (const float init_float) {
|
|
|
|
|
|
|
|
std::cout << "Float constructor called " << std::endl;
|
|
|
|
_raw_bits = std::roundf(init_float * (1 << _fract_bits));
|
|
|
|
}
|
|
|
|
|
|
|
|
Fixed::Fixed (Fixed const & src) {
|
|
|
|
|
|
|
|
std::cout << "Copy constructor called" << std::endl;
|
|
|
|
*this = src;
|
|
|
|
}
|
|
|
|
|
|
|
|
Fixed & Fixed::operator= (Fixed const & src) {
|
|
|
|
std::cout << "Copy assignment operator called" << std::endl;
|
|
|
|
_raw_bits = src.getRawBits();
|
|
|
|
return (*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
Fixed::~Fixed (void) {
|
|
|
|
std::cout << "Fixed default destructor called" << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Fixed::getRawBits(void) const {
|
|
|
|
return (_raw_bits);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Fixed::setRawBits(int const raw) {
|
|
|
|
_raw_bits = raw;
|
|
|
|
}
|
|
|
|
|
|
|
|
float Fixed::toFloat(void) const {
|
|
|
|
|
|
|
|
//std::cout << "toFloat member function called" << std::endl;
|
|
|
|
return ((float)((float)_raw_bits / (float)(1 << _fract_bits)));
|
|
|
|
}
|
|
|
|
|
|
|
|
int Fixed::toInt(void) const {
|
|
|
|
|
|
|
|
//std::cout << "toInt member function called" << std::endl;
|
|
|
|
return ((int)((float)_raw_bits / (float)(1 << _fract_bits)));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream & operator << (std::ostream &out, const Fixed &f){
|
|
|
|
out << f.toFloat();
|
|
|
|
return (out);
|
|
|
|
}
|