cpp/CPP02/ex02/Fixed.cpp
2024-12-04 17:29:31 +01:00

131 lines
2.5 KiB
C++

#include "Fixed.hpp"
Fixed::Fixed(void) {
this->rawBits = 0;
}
Fixed::Fixed(const int raw) {
this->rawBits = int(raw * (1 << bits));
}
Fixed::Fixed(const float raw) {
this->rawBits = int(roundf(raw * (1 << bits)));
}
Fixed::~Fixed() {
}
Fixed& Fixed::operator=(const Fixed &fixed) {
if (this != &fixed) {
this->rawBits = fixed.rawBits;
}
return *this;
}
std::ostream& operator<<(std::ostream& os, const Fixed &fixed) {
os << fixed.toFloat();
return os;
}
Fixed::Fixed(const Fixed &fixed) {
this->rawBits = fixed.rawBits;
}
int Fixed::toInt(void) const{
return int(this->rawBits) / int(1 << this->bits);
}
float Fixed::toFloat(void) const{
return float(this->rawBits) / float(1 << this->bits);
}
int Fixed::getRawBits(void) const {
return this->rawBits;
}
void Fixed::setRawBits(const int raw) {
this->rawBits = raw;
}
bool Fixed::operator>(const Fixed &fixed) const {
return this->rawBits > fixed.rawBits;
}
bool Fixed::operator<(const Fixed &fixed) const {
return this->rawBits < fixed.rawBits;
}
bool Fixed::operator>=(const Fixed &fixed) const {
return this->rawBits >= fixed.rawBits;
}
bool Fixed::operator<=(const Fixed &fixed) const {
return this->rawBits <= fixed.rawBits;
}
bool Fixed::operator==(const Fixed &fixed) const {
return this->rawBits == fixed.rawBits;
}
bool Fixed::operator!=(const Fixed &fixed) const {
return this->rawBits != fixed.rawBits;
}
Fixed Fixed::operator+(const Fixed &fixed) const {
Fixed result = Fixed(this->toFloat() + fixed.toFloat());
return result;
}
Fixed Fixed::operator-(const Fixed &fixed) const {
Fixed result = Fixed(this->toFloat() - fixed.toFloat());
return result;
}
Fixed Fixed::operator*(const Fixed &fixed) const {
Fixed result = Fixed(this->toFloat() * fixed.toFloat());
return result;
}
Fixed Fixed::operator/(const Fixed &fixed) const {
Fixed result = Fixed(this->toFloat() / fixed.toFloat());
return result;
}
Fixed& Fixed::operator++(void) {
this->rawBits++;
return *this;
}
Fixed Fixed::operator++(int) {
Fixed tmp(*this);
this->rawBits++;
return tmp;
}
Fixed& Fixed::operator--(void) {
this->rawBits--;
return *this;
}
Fixed Fixed::operator--(int) {
Fixed tmp(*this);
this->rawBits--;
return tmp;
}
const Fixed& Fixed::min(Fixed& a, Fixed& b) {
return (a < b) ? a : b;
}
const Fixed& Fixed::min(const Fixed& a, const Fixed& b) {
return (a < b) ? a : b;
}
const Fixed& Fixed::max(Fixed& a, Fixed& b) {
return (a > b) ? a : b;
}
const Fixed& Fixed::max(const Fixed& a, const Fixed& b) {
return (a > b) ? a : b;
}