d02 ex02 division avec long sans float

This commit is contained in:
Hugo LAMY
2022-02-18 17:48:54 +01:00
parent 6d2c0f9f29
commit 494cb84f13
4 changed files with 281 additions and 91 deletions

View File

@@ -135,80 +135,28 @@ Fixed Fixed::operator- ( Fixed const & rhs ) const {
}
Fixed Fixed::operator* ( Fixed const & rhs ) const {
Fixed result(*this);
int left = this->_value;
int right = rhs._value;
result._value = 0;
// left:5 right:10
while (right > 0) // 1
{
result._value /= 10; // 0/10 = 0 0/10 = 0
result._value += (left * (right % 10)); // 0+(5*0) = 0 0+(5*1) = 1
std::cout << "intermediaire: " << result << " "; //
right /= 10; // 10/10 = 1 1/10 = 0
}
result._value >>= _frac;
// result._value *= rhs._value >> _frac;
// result._value += (this->_value * (rhs._value & ((1 << (_frac + 1)) - 1))) >> _frac;
// result._value = ((long)result._value * (long)rhs._value) >> 8;
result._value = ((long)result._value * (long)rhs._value) >> 8;
return result;
//
// 47 * 256 = 12 032
// 5 * 10 -> 5*256 * 10*256 -> 5 * 10 * 256^2
// 256^2 = 65 536
// 256 : 1 00000000
// 65 536 : 1 00000000 00000000
//
//
// 5 * 10 = 50
// 101 * 1010 = 110010
// 101.00000000 * 1010.00000000 = 110010.00000000
// 101 00000000 * 1010 00000000 = 110010 00000000
// 110010 00000000
// 128 * 2560 = 327680
//
//
// 3 * 5 = 15
// 11 * 101 = 1111
//
//
// 3.5 * 5.25 = 18.375
// 11.1 * 101.01 = 10010.011
// 11 1 * 101 01 = 10010 011
// 7 * 21 = 147
//
//
// 3.5 * 5.25
// 3.5 * (5 + 0.25)
// 3.5 * 5
// + 3.5 * 0.25
//
// 3.5 * 4.25 = 14.875
// 3.5 * (0.05 + 0.20 + 4.00)
// 3.5 * 0.05 = 0.175 = 17.5 / 100
// 3.5 * 0.20 = 0.7 = 7 / 10
// 3.5 * 4.00 = 14
// 14 + 0.7 + 0.175 = 14.875
//
// 3.5 * 5 = 17.5
// 3.5 * 2 = 7
// 3.5 * 4 = 14
// (((17.5 / 10) + 7) / 10) + 14
// 17.5 / 10
// 1.75 + 7
// 8.75 / 10
// 0.875 + 14
//
}
Fixed Fixed::operator/ ( Fixed const & rhs ) const {
std::cout << this->_value << " " << rhs._value << " "
<< (( (this->_value << 8) / rhs._value )) << "\n";
return Fixed( this->toFloat() / rhs.toFloat() );
Fixed result(*this);
result._value = (long)(result._value << _frac) / rhs._value;
// if (rhs._value >> _frac != 0)
// result._value /= rhs._value >> _frac;
return result;
// return Fixed( this->toFloat() / rhs.toFloat() );
//
// 10 / 5 = 2
// 5 / 10 = 0.5
// 10 / 6 = 1.666...
// 6 / 10 = 0.6
//
}
Fixed Fixed::operator++() {
this->_value++;