ajout explications transformation float en fixed
This commit is contained in:
@@ -1,22 +1,40 @@
|
||||
#include "Fixed.hpp"
|
||||
|
||||
/*
|
||||
* function to print integers in binary
|
||||
* functions to print numbers in binary
|
||||
* for the float, found help from stackoverflow :
|
||||
* https://stackoverflow.com/questions/474007/floating-point-to-binary-valuec
|
||||
*/
|
||||
|
||||
void printBits(std::string before, unsigned int num)
|
||||
std::string printBitsInt(int num)
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
std::cout << before;
|
||||
for (unsigned int mask = 1U << 31; mask; mask = mask >> 1)
|
||||
std::cout << " ";
|
||||
for (unsigned int mask = 1U << (sizeof(int) *8 -1); mask; mask >>= 1)
|
||||
{
|
||||
std::cout << ((num & mask) != 0);
|
||||
i++;
|
||||
if (i % 8 == 0)
|
||||
// if (i % 8 == 0 && i < 32)
|
||||
if (i == 1 || i == 9 || i == 24)
|
||||
std::cout << ' ';
|
||||
}
|
||||
std::cout << "(" << (signed int)num << ")" << '\n';
|
||||
return "";
|
||||
}
|
||||
|
||||
std::string printBitsFloat(float num)
|
||||
{
|
||||
int *p = (int *)#
|
||||
int i = 0;
|
||||
|
||||
for (unsigned int mask = 1U << (sizeof(float) *8 -1); mask; mask >>= 1)
|
||||
{
|
||||
std::cout << ((*p & mask) != 0);
|
||||
i++;
|
||||
if (i == 1 || i == 9)
|
||||
std::cout << ' ';
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -37,18 +55,18 @@ int const Fixed::_max = -1U >> (_frac +1);
|
||||
*/
|
||||
|
||||
Fixed::Fixed() : _value(0) {
|
||||
std::cout << "Default constructor called" << '\n';
|
||||
// std::cout << "Default constructor called" << '\n';
|
||||
return;
|
||||
}
|
||||
|
||||
Fixed::Fixed(Fixed const & src) {
|
||||
std::cout << "Copy constructor called" << '\n';
|
||||
// std::cout << "Copy constructor called" << '\n';
|
||||
*this = src;
|
||||
return;
|
||||
}
|
||||
|
||||
Fixed::~Fixed( void ) {
|
||||
std::cout << "Destructor called" << '\n';
|
||||
// std::cout << "Destructor called" << '\n';
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -57,27 +75,26 @@ Fixed::~Fixed( void ) {
|
||||
*/
|
||||
|
||||
Fixed::Fixed(int integer) {
|
||||
std::cout << "Int constructor called" << '\n';
|
||||
// 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);
|
||||
std::cout << "integer : " << printBitsInt(integer) << " (" << integer << ")\n";
|
||||
this->_value = integer << this->_frac;
|
||||
printBits("integer : ", this->_value);
|
||||
}
|
||||
|
||||
Fixed::Fixed(float const floater) {
|
||||
std::cout << "Float constructor called" << '\n';
|
||||
// std::cout << "Float constructor called" << '\n';
|
||||
|
||||
if (floater < ~this->_max || floater > this->_max)
|
||||
{
|
||||
std::cout << "error: float out of range" << '\n';
|
||||
return;
|
||||
}
|
||||
|
||||
std::cout << "floater : " << printBitsFloat(floater) << " (" << floater << ")\n";
|
||||
|
||||
}
|
||||
|
||||
@@ -86,7 +103,7 @@ Fixed::Fixed(float const floater) {
|
||||
*/
|
||||
|
||||
Fixed & Fixed::operator=( Fixed const & rhs ) {
|
||||
std::cout << "Copy assignment operator called" << '\n';
|
||||
// std::cout << "Copy assignment operator called" << '\n';
|
||||
if ( this != &rhs )
|
||||
this->_value = rhs.getRawBits();
|
||||
|
||||
@@ -98,7 +115,7 @@ Fixed & Fixed::operator=( Fixed const & rhs ) {
|
||||
*/
|
||||
|
||||
int Fixed::getRawBits( void ) const {
|
||||
std::cout << "getRawBits member function called" << '\n';
|
||||
// std::cout << "getRawBits member function called" << '\n';
|
||||
return this->_value;
|
||||
}
|
||||
|
||||
@@ -110,3 +127,5 @@ void Fixed::toFloat( void ) const {}
|
||||
int Fixed::toInt( void ) const {
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
BIN
d02/ex01/fixed
BIN
d02/ex01/fixed
Binary file not shown.
@@ -3,8 +3,7 @@
|
||||
|
||||
int main( void ) {
|
||||
|
||||
Fixed a( 1 );
|
||||
Fixed const b( 1.5 );
|
||||
Fixed a(1105979538);
|
||||
|
||||
// Fixed a;
|
||||
// Fixed const b( 10 );
|
||||
|
||||
Reference in New Issue
Block a user