float to int les deux methodes fonctionnent

This commit is contained in:
hugogogo
2022-02-14 17:17:19 +01:00
parent 051936c06e
commit 9036ded700
3 changed files with 25 additions and 45 deletions

View File

@@ -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";
*/
}
/*