cpp02/ex03/Fixed.cpp

145 lines
3.2 KiB
C++
Raw Normal View History

2025-05-15 10:55:59 +02:00
#include "Fixed.hpp"
#include <bitset>
#include <sstream>
const int Fixed::fracbits(8);
Fixed::Fixed() { value = 0; }
Fixed::Fixed(const int val) { value = val << fracbits; }
Fixed::Fixed(const float val) {
const static int factor = 1 << fracbits;
float temp = (val * factor);
if (temp >= 0)
temp += 0.5f;
else
temp -= 0.5f;
value = (int)(temp);
}
Fixed::Fixed(const Fixed &other) { this->value = other.value; }
Fixed &Fixed::operator=(const Fixed &other) {
this->setRawBits(other.getRawBits());
return *this;
}
Fixed::~Fixed() {}
int Fixed::getRawBits(void) const { return value; }
void Fixed::setRawBits(int const raw) { value = raw; }
float Fixed::toFloat(void) const {
const static int factor = 1 << fracbits;
float result = (float)value / factor;
return (result);
}
int Fixed::toInt(void) const {
int result = (value >> fracbits);
return (result);
}
std::string Fixed::toBin(void) const {
std::stringstream result;
result << std::bitset<32 - fracbits>(value >> fracbits);
result << ".";
result << std::bitset<fracbits>(value & (1 << fracbits) - 1);
return result.str();
}
bool Fixed::operator>(const Fixed &other) const {
return this->value > other.value;
}
bool Fixed::operator<(const Fixed &other) const {
return this->value < other.value;
}
bool Fixed::operator>=(const Fixed &other) const {
return this->value >= other.value;
}
bool Fixed::operator<=(const Fixed &other) const {
return this->value <= other.value;
}
bool Fixed::operator==(const Fixed &other) const {
return this->value == other.value;
}
bool Fixed::operator!=(const Fixed &other) const {
return this->value != other.value;
}
const Fixed Fixed::operator+(const Fixed &other) const {
Fixed result = *this;
result.value += other.value;
return result;
}
const Fixed Fixed::operator-(const Fixed &other) const {
Fixed result = *this;
result.value -= other.value;
return result;
}
const Fixed Fixed::operator*(const Fixed &other) const {
Fixed result = *this;
result.value *= other.value;
result.value >>= fracbits;
return result;
}
const Fixed Fixed::operator/(const Fixed &other) const {
Fixed result = *this;
result.value /= other.value;
result.value <<= fracbits;
return result;
}
Fixed Fixed::operator++() {
this->value++;
return *this;
}
Fixed Fixed::operator--() {
this->value--;
return *this;
}
Fixed Fixed::operator++(int) {
Fixed result = *this;
operator++();
return result;
}
Fixed Fixed::operator--(int) {
Fixed result = *this;
operator--();
return result;
}
Fixed &Fixed::min(Fixed &left, Fixed &right) {
if (left < right)
return left;
return right;
}
const Fixed &Fixed::min(const Fixed &left, const Fixed &right) {
if (left < right)
return left;
return right;
}
Fixed &Fixed::max(Fixed &left, Fixed &right) {
if (left > right)
return left;
return right;
}
const Fixed &Fixed::max(const Fixed &left, const Fixed &right) {
if (left > right)
return left;
return right;
}
Fixed Fixed::abs() const {
Fixed result = *this;
if (result < 0)
return Fixed(0) - result;
return result;
}
std::ostream &operator<<(std::ostream &stream, const Fixed &fixed) {
stream << fixed.toFloat();
return stream;
}