CPP02 1.0
読み取り中…
検索中…
一致する文字列を見つけられません
Fixed.cpp
[詳解]
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* Fixed.cpp :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: kamitsui <kamitsui@student.42tokyo.jp> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2025/04/28 17:12:22 by kamitsui #+# #+# */
9/* Updated: 2025/05/06 15:39:15 by kamitsui ### ########.fr */
10/* */
11/* ************************************************************************** */
12
25#include "Fixed.hpp"
26#include <cmath>
27#include <iostream>
28#include <limits> // Required for std::numeric_limits
29
30const int Fixed::fractionalBits; // Definition of the static member
31
32Fixed::Fixed() : fixedPointValue(0) {}
33// std::cout << "Default constructor called" << std::endl; // Removed for cleaner output
34
35Fixed::Fixed(const int raw) : fixedPointValue(raw << fractionalBits) {}
36// std::cout << "Int constructor called" << std::endl; // Removed for cleaner output
37
38Fixed::Fixed(const float raw) : fixedPointValue(static_cast<int>(roundf(raw * (1 << fractionalBits)))) {}
39// std::cout << "Float constructor called" << std::endl; // Removed for cleaner output
40
41Fixed::Fixed(const Fixed &other) : fixedPointValue(other.fixedPointValue) {}
42// std::cout << "Copy constructor called" << std::endl; // Removed for cleaner output
43
44Fixed &Fixed::operator=(const Fixed &other) {
45 // std::cout << "Copy assignment operator called" << std::endl; // Removed for cleaner output
46 if (this != &other) {
47 this->fixedPointValue = other.fixedPointValue;
48 }
49 return *this;
50}
51
53// std::cout << "Destructor called" << std::endl; // Removed for cleaner output
54
55int Fixed::getRawBits(void) const { return this->fixedPointValue; }
56// std::cout << "getRawBits member function called" << std::endl; // Removed for cleaner output
57
58void Fixed::setRawBits(int const raw) { this->fixedPointValue = raw; }
59
60float Fixed::toFloat(void) const { return static_cast<float>(this->fixedPointValue) / (1 << fractionalBits); }
61
62int Fixed::toInt(void) const { return this->fixedPointValue >> fractionalBits; }
63
65 o << fixed.toFloat();
66 return o;
67}
68
69// Comparison operators
70
76bool Fixed::operator>(const Fixed &other) const { return this->fixedPointValue > other.fixedPointValue; }
77
83bool Fixed::operator<(const Fixed &other) const { return this->fixedPointValue < other.fixedPointValue; }
84
90bool Fixed::operator>=(const Fixed &other) const { return this->fixedPointValue >= other.fixedPointValue; }
91
97bool Fixed::operator<=(const Fixed &other) const { return this->fixedPointValue <= other.fixedPointValue; }
98
104bool Fixed::operator==(const Fixed &other) const { return this->fixedPointValue == other.fixedPointValue; }
105
111bool Fixed::operator!=(const Fixed &other) const { return this->fixedPointValue != other.fixedPointValue; }
112
113// Arithmetic operators
114
120Fixed Fixed::operator+(const Fixed &other) const { return Fixed(this->toFloat() + other.toFloat()); }
121
127Fixed Fixed::operator-(const Fixed &other) const { return Fixed(this->toFloat() - other.toFloat()); }
128
134Fixed Fixed::operator*(const Fixed &other) const { return Fixed(this->toFloat() * other.toFloat()); }
135
141Fixed Fixed::operator/(const Fixed &other) const {
142 if (other.fixedPointValue == 0) {
143 std::cerr << "Division by zero is undefined. Returning maximum float." << std::endl;
144 return Fixed(std::numeric_limits<float>::max()); // Return max float value.
145 }
146 return Fixed(this->toFloat() / other.toFloat());
147}
148
149// Increment/decrement operators
150
155Fixed &Fixed::operator++() { // Pre-increment
156 this->fixedPointValue += (1 << fractionalBits); // Add smallest representable value (1.0)
157 return *this;
158}
159
165Fixed Fixed::operator++(int) { // Post-increment
166 Fixed temp = *this;
167 this->fixedPointValue += (1 << fractionalBits);
168 return temp;
169}
170
175Fixed &Fixed::operator--() { // Pre-decrement
176 this->fixedPointValue -= (1 << fractionalBits);
177 return *this;
178}
179
185Fixed Fixed::operator--(int) { // Post-decrement
186 Fixed temp = *this;
187 this->fixedPointValue -= (1 << fractionalBits);
188 return temp;
189}
190
191// Min/Max functions
192
199Fixed &Fixed::min(Fixed &a, Fixed &b) { return (a < b) ? a : b; }
200
207const Fixed &Fixed::min(const Fixed &a, const Fixed &b) { return (a < b) ? a : b; }
208
215Fixed &Fixed::max(Fixed &a, Fixed &b) { return (a > b) ? a : b; }
216
223const Fixed &Fixed::max(const Fixed &a, const Fixed &b) { return (a > b) ? a : b; }
basic_ostream< _CharT, _Traits > & endl(basic_ostream< _CharT, _Traits > &__os)
ostream cerr
ios_base & fixed(ios_base &__base)
ex00 Fixed Class
Definition Fixed.hpp:28
int toInt(void) const
Converts the fixed-point value to an integer value (truncates the fractional part).
Definition Fixed.cpp:101
bool operator>=(const Fixed &other) const
Overloads the greater-than-or-equal-to operator.
Definition Fixed.cpp:90
int getRawBits(void) const
Gets the raw value of the fixed-point number.
Definition Fixed.cpp:73
bool operator<(const Fixed &other) const
Overloads the less-than operator.
Definition Fixed.cpp:83
void setRawBits(int const raw)
Sets the raw value of the fixed-point number.
Definition Fixed.cpp:84
Fixed & operator--()
Overloads the pre-decrement operator.
Definition Fixed.cpp:175
static Fixed & max(Fixed &a, Fixed &b)
Static member function to find the maximum of two Fixed objects (non-const).
Definition Fixed.cpp:215
Fixed operator/(const Fixed &other) const
Overloads the division operator.
Definition Fixed.cpp:141
bool operator<=(const Fixed &other) const
Overloads the less-than-or-equal-to operator.
Definition Fixed.cpp:97
bool operator>(const Fixed &other) const
Overloads the greater-than operator.
Definition Fixed.cpp:76
bool operator==(const Fixed &other) const
Overloads the equality operator.
Definition Fixed.cpp:104
Fixed & operator=(const Fixed &other)
Copy assignment operator.
Definition Fixed.cpp:52
Fixed operator-(const Fixed &other) const
Overloads the subtraction operator.
Definition Fixed.cpp:127
Fixed operator*(const Fixed &other) const
Overloads the multiplication operator.
Definition Fixed.cpp:134
Fixed()
Default constructor.
Definition Fixed.cpp:33
Fixed & operator++()
Overloads the pre-increment operator.
Definition Fixed.cpp:155
static Fixed & min(Fixed &a, Fixed &b)
Static member function to find the minimum of two Fixed objects (non-const).
Definition Fixed.cpp:199
~Fixed()
Destructor.
Definition Fixed.cpp:65
Fixed operator+(const Fixed &other) const
Overloads the addition operator.
Definition Fixed.cpp:120
float toFloat(void) const
Converts the fixed-point value to a floating-point value.
Definition Fixed.cpp:92
bool operator!=(const Fixed &other) const
Overloads the inequality operator.
Definition Fixed.cpp:111
std::ostream & operator<<(std::ostream &o, const Fixed &fixed)
Overloads the output stream operator to insert the float representation of the fixed-point number int...
Definition Fixed.cpp:113
Header file for the Fixed class representing fixed-point numbers with overloaded operators and min/ma...