Compare commits
7 Commits
1d752c591b
...
master
Author | SHA1 | Date | |
---|---|---|---|
ffc3d04ca3 | |||
18a2008a96 | |||
13f208d577 | |||
fe32ade285 | |||
1c500e28bf | |||
4d3528ed3f | |||
7de4817171 |
@ -1,3 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
class Fixed
|
||||
{
|
||||
private:
|
||||
|
@ -22,13 +22,13 @@ Fixed::Fixed(const Fixed& src)
|
||||
|
||||
Fixed::Fixed(const int& src)
|
||||
{
|
||||
std::cout << "Copy constructor called" << std::endl;
|
||||
std::cout << "Int constructor called" << std::endl;
|
||||
this->_value = src << this->_nb_bits;
|
||||
}
|
||||
|
||||
Fixed::Fixed(const float& src)
|
||||
{
|
||||
std::cout << "Copy constructor called" << std::endl;
|
||||
std::cout << "Float constructor called" << std::endl;
|
||||
this->_value = (int) roundf(src * (1 << this->_nb_bits));
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,6 @@
|
||||
#ifndef FIXED_H
|
||||
# define FIXED_H
|
||||
#pragma once
|
||||
|
||||
# include <ostream>
|
||||
#include <ostream>
|
||||
|
||||
class Fixed
|
||||
{
|
||||
@ -25,5 +24,3 @@ class Fixed
|
||||
};
|
||||
|
||||
std::ostream& operator << (std::ostream &out, const Fixed& fixed);
|
||||
|
||||
#endif
|
||||
|
@ -10,7 +10,7 @@ Fixed::~Fixed()
|
||||
|
||||
Fixed::Fixed()
|
||||
{
|
||||
ths->_value = 0;
|
||||
this->_value = 0;
|
||||
std::cout << "Default constructor called" << std::endl;
|
||||
}
|
||||
|
||||
@ -22,13 +22,13 @@ Fixed::Fixed(const Fixed& src)
|
||||
|
||||
Fixed::Fixed(const int& src)
|
||||
{
|
||||
std::cout << "Copy constructor called" << std::endl;
|
||||
std::cout << "Int constructor called" << std::endl;
|
||||
this->_value = src << this->_nb_bits;
|
||||
}
|
||||
|
||||
Fixed::Fixed(const float& src)
|
||||
{
|
||||
std::cout << "Copy constructor called" << std::endl;
|
||||
std::cout << "Float constructor called" << std::endl;
|
||||
this->_value = (int) roundf(src * (1 << this->_nb_bits));
|
||||
}
|
||||
|
||||
@ -93,52 +93,52 @@ Fixed Fixed::operator--(int)
|
||||
return (tmp);
|
||||
}
|
||||
|
||||
bool Fixed::operator==(const Fixed& src)
|
||||
bool Fixed::operator==(const Fixed& src) const
|
||||
{
|
||||
return (this->_value == src._value);
|
||||
}
|
||||
|
||||
bool Fixed::operator!=(const Fixed& src)
|
||||
bool Fixed::operator!=(const Fixed& src) const
|
||||
{
|
||||
return (this->_value != src._value);
|
||||
}
|
||||
|
||||
bool Fixed::operator>=(const Fixed& src)
|
||||
bool Fixed::operator>=(const Fixed& src) const
|
||||
{
|
||||
return (this->_value >= src._value);
|
||||
}
|
||||
|
||||
bool Fixed::operator<=(const Fixed& src)
|
||||
bool Fixed::operator<=(const Fixed& src) const
|
||||
{
|
||||
return (this->_value <= src._value);
|
||||
}
|
||||
|
||||
bool Fixed::operator>(const Fixed& src)
|
||||
bool Fixed::operator>(const Fixed& src) const
|
||||
{
|
||||
return (this->_value > src._value);
|
||||
}
|
||||
|
||||
bool Fixed::operator<(const Fixed& src)
|
||||
bool Fixed::operator<(const Fixed& src) const
|
||||
{
|
||||
return (this->_value < src._value);
|
||||
}
|
||||
|
||||
Fixed Fixed::operator*(const Fixed &src)
|
||||
Fixed Fixed::operator*(const Fixed &src) const
|
||||
{
|
||||
return Fixed(this->toFloat() * src.toFloat());
|
||||
}
|
||||
|
||||
Fixed Fixed::operator/(const Fixed &src)
|
||||
Fixed Fixed::operator/(const Fixed &src) const
|
||||
{
|
||||
return Fixed(this->toFloat() / src.toFloat());
|
||||
}
|
||||
|
||||
Fixed Fixed::operator+(const Fixed &src)
|
||||
Fixed Fixed::operator+(const Fixed &src) const
|
||||
{
|
||||
return Fixed(this->toFloat() + src.toFloat());
|
||||
}
|
||||
|
||||
Fixed Fixed::operator-(const Fixed &src)
|
||||
Fixed Fixed::operator-(const Fixed &src) const
|
||||
{
|
||||
return Fixed(this->toFloat() - src.toFloat());
|
||||
}
|
||||
@ -159,14 +159,14 @@ Fixed& Fixed::max(Fixed& f1, Fixed &f2)
|
||||
|
||||
const Fixed& Fixed::min(const Fixed& f1, const Fixed &f2)
|
||||
{
|
||||
if (f1._value > f2._value)
|
||||
if (f1._value < f2._value)
|
||||
return (f1);
|
||||
return (f2);
|
||||
}
|
||||
|
||||
Fixed& Fixed::min(Fixed& f1, Fixed &f2)
|
||||
{
|
||||
if (f1._value > f2._value)
|
||||
if (f1._value < f2._value)
|
||||
return (f1);
|
||||
return (f2);
|
||||
}
|
||||
|
@ -1,5 +1,4 @@
|
||||
#ifndef FIXED_H
|
||||
# define FIXED_H
|
||||
#pragma once
|
||||
|
||||
# include <ostream>
|
||||
|
||||
@ -18,17 +17,17 @@ class Fixed
|
||||
|
||||
Fixed &operator=(const Fixed& src);
|
||||
|
||||
bool operator<(const Fixed& src);
|
||||
bool operator>(const Fixed& src);
|
||||
bool operator>=(const Fixed& src);
|
||||
bool operator<=(const Fixed& src);
|
||||
bool operator==(const Fixed& src);
|
||||
bool operator!=(const Fixed& src);
|
||||
bool operator<(const Fixed& src) const;
|
||||
bool operator>(const Fixed& src) const;
|
||||
bool operator>=(const Fixed& src) const;
|
||||
bool operator<=(const Fixed& src) const;
|
||||
bool operator==(const Fixed& src) const;
|
||||
bool operator!=(const Fixed& src) const;
|
||||
|
||||
Fixed operator-(const Fixed& src);
|
||||
Fixed operator+(const Fixed& src);
|
||||
Fixed operator*(const Fixed& src);
|
||||
Fixed operator/(const Fixed& src);
|
||||
Fixed operator-(const Fixed& src) const;
|
||||
Fixed operator+(const Fixed& src) const;
|
||||
Fixed operator*(const Fixed& src) const;
|
||||
Fixed operator/(const Fixed& src) const;
|
||||
|
||||
|
||||
Fixed &operator++();
|
||||
@ -47,5 +46,3 @@ class Fixed
|
||||
};
|
||||
|
||||
std::ostream& operator << (std::ostream &out, const Fixed& fixed);
|
||||
|
||||
#endif
|
||||
|
@ -2,14 +2,53 @@
|
||||
#include <iostream>
|
||||
|
||||
int main( void ) {
|
||||
Fixed a(0);
|
||||
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;
|
||||
{
|
||||
Fixed a(0);
|
||||
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;
|
||||
}
|
||||
{
|
||||
const Fixed a(2.5f);
|
||||
const Fixed b(1.3f);
|
||||
|
||||
std::cout << "b: " << b << std::endl;
|
||||
std::cout << "a: " << a << std::endl;
|
||||
|
||||
Fixed sum = a + b;
|
||||
std::cout << "Addition: " << sum << std::endl;
|
||||
|
||||
Fixed difference = a - b;
|
||||
std::cout << "Subtraction: " << difference << std::endl;
|
||||
|
||||
Fixed product = a * b;
|
||||
std::cout << "Multiplication: " << product << std::endl;
|
||||
|
||||
Fixed quotient = a / b;
|
||||
std::cout << "Division: " << quotient << std::endl;
|
||||
|
||||
bool isEqual = (a == b);
|
||||
std::cout << "Is a equal to b? " << std::boolalpha << isEqual << std::endl;
|
||||
|
||||
bool isNotEqual = (a != b);
|
||||
std::cout << "Is a not equal to b? " << std::boolalpha << isNotEqual << std::endl;
|
||||
|
||||
bool isLessThan = (a < b);
|
||||
std::cout << "Is a less than b? " << std::boolalpha << isLessThan << std::endl;
|
||||
|
||||
bool isGreaterThan = (a > b);
|
||||
std::cout << "Is a greater than b? " << std::boolalpha << isGreaterThan << std::endl;
|
||||
|
||||
bool isLessThanOrEqual = (a <= b);
|
||||
std::cout << "Is a less than or equal to b? " << std::boolalpha << isLessThanOrEqual << std::endl;
|
||||
|
||||
bool isGreaterThanOrEqual = (a >= b);
|
||||
std::cout << "Is a greater than or equal to b? " << std::boolalpha << isGreaterThanOrEqual << std::endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user