diff --git a/d02/ex01/Fixed.cpp b/d02/ex01/Fixed.cpp index f94c568..7c54d70 100644 --- a/d02/ex01/Fixed.cpp +++ b/d02/ex01/Fixed.cpp @@ -80,9 +80,9 @@ Fixed::Fixed(int integer) { std::cout << "error: integer out of range" << '\n'; return; } - std::cout << "integer : " << printBitsInt(integer) << " (" << integer << ")\n"; +// std::cout << "integer : " << printBitsInt(integer) << " (" << integer << ")\n"; this->_value = integer << this->_frac; - std::cout << "_value : " << printBitsInt(this->_value) << " (" << this->_value << ")\n"; +// std::cout << "_value : " << printBitsInt(this->_value) << " (" << this->_value << ")\n"; } Fixed::Fixed(float const floater) { @@ -93,52 +93,22 @@ Fixed::Fixed(float const floater) { std::cout << "error: float out of range" << '\n'; return; } - std::cout << "floater : " << printBitsFloat(floater) << " (" << floater << ")\n"; +// std::cout << "floater : " << printBitsFloat(floater) << " (" << floater << ")\n"; this->_value = floater * (1 << this->_frac); - std::cout << "_value : " << printBitsInt(this->_value) << " (" << this->_value << ")\n"; +// std::cout << "_value : " << printBitsInt(this->_value) << " (" << this->_value << ")\n"; - this->_value = *((int *)&floater); // cast floater into int -std::cout << "_value : " << printBitsInt(this->_value) << " (" << this->_value << ")\n"; - - int sign = this->_value & (1 << 31); // set most left bits to sign -std::cout << "sign : " << printBitsInt(sign) << " (" << sign << ")\n"; - - int exponent = ((unsigned int)(this->_value << 1) >> 24) - 127; // extract exponent value -std::cout << "exponent: " << printBitsInt(exponent) << " (" << exponent << ")\n"; - - int decimal = (this->_value << (9)); -std::cout << "decimal : " << printBitsInt(decimal) << " (" << decimal << ")\n"; - decimal = (this->_value << (9 + exponent)); -std::cout << "decimal : " << printBitsInt(decimal) << " (" << decimal << ")\n"; - - int integer = (this->_value << 8) | (1 << 31); // add left 1 to integer part -std::cout << "left 1 : " << printBitsInt(integer) << " (" << integer << ")\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; // reverse for negatif -std::cout << "reverse : " << printBitsInt(integer) << " (" << integer << ")\n"; - integer = ((unsigned int)integer >> 1) | sign; // add sign to integer part -std::cout << "left +/-: " << printBitsInt(integer) << " (" << integer << ")\n"; - integer >>= (30 - exponent); // extract integer part -std::cout << "integer : " << printBitsInt(integer) << " (" << integer << ")\n"; - -// this->_value = (unsigned int)(this->_value << 8); // shift to left to erase exponent part -// this->_value |= 1 << 31; // add the missing left 1 -//std::cout << "to left : " << printBitsInt(this->_value) << " (" << this->_value << ")\n"; -// this->_value = (unsigned int)this->_value >> 1; -//std::cout << "to right: " << printBitsInt(this->_value) << " (" << this->_value << ")\n"; -// this->_value |= sign; -//std::cout << "sign : " << printBitsInt(this->_value) << " (" << this->_value << ")\n"; -// this->_value = this->_value >> (30 - exponent - this->_frac); // shift to right untill fixed point -//std::cout << "_value : " << printBitsInt(this->_value) << " (" << this->_value << ")\n"; -//// int decimal = (unsigned int)(this->_value << (24)) >> 24; // extract decimal -////std::cout << "decimal : " << printBitsInt(decimal) << " (" << decimal << ")\n"; -// this->_value >>= this->_frac; -//std::cout << "int valu: " << printBitsInt(this->_value) << " (" << this->_value << ")\n"; -//// this->_value += (unsigned int)sign >> 31; -////std::cout << "val sign: " << printBitsInt(this->_value) << " (" << this->_value << ")\n"; -// this->_value = (this->_value << this->_frac); -//std::cout << "val sign: " << printBitsInt(this->_value) << " (" << this->_value << ")\n"; - + 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"; +*/ } /* diff --git a/d02/ex01/fixed b/d02/ex01/fixed index 7171e14..b9b8e43 100755 Binary files a/d02/ex01/fixed and b/d02/ex01/fixed differ diff --git a/d02/ex01/main.cpp b/d02/ex01/main.cpp index 78e4ce0..5bea77c 100644 --- a/d02/ex01/main.cpp +++ b/d02/ex01/main.cpp @@ -9,6 +9,16 @@ int main( void ) { 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 );