113 lines
2.0 KiB
C++
113 lines
2.0 KiB
C++
#include "Fixed.hpp"
|
|
|
|
/*
|
|
* function to print integers in binary
|
|
*/
|
|
|
|
void printBits(std::string before, unsigned int num)
|
|
{
|
|
int i = 0;
|
|
|
|
std::cout << before;
|
|
for (unsigned int mask = 1U << 31; mask; mask = mask >> 1)
|
|
{
|
|
std::cout << ((num & mask) != 0);
|
|
i++;
|
|
if (i % 8 == 0)
|
|
std::cout << ' ';
|
|
}
|
|
std::cout << "(" << (signed int)num << ")" << '\n';
|
|
}
|
|
|
|
/*
|
|
* 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';
|
|
return;
|
|
}
|
|
|
|
Fixed::Fixed(Fixed const & src) {
|
|
std::cout << "Copy constructor called" << '\n';
|
|
*this = src;
|
|
return;
|
|
}
|
|
|
|
Fixed::~Fixed( void ) {
|
|
std::cout << "Destructor called" << '\n';
|
|
return;
|
|
}
|
|
|
|
/*
|
|
* int and float constructors
|
|
*/
|
|
|
|
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);
|
|
this->_value = integer << this->_frac;
|
|
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 ) {
|
|
std::cout << "Copy assignment operator called" << '\n';
|
|
if ( this != &rhs )
|
|
this->_value = rhs.getRawBits();
|
|
|
|
return *this;
|
|
}
|
|
|
|
/*
|
|
* other functions
|
|
*/
|
|
|
|
int Fixed::getRawBits( void ) const {
|
|
std::cout << "getRawBits member function called" << '\n';
|
|
return this->_value;
|
|
}
|
|
|
|
void Fixed::setRawBits( int const raw ) {
|
|
this->_value = raw;
|
|
}
|
|
|
|
void Fixed::toFloat( void ) const {}
|
|
int Fixed::toInt( void ) const {
|
|
return 0;
|
|
}
|