From: Lukas Jiriste Date: Tue, 7 Jan 2025 17:33:42 +0000 (+0100) Subject: Fix Fixed multiplication and division X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;ds=inline;p=42%2FCPP_Module_02 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. --- 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++()