Fix multiplication rounding
authorLukas Jiriste <ljiriste@student.42prague.com>
Thu, 17 Oct 2024 14:08:03 +0000 (16:08 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Thu, 17 Oct 2024 14:08:03 +0000 (16:08 +0200)
ex02/Fixed.cpp

index 35b37bfadcd6b6c4ea664b16a2231a98cef93449..502ae743f808eb31ece11504f3b148afa6b01b8f 100644 (file)
@@ -111,7 +111,14 @@ Fixed      Fixed::operator*(const Fixed &other) const
 {
        Fixed   res;
 
-       res.m_num = (m_num * other.m_num) >> point_pos;
+       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);
 }