|
@@ -0,0 +1,152 @@
|
|
|
|
|
+/* ************************************************************************** */
|
|
|
|
|
+/* */
|
|
|
|
|
+/* ::: :::::::: */
|
|
|
|
|
+/* Fixed.class.cpp :+: :+: :+: */
|
|
|
|
|
+/* +:+ +:+ +:+ */
|
|
|
|
|
+/* By: bchanot <bchanot@gmail.fr> +#+ +:+ +#+ */
|
|
|
|
|
+/* +#+#+#+#+#+ +#+ */
|
|
|
|
|
+/* Created: 2025/07/16 16:07:34 by bchanot #+# #+# */
|
|
|
|
|
+/* Updated: 2025/07/17 18:55:07 by bchanot ### ########.fr */
|
|
|
|
|
+/* */
|
|
|
|
|
+/* ************************************************************************** */
|
|
|
|
|
+
|
|
|
|
|
+#include "Fixed.class.hpp"
|
|
|
|
|
+#include <iostream>
|
|
|
|
|
+#include <cmath>
|
|
|
|
|
+
|
|
|
|
|
+Fixed::Fixed(void) : _value(0) {
|
|
|
|
|
+ std::cout << "Default constructor called" << std::endl;
|
|
|
|
|
+ return ;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+Fixed::Fixed(int const num) : _value((int)roundf(num * (1 << _frac))){
|
|
|
|
|
+ std::cout << "Int constructor called" << std::endl;
|
|
|
|
|
+ return ;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+Fixed::Fixed(float const num) : _value((float)roundf(num * (1 << _frac))){
|
|
|
|
|
+ std::cout << "Float constructor called" << std::endl;
|
|
|
|
|
+ return ;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+Fixed::Fixed(Fixed const &src) {
|
|
|
|
|
+ *this = src;
|
|
|
|
|
+ std::cout << "Copy constructor called" << std::endl;
|
|
|
|
|
+ return ;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+Fixed::~Fixed(void) {
|
|
|
|
|
+ std::cout << "Destructor called" << std::endl;
|
|
|
|
|
+ return ;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+int Fixed::getRawBits(void) const {
|
|
|
|
|
+ std::cout << "getRawBits member function called" << std::endl;
|
|
|
|
|
+ return this->_value;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void Fixed::setRawBits(int const raw) {
|
|
|
|
|
+ std::cout << "setRawBits member function called" << std::endl;
|
|
|
|
|
+ this->_value = raw;
|
|
|
|
|
+ return ;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+Fixed &Fixed::operator=(Fixed const &rhs) {
|
|
|
|
|
+ std::cout << "Copy assignment operator called" << std::endl;
|
|
|
|
|
+ this->_value = rhs.getRawBits();
|
|
|
|
|
+ return *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();
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+bool Fixed::operator<=(Fixed const &rhs) const {
|
|
|
|
|
+ return this->toFloat() <= rhs.toFloat();
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+bool Fixed::operator>=(Fixed const &rhs) const {
|
|
|
|
|
+ return this->toFloat() >= rhs.toFloat();
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+bool Fixed::operator<(Fixed const &rhs) const {
|
|
|
|
|
+ return this->toFloat() < rhs.toFloat();
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+bool Fixed::operator>(Fixed const &rhs) const {
|
|
|
|
|
+ return this->toFloat() > rhs.toFloat();
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+Fixed Fixed::operator+(Fixed const &rhs) const {
|
|
|
|
|
+ return Fixed::toFloat() + rhs.toFloat();
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+Fixed Fixed::operator-(Fixed const &rhs) const {
|
|
|
|
|
+ return Fixed::toFloat() - rhs.toFloat();
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+Fixed Fixed::operator*(Fixed const &rhs) const {
|
|
|
|
|
+ return Fixed::toFloat() * rhs.toFloat();
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+Fixed Fixed::operator/(Fixed const &rhs) const {
|
|
|
|
|
+ return Fixed::toFloat() / rhs.toFloat();
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+Fixed &Fixed::operator++(void) {
|
|
|
|
|
+ this->_value++;
|
|
|
|
|
+ return *this;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+Fixed &Fixed::operator--(void) {
|
|
|
|
|
+ this->_value--;
|
|
|
|
|
+ return *this;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+Fixed Fixed::operator++(int) {
|
|
|
|
|
+ Fixed other(*this);
|
|
|
|
|
+
|
|
|
|
|
+ operator++();
|
|
|
|
|
+ return other;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+Fixed Fixed::operator--(int) {
|
|
|
|
|
+ Fixed other(*this);
|
|
|
|
|
+
|
|
|
|
|
+ operator--();
|
|
|
|
|
+ return other;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+const Fixed &Fixed::min(Fixed const &left, Fixed const &right) {
|
|
|
|
|
+ return left < right ? left : right;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+Fixed &Fixed::min(Fixed &left, Fixed &right) {
|
|
|
|
|
+ return left < right ? left : right;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+const Fixed &Fixed::max(Fixed const &left, Fixed const &right) {
|
|
|
|
|
+ return left > right ? left : right;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+Fixed &Fixed::max(Fixed &left, Fixed &right) {
|
|
|
|
|
+ return left > right ? left : right;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+float Fixed::toFloat(void) const {
|
|
|
|
|
+ return (float(this->_value) / float(1 << this->_frac));
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+int Fixed::toInt(void) const {
|
|
|
|
|
+ return (int(this->_value) / int(1 << this->_frac));
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+std::ostream &operator<<(std::ostream &o, Fixed const &i) {
|
|
|
|
|
+ o << i.toFloat();
|
|
|
|
|
+ return o;
|
|
|
|
|
+}
|