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:17:36 by kamitsui #+# #+# */
9/* Updated: 2025/05/06 17:10:39 by kamitsui ### ########.fr */
10/* */
11/* ************************************************************************** */
12
25#include "Fixed.hpp"
26#include <cmath>
27#include <iostream>
28#include <limits>
29
30const int Fixed::fractionalBits; // Definition of the static member
31
32Fixed::Fixed() : fixedPointValue(0) {}
33
34Fixed::Fixed(const int raw) : fixedPointValue(raw << fractionalBits) {}
35
36Fixed::Fixed(const float raw) : fixedPointValue(static_cast<int>(roundf(raw * (1 << fractionalBits)))) {}
37
38Fixed::Fixed(const Fixed &other) : fixedPointValue(other.fixedPointValue) {}
39
40Fixed &Fixed::operator=(const Fixed &other) {
41 if (this != &other) {
42 this->fixedPointValue = other.fixedPointValue;
43 }
44 return *this;
45}
46
48
49int Fixed::getRawBits(void) const { return this->fixedPointValue; }
50
51void Fixed::setRawBits(int const raw) { this->fixedPointValue = raw; }
52
53float Fixed::toFloat(void) const { return static_cast<float>(this->fixedPointValue) / (1 << fractionalBits); }
54
55int Fixed::toInt(void) const { return this->fixedPointValue >> fractionalBits; }
56
58 o << fixed.toFloat();
59 return o;
60}
61
62// Comparison operators
63bool Fixed::operator>(const Fixed &other) const { return this->fixedPointValue > other.fixedPointValue; }
64
65bool Fixed::operator<(const Fixed &other) const { return this->fixedPointValue < other.fixedPointValue; }
66
67bool Fixed::operator>=(const Fixed &other) const { return this->fixedPointValue >= other.fixedPointValue; }
68
69bool Fixed::operator<=(const Fixed &other) const { return this->fixedPointValue <= other.fixedPointValue; }
70
71bool Fixed::operator==(const Fixed &other) const { return this->fixedPointValue == other.fixedPointValue; }
72
73bool Fixed::operator!=(const Fixed &other) const { return this->fixedPointValue != other.fixedPointValue; }
74
75// Arithmetic operators
76Fixed Fixed::operator+(const Fixed &other) const { return Fixed(this->toFloat() + other.toFloat()); }
77
78Fixed Fixed::operator-(const Fixed &other) const { return Fixed(this->toFloat() - other.toFloat()); }
79
80Fixed Fixed::operator*(const Fixed &other) const { return Fixed(this->toFloat() * other.toFloat()); }
81
82Fixed Fixed::operator/(const Fixed &other) const {
83 if (other.fixedPointValue == 0) {
84 std::cerr << "Division by zero is undefined. Returning maximum float." << std::endl;
86 }
87 return Fixed(this->toFloat() / other.toFloat());
88}
89
90// Increment/decrement operators
91Fixed &Fixed::operator++() { // Pre-increment
92 this->fixedPointValue += (1 << fractionalBits);
93 return *this;
94}
95
96Fixed Fixed::operator++(int) { // Post-increment
97 Fixed temp = *this;
98 this->fixedPointValue += (1 << fractionalBits);
99 return temp;
100}
101
102Fixed &Fixed::operator--() { // Pre-decrement
103 this->fixedPointValue -= (1 << fractionalBits);
104 return *this;
105}
106
107Fixed Fixed::operator--(int) { // Post-decrement
108 Fixed temp = *this;
109 this->fixedPointValue -= (1 << fractionalBits);
110 return temp;
111}
112
113// Min/Max functions
114Fixed &Fixed::min(Fixed &a, Fixed &b) { return (a < b) ? a : b; }
115
116const Fixed &Fixed::min(const Fixed &a, const Fixed &b) { return (a < b) ? a : b; }
117
118Fixed &Fixed::max(Fixed &a, Fixed &b) { return (a > b) ? a : b; }
119
120const 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...