From 60c4c5309af87480fb32f3815bc02031eff43e9b Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Mon, 19 Oct 2020 12:42:39 +0200 Subject: Added cpp02/ex02 --- cpp02/ex02/Fixed.cpp | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 cpp02/ex02/Fixed.cpp (limited to 'cpp02/ex02/Fixed.cpp') diff --git a/cpp02/ex02/Fixed.cpp b/cpp02/ex02/Fixed.cpp new file mode 100644 index 0000000..7769083 --- /dev/null +++ b/cpp02/ex02/Fixed.cpp @@ -0,0 +1,85 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Fixed.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/13 11:45:18 by charles #+# #+# */ +/* Updated: 2020/10/19 12:41:30 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Fixed.hpp" + +Fixed::Fixed() : m_value(0) {} + +Fixed::Fixed(Fixed const& other) { *this = other; } + +Fixed::~Fixed() {} + +Fixed& Fixed::operator=(Fixed const& other) +{ + m_value = other.getRawBits(); + return *this; +} + +Fixed::Fixed(const int from) +{ + m_value = from << m_fractionalBits; +} + +Fixed::Fixed(const float from) +{ + m_value = roundf(from * (1 << m_fractionalBits)); +} + +int Fixed::getRawBits() const +{ + return m_value; +} + +void Fixed::setRawBits(int const raw) +{ + m_value = raw; +} + +float Fixed::toFloat() const +{ + return (float)m_value / (float)(1 << m_fractionalBits); +} + +int Fixed::toInt() const +{ + return m_value >> m_fractionalBits; +} + +Fixed Fixed::operator+(Fixed const& other) const { return Fixed(toFloat() + other.toFloat()); } +Fixed Fixed::operator-(Fixed const& other) const { return Fixed(toFloat() - other.toFloat()); } +Fixed Fixed::operator*(Fixed const& other) const { return Fixed(toFloat() * other.toFloat()); } +Fixed Fixed::operator/(Fixed const& other) const { return Fixed(toFloat() / other.toFloat()); } + +Fixed& Fixed::operator++() { m_value++; return *this; } +Fixed& Fixed::operator--() { m_value++; return *this; } +Fixed Fixed::operator++(int) { Fixed copy(*this); m_value++; return copy; } +Fixed Fixed::operator--(int) { Fixed copy(*this); m_value--; return copy; } + +bool Fixed::operator<(Fixed const& other) const { return m_value < other.m_value; } +bool Fixed::operator>(Fixed const& other) const { return m_value > other.m_value; } +bool Fixed::operator<=(Fixed const& other) const { return !(*this > other); } +bool Fixed::operator>=(Fixed const& other) const { return !(*this < other); } +bool Fixed::operator==(Fixed const& other) const { return m_value == other.m_value; } +bool Fixed::operator!=(Fixed const& other) const { return !(*this == other); } + +int Fixed::getFractionalBits() { return m_fractionalBits; } + +Fixed& Fixed::max(Fixed& a, Fixed& b) { return a > b ? a : b; } +Fixed& Fixed::min(Fixed& a, Fixed& b) { return a < b ? a : b; } +Fixed const& Fixed::max(Fixed const& a, Fixed const& b) { return a > b ? a : b; } +Fixed const& Fixed::min(Fixed const& a, Fixed const& b) { return a < b ? a : b; } + +std::ostream& operator<<(std::ostream& out, Fixed const& f) +{ + out << f.toFloat(); + return out; +} -- cgit