ajout max for fixed point range
This commit is contained in:
@@ -1,5 +1,9 @@
|
|||||||
#include "Fixed.hpp"
|
#include "Fixed.hpp"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* function to print integers in binary
|
||||||
|
*/
|
||||||
|
|
||||||
void printBits(std::string before, unsigned int num)
|
void printBits(std::string before, unsigned int num)
|
||||||
{
|
{
|
||||||
int i = 0;
|
int i = 0;
|
||||||
@@ -12,51 +16,89 @@ void printBits(std::string before, unsigned int num)
|
|||||||
if (i % 8 == 0)
|
if (i % 8 == 0)
|
||||||
std::cout << ' ';
|
std::cout << ' ';
|
||||||
}
|
}
|
||||||
std::cout << "(" << num << ")" << '\n';
|
std::cout << "(" << (signed int)num << ")" << '\n';
|
||||||
}
|
}
|
||||||
|
|
||||||
// default/parametric constructor
|
/*
|
||||||
Fixed::Fixed( void ) : _value( 0 ) {
|
* statics variables initialisation
|
||||||
|
*
|
||||||
|
* for MAX integer :
|
||||||
|
* 00000000 01111111 11111111 11111111 ( 8388607) (-1U >> (this->_frac +1))
|
||||||
|
* <= ... >=
|
||||||
|
* 11111111 10000000 00000000 00000000 (-8388608)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
int const Fixed::_frac = 8;
|
||||||
|
int const Fixed::_max = -1U >> (_frac +1);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* default constructor / copy constructor / destructor
|
||||||
|
*/
|
||||||
|
|
||||||
|
Fixed::Fixed() : _value(0) {
|
||||||
std::cout << "Default constructor called" << '\n';
|
std::cout << "Default constructor called" << '\n';
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// copy constructor
|
|
||||||
Fixed::Fixed(Fixed const & src) {
|
Fixed::Fixed(Fixed const & src) {
|
||||||
std::cout << "Copy constructor called" << '\n';
|
std::cout << "Copy constructor called" << '\n';
|
||||||
*this = src;
|
*this = src;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// destructor
|
|
||||||
Fixed::~Fixed( void ) {
|
Fixed::~Fixed( void ) {
|
||||||
std::cout << "Destructor called" << '\n';
|
std::cout << "Destructor called" << '\n';
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Fixed::Fixed( int integer ) {
|
/*
|
||||||
|
* int and float constructors
|
||||||
|
*/
|
||||||
|
|
||||||
std::cout << integer << '\n';
|
Fixed::Fixed(int integer) {
|
||||||
|
std::cout << "Int constructor called" << '\n';
|
||||||
|
|
||||||
|
if (integer < ~this->_max || integer > this->_max)
|
||||||
|
{
|
||||||
|
std::cout << "error: integer out of range" << '\n';
|
||||||
|
return;
|
||||||
|
}
|
||||||
printBits("integer : ", integer);
|
printBits("integer : ", integer);
|
||||||
this->_value = integer << this->_frac;
|
this->_value = integer << this->_frac;
|
||||||
printBits("this->_value : ", this->_value);
|
printBits("integer : ", this->_value);
|
||||||
|
}
|
||||||
|
|
||||||
|
Fixed::Fixed(float const floater) {
|
||||||
|
std::cout << "Float constructor called" << '\n';
|
||||||
|
|
||||||
|
if (floater < ~this->_max || floater > this->_max)
|
||||||
|
{
|
||||||
|
std::cout << "error: float out of range" << '\n';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// assignement operator
|
/*
|
||||||
Fixed & Fixed::operator=( Fixed const & rhs ) {
|
* assignement operator
|
||||||
|
*/
|
||||||
|
|
||||||
|
Fixed & Fixed::operator=( Fixed const & rhs ) {
|
||||||
std::cout << "Copy assignment operator called" << '\n';
|
std::cout << "Copy assignment operator called" << '\n';
|
||||||
if ( this != &rhs )
|
if ( this != &rhs )
|
||||||
this->_value = rhs.getRawBits();
|
this->_value = rhs.getRawBits();
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* other functions
|
||||||
|
*/
|
||||||
|
|
||||||
int Fixed::getRawBits( void ) const {
|
int Fixed::getRawBits( void ) const {
|
||||||
std::cout << "getRawBits member function called" << '\n';
|
std::cout << "getRawBits member function called" << '\n';
|
||||||
|
|
||||||
return this->_value;
|
return this->_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,3 +106,7 @@ void Fixed::setRawBits( int const raw ) {
|
|||||||
this->_value = raw;
|
this->_value = raw;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Fixed::toFloat( void ) const {}
|
||||||
|
int Fixed::toInt( void ) const {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|||||||
@@ -18,11 +18,14 @@ public:
|
|||||||
|
|
||||||
int getRawBits(void) const;
|
int getRawBits(void) const;
|
||||||
void setRawBits(int const raw);
|
void setRawBits(int const raw);
|
||||||
|
void toFloat(void) const;
|
||||||
|
int toInt(void) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
int _value;
|
int _value;
|
||||||
static int const _frac = 8;
|
static int const _frac;
|
||||||
|
static int const _max;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
BIN
d02/ex01/fixed
BIN
d02/ex01/fixed
Binary file not shown.
@@ -4,16 +4,7 @@
|
|||||||
int main( void ) {
|
int main( void ) {
|
||||||
|
|
||||||
Fixed a( 1 );
|
Fixed a( 1 );
|
||||||
std::cout << a.getRawBits() << '\n';
|
Fixed const b( 1.5 );
|
||||||
|
|
||||||
Fixed b( 6 );
|
|
||||||
std::cout << b.getRawBits() << '\n';
|
|
||||||
|
|
||||||
Fixed c( -6 );
|
|
||||||
std::cout << c.getRawBits() << '\n';
|
|
||||||
|
|
||||||
Fixed d( -1 );
|
|
||||||
std::cout << d.getRawBits() << '\n';
|
|
||||||
|
|
||||||
// Fixed a;
|
// Fixed a;
|
||||||
// Fixed const b( 10 );
|
// Fixed const b( 10 );
|
||||||
|
|||||||
Reference in New Issue
Block a user