Fixed.cpp
  1  /* ************************************************************************** */
  2  /*                                                                            */
  3  /*                                                        :::      ::::::::   */
  4  /*   Fixed.cpp                                          :+:      :+:    :+:   */
  5  /*                                                    +:+ +:+         +:+     */
  6  /*   By: gychoi <gychoi@student.42seoul.kr>         +#+  +:+       +#+        */
  7  /*                                                +#+#+#+#+#+   +#+           */
  8  /*   Created: 2023/08/02 17:46:11 by gychoi            #+#    #+#             */
  9  /*   Updated: 2023/08/04 17:47:00 by gychoi           ###   ########.fr       */
 10  /*                                                                            */
 11  /* ************************************************************************** */
 12  
 13  #include <cmath>
 14  #include "Fixed.hpp"
 15  
 16  Fixed::Fixed(void) : _rawBits(0) {}
 17  
 18  Fixed::Fixed(Fixed const& fixed)
 19  {
 20  	_rawBits = fixed.getRawBits();
 21  }
 22  
 23  Fixed::Fixed(int const intValue)
 24  {
 25  	_rawBits = intValue << _fractionalBits;
 26  }
 27  
 28  Fixed::Fixed(float const floatValue)
 29  {
 30  	_rawBits = static_cast<int>(roundf(floatValue * (1 << _fractionalBits)));
 31  }
 32  
 33  Fixed&	Fixed::operator=(Fixed const& fixed)
 34  {
 35  	if (this != &fixed)
 36  		setRawBits(fixed.getRawBits());
 37  	return (*this);
 38  }
 39  
 40  Fixed::~Fixed(void) {}
 41  
 42  int	Fixed::getRawBits(void) const
 43  {
 44  	return (_rawBits);
 45  }
 46  
 47  void	Fixed::setRawBits(int const raw)
 48  {
 49  	_rawBits = raw;
 50  }
 51  
 52  int	Fixed::toInt(void) const
 53  {
 54  	return (_rawBits / (1 <<_fractionalBits));
 55  }
 56  
 57  float	Fixed::toFloat(void) const
 58  {
 59  	return (static_cast<float>(_rawBits) / (1 << _fractionalBits));
 60  }
 61  
 62  std::ostream&	operator<<(std::ostream& os, Fixed const& fixed)
 63  {
 64  	os << fixed.toFloat();
 65  	return (os);
 66  }
 67  
 68  bool	Fixed::operator>(Fixed const& fixed) const
 69  {
 70  	return (_rawBits > fixed.getRawBits());
 71  }
 72  
 73  bool	Fixed::operator<(Fixed const& fixed) const
 74  {
 75  	return (_rawBits < fixed.getRawBits());
 76  }
 77  
 78  bool	Fixed::operator>=(Fixed const& fixed) const
 79  {
 80  	return (_rawBits >= fixed.getRawBits());
 81  }
 82  
 83  bool	Fixed::operator<=(Fixed const& fixed) const
 84  {
 85  	return (_rawBits <= fixed.getRawBits());
 86  }
 87  
 88  bool	Fixed::operator==(Fixed const& fixed) const
 89  {
 90  	return (_rawBits == fixed.getRawBits());
 91  }
 92  
 93  bool	Fixed::operator!=(Fixed const& fixed) const
 94  {
 95  	return (_rawBits != fixed.getRawBits());
 96  }
 97  
 98  typedef long long	int64;
 99  
100  Fixed	Fixed::operator+(Fixed const& fixed) const
101  {
102  	Fixed	ret;
103  	int64	b1 = _rawBits;
104  	int64	b2 = fixed.getRawBits();
105  
106  	ret.setRawBits(static_cast<int>(b1 + b2));
107  	return (ret);
108  }
109  
110  Fixed	Fixed::operator-(Fixed const& fixed) const
111  {
112  	Fixed	ret;
113  	int64	b1 = _rawBits;
114  	int64	b2 = fixed.getRawBits();
115  
116  	ret.setRawBits(static_cast<int>(b1 - b2));
117  	return (ret);
118  }
119  
120  Fixed	Fixed::operator*(Fixed const& fixed) const
121  {
122  	Fixed	ret;
123  	int64	b1 = _rawBits;
124  	int64	b2 = fixed.getRawBits();
125  
126  	ret.setRawBits(static_cast<int>((b1 * b2) / (1 << _fractionalBits)));
127  	return (ret);
128  }
129  
130  Fixed	Fixed::operator/(Fixed const& fixed) const
131  {
132  	Fixed	ret;
133  	int64	b1 = _rawBits;
134  	int64	b2 = fixed.getRawBits();
135  
136  	ret.setRawBits(static_cast<int>((b1 * (1 << _fractionalBits) / b2)));
137  	return (ret);
138  }
139  
140  Fixed&	Fixed::operator++(void)
141  {
142  	++_rawBits;
143  	return (*this);
144  }
145  
146  Fixed&	Fixed::operator--(void)
147  {
148  	--_rawBits;
149  	return (*this);
150  }
151  
152  Fixed const	Fixed::operator++(int)
153  {
154  	Fixed	temp(*this);
155  
156  	_rawBits++;
157  	return (temp);
158  }
159  
160  Fixed const	Fixed::operator--(int)
161  {
162  	Fixed	temp(*this);
163  
164  	_rawBits--;
165  	return (temp);
166  }
167  
168  Fixed&	Fixed::min(Fixed& a, Fixed& b)
169  {
170  	return ((a < b) ? a : b);
171  }
172  
173  Fixed const&	Fixed::min(Fixed const& a, Fixed const& b)
174  {
175  	return ((a < b) ? a : b);
176  }
177  
178  Fixed&	Fixed::max(Fixed& a, Fixed& b)
179  {
180  	return ((a > b) ? a : b);
181  }
182  
183  Fixed const&	Fixed::max(Fixed const& a, Fixed const& b)
184  {
185  	return ((a > b) ? a : b);
186  }