feat(ex02): it works
This commit is contained in:
parent
fce90b9a43
commit
6f17e2e888
5 changed files with 251 additions and 0 deletions
137
ex02/Fixed.cpp
Normal file
137
ex02/Fixed.cpp
Normal file
|
|
@ -0,0 +1,137 @@
|
|||
#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;
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &stream, const Fixed &fixed) {
|
||||
stream << fixed.toFloat();
|
||||
return stream;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue