Fix Fixed multiplication and division trunk
authorLukas Jiriste <ljiriste@student.42prague.com>
Tue, 7 Jan 2025 17:33:42 +0000 (18:33 +0100)
committerLukas Jiriste <ljiriste@student.42prague.com>
Tue, 7 Jan 2025 17:33:42 +0000 (18:33 +0100)
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
ex03/Fixed.cpp

index 1d6acbe91ed0cabb76ddd2a2c776dbbbd8419d2e..37c6a003463e0811706d957b076481701a693ea2 100644 (file)
@@ -109,25 +109,12 @@ Fixed     Fixed::operator-(const Fixed &other) const
 }
 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  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++()
 }
 
 Fixed  Fixed::operator++()
index 362baa19e7a922b403fd19db19a5b2e60d5a51e7..4949dd61940630fd0bdb9efd422cddc92f155e99 100644 (file)
@@ -103,25 +103,12 @@ Fixed     Fixed::operator-(const Fixed &other) const
 }
 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  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++()
 }
 
 Fixed  Fixed::operator++()