d02 ex02 et ex01 variables statics et min max

This commit is contained in:
Hugo LAMY
2022-02-18 18:17:00 +01:00
parent 494cb84f13
commit bd83d90b5f
9 changed files with 41 additions and 280 deletions

View File

@@ -48,6 +48,7 @@ std::string printBitsFloat(float num)
int const Fixed::_frac = 8;
int const Fixed::_max = -1U >> (_frac +1);
int const Fixed::_min = ~_max;
/*
@@ -73,17 +74,17 @@ Fixed::~Fixed( void ) {
*/
Fixed::Fixed(int integer) {
if (integer < ~this->_max || integer > this->_max)
if (integer < Fixed::_min || integer > Fixed::_max)
std::cout << "error: integer out of range" << '\n';
else
this->_value = integer << this->_frac;
this->_value = integer << Fixed::_frac;
}
Fixed::Fixed(float const floater) {
if (floater < ~this->_max || floater > this->_max)
if (floater < Fixed::_min || floater > Fixed::_max)
std::cout << "error: float out of range" << '\n';
else
this->_value = roundf(floater * (1 << this->_frac));
this->_value = roundf(floater * (1 << Fixed::_frac));
}
@@ -106,22 +107,22 @@ Fixed & Fixed::operator=( Fixed const & rhs ) {
*/
bool Fixed::operator< (Fixed const & rhs) const {
return this->toFloat() < rhs.toFloat();
return this->_value < rhs._value;
}
bool Fixed::operator> (Fixed const & rhs) const {
return this->toFloat() > rhs.toFloat();
return rhs < *this;
}
bool Fixed::operator<=(Fixed const & rhs) const {
return this->toFloat() <= rhs.toFloat();
return !(rhs > *this);
}
bool Fixed::operator>=(Fixed const & rhs) const {
return this->toFloat() >= rhs.toFloat();
return !(rhs < *this);
}
bool Fixed::operator==(Fixed const & rhs) const {
return this->toFloat() == rhs.toFloat();
}
bool Fixed::operator!=(Fixed const & rhs) const {
return this->toFloat() != rhs.toFloat();
return !(*this == rhs);
}
Fixed Fixed::operator+ ( Fixed const & rhs ) const {
Fixed result(*this);
@@ -135,34 +136,19 @@ Fixed Fixed::operator- ( Fixed const & rhs ) const {
}
Fixed Fixed::operator* ( Fixed const & rhs ) const {
Fixed result(*this);
result._value = ((long)result._value * (long)rhs._value) >> 8;
result._value = ((long)result._value * (long)rhs._value) >> Fixed::_frac;
return result;
}
Fixed Fixed::operator/ ( Fixed const & rhs ) const {
Fixed result(*this);
result._value = (long)(result._value << _frac) / rhs._value;
// if (rhs._value >> _frac != 0)
// result._value /= rhs._value >> _frac;
result._value = (long)(result._value << Fixed::_frac) / rhs._value;
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++() {
Fixed & Fixed::operator++() {
this->_value++;
return *this;
}
Fixed Fixed::operator--() {
Fixed & Fixed::operator--() {
this->_value--;
return *this;
}
@@ -217,10 +203,10 @@ void Fixed::setRawBits( int const raw ) {
}
int Fixed::toInt( void ) const {
return (this->_value >> this->_frac);
return (this->_value >> Fixed::_frac);
}
float Fixed::toFloat( void ) const {
return ((float)this->_value / (float)(1 << this->_frac));
return ((float)this->_value / (float)(1 << Fixed::_frac));
}