modif d01 ex03 weapon gettype return ref sur const string, d02 ex02 fini

This commit is contained in:
Hugo LAMY
2022-02-16 18:06:24 +01:00
parent d45a096244
commit ae62a6d0d7
7 changed files with 149 additions and 88 deletions

View File

@@ -1,5 +1,6 @@
#include "Fixed.hpp"
/*
* functions to print numbers in binary
* for the float, found help from stackoverflow :
@@ -35,6 +36,7 @@ std::string printBitsFloat(float num)
return "";
}
/*
* statics variables initialisation
*
@@ -48,6 +50,7 @@ std::string printBitsFloat(float num)
int const Fixed::_frac = 8;
int const Fixed::_max = -1U >> (_frac +1);
/*
* default constructor / copy constructor / destructor
*/
@@ -65,6 +68,7 @@ Fixed::~Fixed( void ) {
return;
}
/*
* int and float constructors
*/
@@ -80,9 +84,10 @@ Fixed::Fixed(float const floater) {
if (floater < ~this->_max || floater > this->_max)
std::cout << "error: float out of range" << '\n';
else
this->_value = floater * (1 << this->_frac);
this->_value = roundf(floater * (1 << this->_frac));
}
/*
* assignement operator
*/
@@ -93,8 +98,12 @@ Fixed & Fixed::operator=( Fixed const & rhs ) {
return *this;
}
/*
* operators < ; > ; <= ; == ; != ; + ; - ; * ; / ; ++ ; --
* ref : https://en.cppreference.com/w/cpp/language/operators
* for division, if you want to avoid floats (legitimate) :
* https://stackoverflow.com/questions/8506317/fixed-point-unsigned-division-in-c
*/
bool Fixed::operator< (Fixed const & rhs) const {
@@ -127,12 +136,50 @@ Fixed Fixed::operator* ( Fixed const & rhs ) const {
Fixed Fixed::operator/ ( Fixed const & rhs ) const {
return Fixed( this->toFloat() / rhs.toFloat() );
}
//void Fixed::operator++( void ){
// this->_value += 1;
//}
//void Fixed::operator--( void ){
// this->_value -= 1;
//}
Fixed Fixed::operator++() {
this->_value++;
return *this;
}
Fixed Fixed::operator--() {
this->_value--;
return *this;
}
Fixed Fixed::operator++( int ) {
Fixed old = *this;
Fixed::operator++();
return old;
}
Fixed Fixed::operator--( int ) {
Fixed old = *this;
Fixed::operator--();
return old;
}
/*
* returns min and max
*/
Fixed const & Fixed::min(Fixed const & lhs, Fixed const & rhs) {
if (lhs < rhs)
return lhs;
return rhs;
}
Fixed const & Fixed::max(Fixed const & lhs, Fixed const & rhs) {
if (lhs > rhs)
return lhs;
return rhs;
}
Fixed & Fixed::min(Fixed & lhs, Fixed & rhs) {
if (lhs < rhs)
return lhs;
return rhs;
}
Fixed & Fixed::max(Fixed & lhs, Fixed & rhs) {
if (lhs > rhs)
return lhs;
return rhs;
}
/*
@@ -154,9 +201,10 @@ float Fixed::toFloat( void ) const {
return ((float)this->_value / (float)(1 << this->_frac));
}
/*
* overload "<<" -> output fixed point in float representation
* took here : https://github.com/pgomez-a/42_CPP_Piscine/blob/master/cpp02/ex01/Fixed.cpp
* found here : https://github.com/pgomez-a/42_CPP_Piscine/blob/master/cpp02/ex01/Fixed.cpp
*/
std::ostream & operator<<(std::ostream & o, Fixed const & rhs)