From 6f17e2e88891a0d28bcabe17776cf75bc65f24e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Wed, 14 May 2025 17:17:33 +0200 Subject: [PATCH] feat(ex02): it works --- ex02/.gitignore | 1 + ex02/Fixed.cpp | 137 ++++++++++++++++++++++++++++++++++++++++++++++++ ex02/Fixed.hpp | 51 ++++++++++++++++++ ex02/Makefile | 39 ++++++++++++++ ex02/main.cpp | 23 ++++++++ 5 files changed, 251 insertions(+) create mode 100644 ex02/.gitignore create mode 100644 ex02/Fixed.cpp create mode 100644 ex02/Fixed.hpp create mode 100644 ex02/Makefile create mode 100644 ex02/main.cpp diff --git a/ex02/.gitignore b/ex02/.gitignore new file mode 100644 index 0000000..03169ab --- /dev/null +++ b/ex02/.gitignore @@ -0,0 +1 @@ +ex02 diff --git a/ex02/Fixed.cpp b/ex02/Fixed.cpp new file mode 100644 index 0000000..c93a169 --- /dev/null +++ b/ex02/Fixed.cpp @@ -0,0 +1,137 @@ +#include "Fixed.hpp" +#include +#include + +const int Fixed::fracbits(8); + +Fixed::Fixed() { value = 0; } + +Fixed::Fixed(const int val) { value = val << fracbits; } + +Fixed::Fixed(const float val) { + const static int factor = 1 << fracbits; + float temp = (val * factor); + if (temp >= 0) + temp += 0.5f; + else + temp -= 0.5f; + value = (int)(temp); +} + +Fixed::Fixed(const Fixed &other) { this->value = other.value; } + +Fixed &Fixed::operator=(const Fixed &other) { + this->setRawBits(other.getRawBits()); + return *this; +} + +Fixed::~Fixed() {} + +int Fixed::getRawBits(void) const { return value; } + +void Fixed::setRawBits(int const raw) { value = raw; } + +float Fixed::toFloat(void) const { + const static int factor = 1 << fracbits; + float result = (float)value / factor; + return (result); +} + +int Fixed::toInt(void) const { + int result = (value >> fracbits); + return (result); +} + +std::string Fixed::toBin(void) const { + std::stringstream result; + result << std::bitset<32 - fracbits>(value >> fracbits); + result << "."; + result << std::bitset(value & (1 << fracbits) - 1); + return result.str(); +} + +bool Fixed::operator>(const Fixed &other) const { + return this->value > other.value; +} +bool Fixed::operator<(const Fixed &other) const { + return this->value < other.value; +} +bool Fixed::operator>=(const Fixed &other) const { + return this->value >= other.value; +} +bool Fixed::operator<=(const Fixed &other) const { + return this->value <= other.value; +} +bool Fixed::operator==(const Fixed &other) const { + return this->value == other.value; +} +bool Fixed::operator!=(const Fixed &other) const { + return this->value != other.value; +} + +const Fixed Fixed::operator+(const Fixed &other) const { + Fixed result = *this; + result.value += other.value; + return result; +} +const Fixed Fixed::operator-(const Fixed &other) const { + Fixed result = *this; + result.value -= other.value; + return result; +} +const Fixed Fixed::operator*(const Fixed &other) const { + Fixed result = *this; + result.value *= other.value; + result.value >>= fracbits; + return result; +} +const Fixed Fixed::operator/(const Fixed &other) const { + Fixed result = *this; + result.value /= other.value; + result.value <<= fracbits; + return result; +} +Fixed Fixed::operator++() { + this->value++; + return *this; +} +Fixed Fixed::operator--() { + this->value--; + return *this; +} +Fixed Fixed::operator++(int) { + Fixed result = *this; + operator++(); + return result; +} +Fixed Fixed::operator--(int) { + Fixed result = *this; + operator--(); + return result; +} + +Fixed &Fixed::min(Fixed &left, Fixed &right) { + if (left < right) + return left; + return right; +} +const Fixed &Fixed::min(const Fixed &left, const Fixed &right) { + if (left < right) + return left; + return right; +} +Fixed &Fixed::max(Fixed &left, Fixed &right) { + if (left > right) + return left; + return right; +} +const Fixed &Fixed::max(const Fixed &left, const Fixed &right) { + if (left > right) + return left; + return right; +} + +std::ostream &operator<<(std::ostream &stream, const Fixed &fixed) { + stream << fixed.toFloat(); + return stream; +} diff --git a/ex02/Fixed.hpp b/ex02/Fixed.hpp new file mode 100644 index 0000000..b931c50 --- /dev/null +++ b/ex02/Fixed.hpp @@ -0,0 +1,51 @@ +#ifndef FIXED_HPP +#define FIXED_HPP + +#include + +class Fixed { +public: + Fixed(); + Fixed(const int val); + Fixed(const float val); + Fixed(const Fixed &other); + Fixed &operator=(const Fixed &other); + ~Fixed(); + + int getRawBits(void) const; + void setRawBits(int const raw); + + float toFloat(void) const; + int toInt(void) const; + + std::string toBin(void) const; + + bool operator>(const Fixed &other) const; + bool operator<(const Fixed &other) const; + bool operator>=(const Fixed &other) const; + bool operator<=(const Fixed &other) const; + bool operator==(const Fixed &other) const; + bool operator!=(const Fixed &other) const; + + const Fixed operator+(const Fixed &other) const; + const Fixed operator-(const Fixed &other) const; + const Fixed operator*(const Fixed &other) const; + const Fixed operator/(const Fixed &other) const; + Fixed operator++(); + Fixed operator--(); + Fixed operator++(int); + Fixed operator--(int); + + static Fixed &min(Fixed &left, Fixed &right); + static const Fixed &min(const Fixed &left, const Fixed &right); + static Fixed &max(Fixed &left, Fixed &right); + static const Fixed &max(const Fixed &left, const Fixed &right); + + friend std::ostream &operator<<(std::ostream &stream, const Fixed &fixed); + +private: + int value; + static const int fracbits; +}; + +#endif diff --git a/ex02/Makefile b/ex02/Makefile new file mode 100644 index 0000000..13f64f0 --- /dev/null +++ b/ex02/Makefile @@ -0,0 +1,39 @@ +NAME = ex02 +ifeq ($(CPPFLAGS),) + CPPFLAGS = -Wall -Wextra -Werror -std=c++98 -g +endif +ifeq ($(CXX),) + CXX = c++ +endif +# g++ is the default on 42 computers +ifeq ($(CXX),g++) + CXX = c++ +endif +srcs = \ + Fixed.cpp \ + +main_objs = main.o $(srcs:.cpp=.o) +all_objs = $(main_objs) +deps = $(all_objs:.o=.d) + +all: $(NAME) + +-include $(deps) + +$(NAME): $(main_objs) + $(CXX) $(CPPFLAGS) -o $@ $^ + +%.o: %.cpp + $(CXX) -c $(CPPFLAGS) -o $*.o $*.cpp + $(CXX) -MM $(CPPFLAGS) -MT $*.o $*.cpp > $*.d + +clean: + find . -name '*.o' -print -delete + find . -name '*.d' -print -delete + +fclean: clean + rm -f $(NAME) + +re: + +make fclean + +make all diff --git a/ex02/main.cpp b/ex02/main.cpp new file mode 100644 index 0000000..c0f9e92 --- /dev/null +++ b/ex02/main.cpp @@ -0,0 +1,23 @@ +#include "Fixed.hpp" +#include + +int main(void) { + Fixed a; + Fixed fivedotohfive = Fixed(5.05f); + Fixed two = Fixed(2); + std::cout << fivedotohfive.toBin() << std::endl; + std::cout << two.toBin() << std::endl; + Fixed const b(fivedotohfive * two); + std::cout << a << std::endl; + std::cout << ++a << std::endl; + std::cout << a << std::endl; + std::cout << a++ << std::endl; + std::cout << a << std::endl; + std::cout << b << std::endl; + std::cout << Fixed::max(a, b) << std::endl; + std::cout << b.toBin() << std::endl; + Fixed const c(fivedotohfive / two); + std::cout << c << std::endl; + std::cout << c.toBin() << std::endl; + return 0; +}