creation d02 ex02
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cmath>
|
||||
|
||||
class Fixed {
|
||||
|
||||
@@ -18,7 +19,7 @@ public:
|
||||
|
||||
int getRawBits(void) const;
|
||||
void setRawBits(int const raw);
|
||||
void toFloat(void) const;
|
||||
float toFloat(void) const;
|
||||
int toInt(void) const;
|
||||
|
||||
private:
|
||||
@@ -29,4 +30,6 @@ private:
|
||||
|
||||
};
|
||||
|
||||
std::ostream & operator<<(std::ostream & out, Fixed const & fixed);
|
||||
|
||||
#endif
|
||||
|
||||
BIN
d02/ex01/fixed
BIN
d02/ex01/fixed
Binary file not shown.
@@ -3,39 +3,22 @@
|
||||
|
||||
int main( void ) {
|
||||
|
||||
// Fixed a(1105979538);
|
||||
Fixed a(1105979);
|
||||
std::cout << '\n';
|
||||
Fixed b(1000.5979f);
|
||||
std::cout << '\n';
|
||||
Fixed c(-1105.979f);
|
||||
std::cout << '\n';
|
||||
Fixed d(3726.7623683f);
|
||||
std::cout << '\n';
|
||||
Fixed e(4328239.384271721f);
|
||||
std::cout << '\n';
|
||||
Fixed f(1.11117f);
|
||||
std::cout << '\n';
|
||||
Fixed g(42.42f);
|
||||
std::cout << '\n';
|
||||
Fixed h(1234.4321f);
|
||||
Fixed a;
|
||||
Fixed const b( 10 );
|
||||
Fixed const c( 42.42f );
|
||||
Fixed const d( b );
|
||||
|
||||
// Fixed a;
|
||||
// Fixed const b( 10 );
|
||||
// Fixed const c( 42.42f );
|
||||
// Fixed const d( b );
|
||||
//
|
||||
// a = Fixed( 1234.4321f );
|
||||
//
|
||||
// std::cout << "a is " << a << std::endl;
|
||||
// std::cout << "b is " << b << std::endl;
|
||||
// std::cout << "c is " << c << std::endl;
|
||||
// std::cout << "d is " << d << std::endl;
|
||||
//
|
||||
// std::cout << "a is " << a.toInt() << " as integer" << std::endl;
|
||||
// std::cout << "b is " << b.toInt() << " as integer" << std::endl;
|
||||
// std::cout << "c is " << c.toInt() << " as integer" << std::endl;
|
||||
// std::cout << "d is " << d.toInt() << " as integer" << std::endl;
|
||||
a = Fixed( 1234.4321f );
|
||||
|
||||
std::cout << "a is " << a << std::endl;
|
||||
std::cout << "b is " << b << std::endl;
|
||||
std::cout << "c is " << c << std::endl;
|
||||
std::cout << "d is " << d << std::endl;
|
||||
|
||||
std::cout << "a is " << a.toInt() << " as integer" << std::endl;
|
||||
std::cout << "b is " << b.toInt() << " as integer" << std::endl;
|
||||
std::cout << "c is " << c.toInt() << " as integer" << std::endl;
|
||||
std::cout << "d is " << d.toInt() << " as integer" << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user