creation d02 ex02

This commit is contained in:
hugogogo
2022-02-14 21:06:36 +01:00
parent 9036ded700
commit b08663e74e
9 changed files with 310 additions and 81 deletions

View File

@@ -53,18 +53,18 @@ int const Fixed::_max = -1U >> (_frac +1);
*/
Fixed::Fixed() : _value(0) {
// std::cout << "Default constructor called" << '\n';
std::cout << "Default constructor called" << '\n';
return;
}
Fixed::Fixed(Fixed const & src) {
// std::cout << "Copy constructor called" << '\n';
std::cout << "Copy constructor called" << '\n';
*this = src;
return;
}
Fixed::~Fixed( void ) {
// std::cout << "Destructor called" << '\n';
std::cout << "Destructor called" << '\n';
return;
}
@@ -73,42 +73,21 @@ Fixed::~Fixed( void ) {
*/
Fixed::Fixed(int integer) {
// std::cout << "Int constructor called" << '\n';
std::cout << "Int constructor called" << '\n';
if (integer < ~this->_max || integer > this->_max)
{
std::cout << "error: integer out of range" << '\n';
return;
}
// std::cout << "integer : " << printBitsInt(integer) << " (" << integer << ")\n";
this->_value = integer << this->_frac;
// std::cout << "_value : " << printBitsInt(this->_value) << " (" << this->_value << ")\n";
else
this->_value = integer << this->_frac;
}
Fixed::Fixed(float const floater) {
// std::cout << "Float constructor called" << '\n';
std::cout << "Float constructor called" << '\n';
if (floater < ~this->_max || floater > this->_max)
{
std::cout << "error: float out of range" << '\n';
return;
}
// std::cout << "floater : " << printBitsFloat(floater) << " (" << floater << ")\n";
this->_value = floater * (1 << this->_frac);
// std::cout << "_value : " << printBitsInt(this->_value) << " (" << this->_value << ")\n";
/*
this->_value = *((int *)&floater); // cast float to int
int sign = this->_value & (1 << 31); // extract sign
int exponent = ((unsigned int)(this->_value << 1) >> 24) - 127; // extract exponent
int integer = (this->_value << 8) | (1 << 31); // add left 1
integer = (unsigned int)integer >> (31 - this->_frac - exponent);// align to right
if (sign != 0)
integer = (~integer + 1); // reverse negatif
integer = (integer << (30 - this->_frac - exponent)) | sign; // add sign
integer >>= (30 - this->_frac - exponent); // align right
std::cout << "integer : " << printBitsInt(integer) << " (" << integer << ")\n";
*/
else
this->_value = floater * (1 << this->_frac);
}
/*
@@ -116,19 +95,17 @@ Fixed::Fixed(float const floater) {
*/
Fixed & Fixed::operator=( Fixed const & rhs ) {
// std::cout << "Copy assignment operator called" << '\n';
std::cout << "Copy assignment operator called" << '\n';
if ( this != &rhs )
this->_value = rhs.getRawBits();
return *this;
}
/*
* other functions
* functions that returns _value
*/
int Fixed::getRawBits( void ) const {
// std::cout << "getRawBits member function called" << '\n';
return this->_value;
}
@@ -136,9 +113,20 @@ void Fixed::setRawBits( int const raw ) {
this->_value = raw;
}
void Fixed::toFloat( void ) const {}
int Fixed::toInt( void ) const {
return 0;
return (this->_value >> this->_frac);
}
float Fixed::toFloat( void ) const {
return ((float)this->_value / (float)(1 << this->_frac));
}
/*
* overload "<<" -> output fixed point in float representation
* took here : https://github.com/pgomez-a/42_CPP_Piscine/blob/master/cpp02/ex01/Fixed.cpp
*/
std::ostream & operator<<(std::ostream & out, Fixed const & fixed)
{
out << fixed.toFloat();
return (out);
}