float to int les deux methodes fonctionnent
This commit is contained in:
@@ -80,9 +80,9 @@ Fixed::Fixed(int integer) {
|
|||||||
std::cout << "error: integer out of range" << '\n';
|
std::cout << "error: integer out of range" << '\n';
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
std::cout << "integer : " << printBitsInt(integer) << " (" << integer << ")\n";
|
// std::cout << "integer : " << printBitsInt(integer) << " (" << integer << ")\n";
|
||||||
this->_value = integer << this->_frac;
|
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) {
|
Fixed::Fixed(float const floater) {
|
||||||
@@ -93,52 +93,22 @@ Fixed::Fixed(float const floater) {
|
|||||||
std::cout << "error: float out of range" << '\n';
|
std::cout << "error: float out of range" << '\n';
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
std::cout << "floater : " << printBitsFloat(floater) << " (" << floater << ")\n";
|
// std::cout << "floater : " << printBitsFloat(floater) << " (" << floater << ")\n";
|
||||||
this->_value = floater * (1 << this->_frac);
|
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";
|
this->_value = *((int *)&floater); // cast float to int
|
||||||
|
int sign = this->_value & (1 << 31); // extract sign
|
||||||
int sign = this->_value & (1 << 31); // set most left bits to sign
|
int exponent = ((unsigned int)(this->_value << 1) >> 24) - 127; // extract exponent
|
||||||
std::cout << "sign : " << printBitsInt(sign) << " (" << sign << ")\n";
|
int integer = (this->_value << 8) | (1 << 31); // add left 1
|
||||||
|
integer = (unsigned int)integer >> (31 - this->_frac - exponent);// align to right
|
||||||
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";
|
|
||||||
if (sign != 0)
|
if (sign != 0)
|
||||||
integer = ~integer; // reverse for negatif
|
integer = (~integer + 1); // reverse negatif
|
||||||
std::cout << "reverse : " << printBitsInt(integer) << " (" << integer << ")\n";
|
integer = (integer << (30 - this->_frac - exponent)) | sign; // add sign
|
||||||
integer = ((unsigned int)integer >> 1) | sign; // add sign to integer part
|
integer >>= (30 - this->_frac - exponent); // align right
|
||||||
std::cout << "left +/-: " << printBitsInt(integer) << " (" << integer << ")\n";
|
std::cout << "integer : " << 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";
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
BIN
d02/ex01/fixed
BIN
d02/ex01/fixed
Binary file not shown.
@@ -9,6 +9,16 @@ int main( void ) {
|
|||||||
Fixed b(1000.5979f);
|
Fixed b(1000.5979f);
|
||||||
std::cout << '\n';
|
std::cout << '\n';
|
||||||
Fixed c(-1105.979f);
|
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 a;
|
||||||
// Fixed const b( 10 );
|
// Fixed const b( 10 );
|
||||||
|
|||||||
Reference in New Issue
Block a user