From 272358014f44390a1c4e48e1228f80c1e2a069e2 Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Tue, 7 Jan 2025 18:33:42 +0100 Subject: [PATCH] Fix Fixed multiplication and division I thought I would implement the arithmetic without float to ensure precise results, but it proved to be quite difficult and probably not the effort in this exercise. --- ex02/Fixed.cpp | 17 ++--------------- ex03/Fixed.cpp | 17 ++--------------- 2 files changed, 4 insertions(+), 30 deletions(-) diff --git a/ex02/Fixed.cpp b/ex02/Fixed.cpp index 1d6acbe..37c6a00 100644 --- a/ex02/Fixed.cpp +++ b/ex02/Fixed.cpp @@ -109,25 +109,12 @@ Fixed Fixed::operator-(const Fixed &other) const } Fixed Fixed::operator*(const Fixed &other) const { - Fixed res; - - res.m_num = m_num * other.m_num; - if (res.m_num & (1 << (point_pos - 1))) - { - res.m_num >>= point_pos; - ++res.m_num; - return (res); - } - res.m_num >>= point_pos; - return (res); + return (toFloat() * other.toFloat()); } Fixed Fixed::operator/(const Fixed &other) const { - Fixed res; - - res.m_num = (m_num << point_pos) / other.m_num; - return (res); + return (toFloat() / other.toFloat()); } Fixed Fixed::operator++() diff --git a/ex03/Fixed.cpp b/ex03/Fixed.cpp index 362baa1..4949dd6 100644 --- a/ex03/Fixed.cpp +++ b/ex03/Fixed.cpp @@ -103,25 +103,12 @@ Fixed Fixed::operator-(const Fixed &other) const } Fixed Fixed::operator*(const Fixed &other) const { - Fixed res; - - res.m_num = m_num * other.m_num; - if (res.m_num & (1 << (point_pos - 1))) - { - res.m_num >>= point_pos; - ++res.m_num; - return (res); - } - res.m_num >>= point_pos; - return (res); + return (toFloat() * other.toFloat()); } Fixed Fixed::operator/(const Fixed &other) const { - Fixed res; - - res.m_num = (m_num << point_pos) / other.m_num; - return (res); + return (toFloat() / other.toFloat()); } Fixed Fixed::operator++() -- 2.30.2