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/ex01/Fixed.cpp

67 lines
2.0 KiB
C++
Raw Permalink Normal View History

2024-11-19 16:31:58 +01:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Fixed.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/12 21:14:55 by adjoly #+# #+# */
/* Updated: 2024/11/19 16:29:28 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;
}
Fixed &Fixed::operator=(const Fixed& cpy) {
std::cout << "Copy assignment operator called" << std::endl;
if (this != &cpy)
_number = cpy.getRawBits();
return (*this);
}
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);
}