You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.4 KiB

3 years ago
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Fixed.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/20 10:07:44 by narnaud #+# #+# */
/* Updated: 2022/06/20 14:15:11 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 << "Fixed constructor from interger called " << std::endl;
_raw_bits = init_int * (1 << _fract_bits);
}
Fixed::Fixed (const float init_float) {
std::cout << "Fixed constructor from float called " << std::endl;
_raw_bits = std::roundf(init_float * (1 << _fract_bits));
}
Fixed::Fixed (Fixed const & src) {
std::cout << "Fixed copy constructor called" << std::endl;
_raw_bits = src.getRawBits();
}
Fixed & Fixed::operator= (Fixed const & src) {
std::cout << "Fixed 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
{
std::cout << "getRawBits member function called" << std::endl;
return (_raw_bits);
}
void Fixed::setRawBits(int const raw)
{
std::cout << "setRawBits member function called" << std::endl;
_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);
}