From 9036ded700fac211814fee5dfca43078ebf3b9ec Mon Sep 17 00:00:00 2001 From: hugogogo Date: Mon, 14 Feb 2022 17:17:19 +0100 Subject: [PATCH] float to int les deux methodes fonctionnent --- d02/ex01/Fixed.cpp | 60 ++++++++++++--------------------------------- d02/ex01/fixed | Bin 19032 -> 19032 bytes d02/ex01/main.cpp | 10 ++++++++ 3 files changed, 25 insertions(+), 45 deletions(-) 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 7171e14918b5127e3fa16a4723d188018b8c8dce..b9b8e43349dd146896973276a9a2a7762bae7ffa 100755 GIT binary patch delta 4105 zcmZ`+4Nz3q6@L2w#pP$06%4E(3jtIAkYYuGNxJ^*x^bj~%_L17jY=}DZ8QCGL~RP) zMS@{{Vm7_B**c7&X=+oOCQOMPOuJ@jtk5LYwb~4In91ts%-xnTn}~>1ef`dT?=6cV zZ)V?l-}%0C?!D)p`*wGz(=^m+3PjBL4oO`3j+ZW;v|TCC(3JwGc(_u~d(2d0p5+Ag z4D?6x*?%)GdRs;UUs>ko@iRn6i z-C|AqS|*s^%#R{wu?pc9 zXn+s^A@3kRK@i(5#j-VDgdZi>Yz+K-nkoovgYD%Tq=TREl`2rE#wihK4i|PBd2=pi+a&fyx z|4`7>n3PNj5$0PpZbu^Ys1Zso9v!1;wQVzKngdthR9vI|(R9op=$~m!_jlTK-=G+! zF{o5S-R<#*o!q0yBF zO{4J-ZeXmIQ&FsP%X{Jg4*Kg(iD~!TV25n>yiO=Ym5jC;BAwrO_R~9HyaIZ z6zYsts4~z<>B7!7VrOqcs+H~uhCAUBwBVQL=L`R zBfqGeFKcw7oL*71)$ocexMg)<4c9H-?7wbVjJ^+1fj+nU8!3%)oVY{-dea z5{D~9&yW$rv$c`)@toAY-qRR@d|bUQ!mS4HQQAcMN(jiwAJGz|8-l(>qmYZgtHnzO zjfHjd@50%4AGS=Xqr*T6{T9X2atSVwitFX>}J{rSa%m5p25Q#us+xi z@s-sL+l6I@$qzeBe%Nu?Vc1+67U;&ERxtnaiK$!tTZWna^Xan zxXN&^#3z1@H8|$Wcrzbx)MjjkA_hrnpopDyIZ5*|FfCglNlGL09mjj3{ zAqO-uA~#hj5mTiDF$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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 );