aboutsummaryrefslogtreecommitdiff
path: root/cpp02/ex02
diff options
context:
space:
mode:
Diffstat (limited to 'cpp02/ex02')
-rw-r--r--cpp02/ex02/Fixed.cpp85
-rw-r--r--cpp02/ex02/Fixed.hpp69
-rw-r--r--cpp02/ex02/main.cpp30
3 files changed, 184 insertions, 0 deletions
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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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;
+}
diff --git a/cpp02/ex02/Fixed.hpp b/cpp02/ex02/Fixed.hpp
new file mode 100644
index 0000000..d839260
--- /dev/null
+++ b/cpp02/ex02/Fixed.hpp
@@ -0,0 +1,69 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* Fixed.hpp :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/04/13 11:40:49 by charles #+# #+# */
+/* Updated: 2020/10/19 12:41:46 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#ifndef FIXED_HPP
+# define FIXED_HPP
+
+# include <iostream>
+# include <cmath>
+
+class Fixed
+{
+public:
+ Fixed();
+ Fixed(Fixed const& other);
+ ~Fixed();
+ Fixed& operator=(Fixed const& other);
+
+ Fixed(const int from);
+ Fixed(const float from);
+
+ int getRawBits() const;
+ void setRawBits(int const raw);
+
+ float toFloat() const;
+ int toInt() const;
+
+ Fixed operator+(Fixed const& other) const;
+ Fixed operator-(Fixed const& other) const;
+ Fixed operator*(Fixed const& other) const;
+ Fixed operator/(Fixed const& other) const;
+
+ Fixed& operator++();
+ Fixed& operator--();
+ Fixed operator++(int);
+ Fixed operator--(int);
+
+ bool operator<(Fixed const& other) const;
+ bool operator>(Fixed const& other) const;
+ bool operator<=(Fixed const& other) const;
+ bool operator>=(Fixed const& other) const;
+ bool operator==(Fixed const& other) const;
+ bool operator!=(Fixed const& other) const;
+
+ static Fixed& max(Fixed& a, Fixed& b);
+ static Fixed& min(Fixed& a, Fixed& b);
+ static const Fixed& max(Fixed const& a, Fixed const& b);
+ static const Fixed& min(Fixed const& a, Fixed const& b);
+
+ static int getFractionalBits();
+
+private:
+ int m_value;
+ static int const m_fractionalBits = 8;
+};
+
+std::ostream& operator<<(std::ostream& out, Fixed const& f);
+
+
+
+#endif
diff --git a/cpp02/ex02/main.cpp b/cpp02/ex02/main.cpp
new file mode 100644
index 0000000..7625260
--- /dev/null
+++ b/cpp02/ex02/main.cpp
@@ -0,0 +1,30 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* main.cpp :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/04/13 11:51:15 by charles #+# #+# */
+/* Updated: 2020/10/19 12:42:08 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <iostream>
+#include "Fixed.hpp"
+
+int main(void)
+{
+ Fixed a;
+ Fixed const b(Fixed(5.05f) * Fixed(2));
+
+ std::cout << a << std::endl;
+ std::cout << ++a << std::endl;
+ std::cout << a << std::endl;
+ std::cout << a++ << std::endl;
+ std::cout << a << std::endl;
+ std::cout << b << std::endl;
+ std::cout << Fixed::max(a, b) << std::endl;
+
+ return 0;
+}