aboutsummaryrefslogtreecommitdiff
path: root/cpp02/ex02/Fixed.cpp
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2020-10-19 12:42:39 +0200
committerCharles Cabergs <me@cacharle.xyz>2020-10-19 12:42:39 +0200
commit60c4c5309af87480fb32f3815bc02031eff43e9b (patch)
tree7e50e0728d320735ea3fbcf2ff10d2239acf97a1 /cpp02/ex02/Fixed.cpp
parenta3ff15046fa3d367ff7304dc9482e81597eb76b9 (diff)
downloadpiscine_cpp-60c4c5309af87480fb32f3815bc02031eff43e9b.tar.gz
piscine_cpp-60c4c5309af87480fb32f3815bc02031eff43e9b.tar.bz2
piscine_cpp-60c4c5309af87480fb32f3815bc02031eff43e9b.zip
Added cpp02/ex02
Diffstat (limited to 'cpp02/ex02/Fixed.cpp')
-rw-r--r--cpp02/ex02/Fixed.cpp85
1 files changed, 85 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;
+}