CPP02 1.0
読み取り中…
検索中…
一致する文字列を見つけられません
Fixed.cpp
[詳解]
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* Fixed.cpp :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: kamitsui <kamitsui@student.42tokyo.jp> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2025/04/26 22:12:49 by kamitsui #+# #+# */
9/* Updated: 2025/05/06 23:23:10 by kamitsui ### ########.fr */
10/* */
11/* ************************************************************************** */
12
24#include "Fixed.hpp"
25#include <cmath>
26#include <iostream>
27
28const int Fixed::fractionalBits; // Definition of the static member
29
30Fixed::Fixed() : fixedPointValue(0) { std::cout << "Default constructor called" << std::endl; }
31
39Fixed::Fixed(const int raw) : fixedPointValue(raw << fractionalBits) {
40 std::cout << "Int constructor called" << std::endl;
41}
51Fixed::Fixed(const float raw) : fixedPointValue(static_cast<int>(roundf(raw * (1 << fractionalBits)))) {
52 std::cout << "Float constructor called" << std::endl;
53}
56Fixed::Fixed(const Fixed &other) : fixedPointValue(other.fixedPointValue) {
57 std::cout << "Copy constructor called" << std::endl;
58}
59
68Fixed &Fixed::operator=(const Fixed &other) {
69 std::cout << "Copy assignment operator called" << std::endl;
70 if (this != &other) {
71 this->fixedPointValue = other.fixedPointValue;
72 }
73 return *this;
74}
77Fixed::~Fixed() { std::cout << "Destructor called" << std::endl; }
78
79int Fixed::getRawBits(void) const {
80 std::cout << "getRawBits member function called" << std::endl;
81 return this->fixedPointValue;
82}
83
84void Fixed::setRawBits(int const raw) { this->fixedPointValue = raw; }
85
92float Fixed::toFloat(void) const { return static_cast<float>(this->fixedPointValue) / (1 << fractionalBits); }
101int Fixed::toInt(void) const { return this->fixedPointValue >> fractionalBits; }
114 o << fixed.toFloat();
115 return o;
116}
basic_ostream< _CharT, _Traits > & endl(basic_ostream< _CharT, _Traits > &__os)
ios_base & fixed(ios_base &__base)
ostream cout
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
int getRawBits(void) const
Gets the raw value of the fixed-point number.
Definition Fixed.cpp:73
void setRawBits(int const raw)
Sets the raw value of the fixed-point number.
Definition Fixed.cpp:84
Fixed & operator=(const Fixed &other)
Copy assignment operator.
Definition Fixed.cpp:52
Fixed()
Default constructor.
Definition Fixed.cpp:33
~Fixed()
Destructor.
Definition Fixed.cpp:65
float toFloat(void) const
Converts the fixed-point value to a floating-point value.
Definition Fixed.cpp:92
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...