diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c6f9a44 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index bab68c5..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "files.associations": { - "iosfwd": "cpp" - } -} \ No newline at end of file diff --git a/ex01/Makefile b/ex01/Makefile index 79bbc24..db4b860 100644 --- a/ex01/Makefile +++ b/ex01/Makefile @@ -1,6 +1,3 @@ -NAME={project_name} -SRC={source_files} -include ../common.mk # **************************************************************************** # # # # :::::::: # @@ -9,11 +6,12 @@ include ../common.mk # By: whaffman +#+ # # +#+ # # Created: 2025/03/24 15:14:58 by whaffman #+# #+# # -# Updated: 2025/03/24 15:15:17 by whaffman ######## odam.nl # +# Updated: 2025/03/25 10:13:11 by whaffman ######## odam.nl # # # # **************************************************************************** # -NAME= Fixed01 -SRC= Fixed.cpp main.cpp +NAME = Fixed01 +SRC = Fixed.cpp main.cpp -include ../common.mk + diff --git a/ex01/src/Fixed.cpp b/ex01/src/Fixed.cpp index 84376b9..0c71f51 100644 --- a/ex01/src/Fixed.cpp +++ b/ex01/src/Fixed.cpp @@ -6,12 +6,13 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/03/24 15:24:34 by whaffman #+# #+# */ -/* Updated: 2025/03/25 10:05:11 by whaffman ######## odam.nl */ +/* Updated: 2025/03/25 11:39:39 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ #include #include "Fixed.hpp" +#include Fixed::Fixed() { @@ -25,6 +26,18 @@ Fixed::Fixed(const Fixed &fixed) *this = fixed; } +Fixed::Fixed(const float value) +{ + std::cout << "Float constructor called" << std::endl; + fixedPointValue = (int)(value * (1 << fractionalBits)); +} + +Fixed::Fixed(const int value) +{ + std::cout << "Int constructor called" << std::endl; + fixedPointValue = value * (1 << fractionalBits); +} + Fixed::~Fixed() { std::cout << "Destructor called" << std::endl; @@ -32,13 +45,14 @@ Fixed::~Fixed() Fixed &Fixed::operator=(const Fixed &fixed) { std::cout << "Copy assignment operator called" << std::endl; - fixedPointValue = fixed.getRawBits(); + fixedPointValue = fixed.fixedPointValue; return *this; } std::ostream &operator<<(std::ostream &out, const Fixed &fixed) { out << fixed.toFloat(); + return (out); } int Fixed::getRawBits(void) const @@ -60,5 +74,5 @@ float Fixed::toFloat(void) const int Fixed::toInt(void) const { - return (int)fixedPointValue >> fractionalBits; + return (int) std::roundf( (float)fixedPointValue / (1 << fractionalBits)); } \ No newline at end of file diff --git a/ex01/src/main.cpp b/ex01/src/main.cpp index 5ada932..d1c7a94 100644 --- a/ex01/src/main.cpp +++ b/ex01/src/main.cpp @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/03/24 15:31:26 by whaffman #+# #+# */ -/* Updated: 2025/03/25 09:59:15 by whaffman ######## odam.nl */ +/* Updated: 2025/03/25 11:40:20 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -19,8 +19,8 @@ int main(void) Fixed const b(10); Fixed const c(42.42f); Fixed const d(b); - a = Fixed(1234.4321f); - + a = Fixed(1234.6321f); + std::cout << "a is " << a << std::endl; std::cout << "b is " << b << std::endl; std::cout << "c is " << c << std::endl; diff --git a/ex02/Makefile b/ex02/Makefile index 3f8d362..d1042e8 100644 --- a/ex02/Makefile +++ b/ex02/Makefile @@ -6,11 +6,11 @@ # By: whaffman +#+ # # +#+ # # Created: 2025/03/24 15:14:58 by whaffman #+# #+# # -# Updated: 2025/03/24 15:15:48 by whaffman ######## odam.nl # +# Updated: 2025/03/25 11:42:39 by whaffman ######## odam.nl # # # # **************************************************************************** # -NAME= -SRC= +NAME=Fixed02 +SRC=main.cpp Fixed.cpp -include ../common.mk diff --git a/ex02/inc/Fixed.hpp b/ex02/inc/Fixed.hpp new file mode 100644 index 0000000..4858fb1 --- /dev/null +++ b/ex02/inc/Fixed.hpp @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* Fixed.hpp :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/03/24 15:22:48 by whaffman #+# #+# */ +/* Updated: 2025/03/25 15:23:47 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +class Fixed { +public: + Fixed(); + Fixed(const int value); + Fixed(const float value); + Fixed(const Fixed &fixed); + ~Fixed(); + + Fixed &operator=(const Fixed &fixed); + Fixed operator+(const Fixed &fixed) const; + Fixed operator-(const Fixed &fixed) const; + Fixed operator*(const Fixed &fixed) const; + Fixed operator/(const Fixed &fixed) const; + + Fixed &operator++(); + Fixed operator++(int); + Fixed &operator--(); + Fixed operator--(int); + + bool operator>(const Fixed &fixed) const; + bool operator<(const Fixed &fixed) const; + bool operator>=(const Fixed &fixed) const; + bool operator<=(const Fixed &fixed) const; + bool operator==(const Fixed &fixed) const; + bool operator!=(const Fixed &fixed) const; + + static Fixed &min(Fixed &a, Fixed &b); + static const Fixed &min(const Fixed &a, const Fixed &b); + static Fixed &max(Fixed &a, Fixed &b); + static const Fixed &max(const Fixed &a, const Fixed &b); + + int getRawBits(void) const; + void setRawBits(int const raw); + float toFloat(void) const; + int toInt(void) const; + + +private: + int fixedPointValue; + static const int fractionalBits = 8; +}; + +std::ostream &operator<<(std::ostream &out, const Fixed &fixed); + + diff --git a/ex02/src/Fixed.cpp b/ex02/src/Fixed.cpp new file mode 100644 index 0000000..1a4e775 --- /dev/null +++ b/ex02/src/Fixed.cpp @@ -0,0 +1,179 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* Fixed.cpp :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/03/24 15:24:34 by whaffman #+# #+# */ +/* Updated: 2025/03/25 15:28:16 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include +#include "Fixed.hpp" +#include + +Fixed::Fixed() +{ + fixedPointValue = 0; + std::cout << "Default constructor called" << std::endl; +} + +Fixed::Fixed(const Fixed &fixed) +{ + std::cout << "Copy constructor called" << std::endl; + *this = fixed; +} + +Fixed::Fixed(const float value) +{ + std::cout << "Float constructor called" << std::endl; + fixedPointValue = (int)(value * (1 << fractionalBits)); +} + +Fixed::Fixed(const int value) +{ + std::cout << "Int constructor called" << std::endl; + fixedPointValue = value * (1 << fractionalBits); +} + +Fixed::~Fixed() +{ + std::cout << "Destructor called" << std::endl; +} +Fixed &Fixed::operator=(const Fixed &fixed) +{ + std::cout << "Copy assignment operator called" << std::endl; + fixedPointValue = fixed.fixedPointValue; + return *this; +} + +std::ostream &operator<<(std::ostream &out, const Fixed &fixed) +{ + out << fixed.toFloat(); + return (out); +} + + +Fixed Fixed::operator+(const Fixed &fixed) const +{ + return (Fixed(toFloat() + fixed.toFloat())); +} + +Fixed Fixed::operator-(const Fixed &fixed) const +{ + return (Fixed(toFloat() - fixed.toFloat())); +} + +Fixed Fixed::operator*(const Fixed &fixed) const +{ + return (Fixed(toFloat() * fixed.toFloat())); +} + +Fixed Fixed::operator/(const Fixed &fixed) const +{ + return (Fixed(toFloat() / fixed.toFloat())); +} + + +Fixed &Fixed::operator++() +{ + fixedPointValue++; + return *this; +} + +Fixed Fixed::operator++(int) +{ + fixedPointValue++; + return Fixed(*this); +} + +Fixed &Fixed::operator--() +{ + fixedPointValue--; + return *this; +} + +Fixed Fixed::operator--(int) +{ + fixedPointValue--; + return Fixed(*this); +} + +bool Fixed::operator>(const Fixed &fixed) const +{ + return (fixedPointValue > fixed.fixedPointValue); +} + +bool Fixed::operator<(const Fixed &fixed) const +{ + return (fixedPointValue < fixed.fixedPointValue); +} + +bool Fixed::operator>=(const Fixed &fixed) const +{ + return (fixedPointValue >= fixed.fixedPointValue); +} + +bool Fixed::operator<=(const Fixed &fixed) const +{ + return (fixedPointValue <= fixed.fixedPointValue); +} + +bool Fixed::operator==(const Fixed &fixed) const +{ + return (fixedPointValue == fixed.fixedPointValue); +} + +bool Fixed::operator!=(const Fixed &fixed) const +{ + return (fixedPointValue != fixed.fixedPointValue); +} + +int Fixed::getRawBits(void) const +{ + std::cout << "getRawBits member function called" << std::endl; + return fixedPointValue; +} + +void Fixed::setRawBits(int const raw) +{ + std::cout << "setRawBits member function called" << std::endl; + fixedPointValue = raw; +} + +float Fixed::toFloat(void) const +{ + return (float)fixedPointValue / (1 << fractionalBits); +} + +int Fixed::toInt(void) const +{ + return (int) std::roundf( (float)fixedPointValue / (1 << fractionalBits)); +} + +Fixed &Fixed::min(Fixed &a, Fixed &b) +{ + if (a < b) + return a; + return b; +} +const Fixed &Fixed::min(const Fixed &a, const Fixed &b) +{ + if (a < b) + return a; + return b; +} +Fixed &Fixed::max(Fixed &a, Fixed &b) +{ + if (a > b) + return a; + return b; +} +const Fixed &Fixed::max(const Fixed &a, const Fixed &b) +{ + if (a > b) + return a; + return b; +} \ No newline at end of file diff --git a/ex02/src/main.cpp b/ex02/src/main.cpp new file mode 100644 index 0000000..2240636 --- /dev/null +++ b/ex02/src/main.cpp @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* main.cpp :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/03/24 15:31:26 by whaffman #+# #+# */ +/* Updated: 2025/03/25 15:26:00 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include +#include "Fixed.hpp" + +int main(void) +{ + // Fixed a; + // Fixed const b(10); + // Fixed const c(42.42f); + // Fixed const d(b); + // a = Fixed(1234.6321f); + + // std::cout << "a is " << a << std::endl; + // std::cout << "b is " << b << std::endl; + // std::cout << "c is " << c << std::endl; + // std::cout << "d is " << d << std::endl; + + // std::cout << "a is " << a.toInt() << " as integer" << std::endl; + // std::cout << "b is " << b.toInt() << " as integer" << std::endl; + // std::cout << "c is " << c.toInt() << " as integer" << std::endl; + // std::cout << "d is " << d.toInt() << " as integer" << std::endl; + + Fixed a; + Fixed const b(Fixed(5.05f) * Fixed(2)); + 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; + + return 0; +} \ No newline at end of file