d02 ex02 multiplications sans float et sans long pbm abandon
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
#include "Fixed.hpp"
|
||||
|
||||
|
||||
/*
|
||||
* functions to print numbers in binary
|
||||
* for the float, found help from stackoverflow :
|
||||
@@ -125,15 +124,90 @@ bool Fixed::operator!=(Fixed const & rhs) const {
|
||||
return this->toFloat() != rhs.toFloat();
|
||||
}
|
||||
Fixed Fixed::operator+ ( Fixed const & rhs ) const {
|
||||
return Fixed( this->toFloat() + rhs.toFloat() );
|
||||
Fixed result(*this);
|
||||
result._value += rhs._value;
|
||||
return (result);
|
||||
}
|
||||
Fixed Fixed::operator- ( Fixed const & rhs ) const {
|
||||
return Fixed( this->toFloat() - rhs.toFloat() );
|
||||
Fixed result(*this);
|
||||
result._value -= rhs._value;
|
||||
return (result);
|
||||
}
|
||||
Fixed Fixed::operator* ( Fixed const & rhs ) const {
|
||||
return Fixed( this->toFloat() * rhs.toFloat() );
|
||||
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;
|
||||
|
||||
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 Fixed::operator++() {
|
||||
|
||||
Reference in New Issue
Block a user