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.

75 lines
2.2 KiB

3 years ago
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Fixed.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/20 10:07:44 by narnaud #+# #+# */
2 years ago
/* Updated: 2022/07/18 14:00:06 by narnaud ### ########.fr */
3 years ago
/* */
/* ************************************************************************** */
#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) {
2 years ago
std::cout << "Integer constructor called " << std::endl;
3 years ago
_raw_bits = init_int * (1 << _fract_bits);
}
Fixed::Fixed (const float init_float) {
2 years ago
std::cout << "Float constructor called " << std::endl;
3 years ago
_raw_bits = std::roundf(init_float * (1 << _fract_bits));
}
Fixed::Fixed (Fixed const & src) {
2 years ago
std::cout << "Copy constructor called" << std::endl;
*this = src;
3 years ago
}
Fixed & Fixed::operator= (Fixed const & src) {
2 years ago
std::cout << "Copy assignment operator called" << std::endl;
3 years ago
_raw_bits = src.getRawBits();
return (*this);
}
Fixed::~Fixed (void) {
std::cout << "Fixed default destructor called" << std::endl;
}
2 years ago
int Fixed::getRawBits(void) const {
3 years ago
return (_raw_bits);
}
2 years ago
void Fixed::setRawBits(int const raw) {
3 years ago
_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);
}