modif readme binary conversions recuperation methode deplacement binaires, extrait fonctions printBits dans fichier a part

This commit is contained in:
Hugo LAMY
2022-02-15 15:20:55 +01:00
parent c9a833bc00
commit 3e92c847fa
6 changed files with 63 additions and 50 deletions

View File

@@ -1,39 +1,5 @@
#include "Fixed.hpp"
/*
* functions to print numbers in binary
* for the float, found help from stackoverflow :
* https://stackoverflow.com/questions/474007/floating-point-to-binary-valuec
*/
std::string printBitsInt(int num)
{
int i = 0;
for (unsigned int mask = 1U << (sizeof(int) *8 -1); mask; mask >>= 1)
{
std::cout << ((num & mask) != 0);
i++;
if (i == 1 || i == 9 || i == 24)
std::cout << ' ';
}
return "";
}
std::string printBitsFloat(float num)
{
int *p = (int *)&num;
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 || i == 24)
std::cout << ' ';
}
return "";
}
#include "printBits.cpp"
/*
* statics variables initialisation
@@ -41,12 +7,13 @@ std::string printBitsFloat(float num)
* for MAX integer :
* 00000000 01111111 11111111 11111111 ( 8388607) (-1U >> (this->_frac +1))
* <= ... >=
* 11111111 10000000 00000000 00000000 (-8388608)
*
* 11111111 10000000 00000000 00000000 (-8388608) ~MAX
*
*/
int const Fixed::_frac = 8;
int const Fixed::_max = -1U >> (_frac +1);
int const Fixed::_fmax = -1U >> (_frac +1);
int const Fixed::_fmin = ~_fmax;
/*
* default constructor / copy constructor / destructor
@@ -75,7 +42,7 @@ Fixed::~Fixed( void ) {
Fixed::Fixed(int integer) {
std::cout << "Int constructor called" << '\n';
if (integer < ~this->_max || integer > this->_max)
if (integer < this->_fmin || integer > this->_fmax)
std::cout << "error: integer out of range" << '\n';
else
this->_value = integer << this->_frac;
@@ -84,10 +51,10 @@ Fixed::Fixed(int integer) {
Fixed::Fixed(float const floater) {
std::cout << "Float constructor called" << '\n';
if (floater < ~this->_max || floater > this->_max)
if (floater < this->_fmin || floater > this->_fmax)
std::cout << "error: float out of range" << '\n';
else
this->_value = floater * (1 << this->_frac);
this->_value = roundf(floater * (1 << this->_frac));
}
/*
@@ -122,7 +89,6 @@ float Fixed::toFloat( void ) const {
/*
* overload "<<" -> output fixed point in float representation
* took here : https://github.com/pgomez-a/42_CPP_Piscine/blob/master/cpp02/ex01/Fixed.cpp
*/
std::ostream & operator<<(std::ostream & o, Fixed const & rhs)

View File

@@ -26,7 +26,8 @@ private:
int _value;
static int const _frac;
static int const _max;
static int const _fmax;
static int const _fmin;
};

BIN
d02/ex01/fixed Executable file

Binary file not shown.

View File

@@ -1,6 +1,9 @@
#include "Fixed.hpp"
#include <iostream>
std::string printBitsFloat(float num);
std::string printBitsInt(int num);
int main( void ) {
Fixed a;

37
d02/ex01/printBits.cpp Normal file
View File

@@ -0,0 +1,37 @@
#include <iostream>
/*
* functions to print numbers in binary
* for the float, found help from stackoverflow :
* https://stackoverflow.com/questions/474007/floating-point-to-binary-valuec
*/
std::string printBitsInt(int num)
{
int i = 0;
for (unsigned int mask = 1U << (sizeof(int) *8 -1); mask; mask >>= 1)
{
std::cout << ((num & mask) != 0);
i++;
if (i == 1 || i == 9 || i == 24)
std::cout << ' ';
}
return "";
}
std::string printBitsFloat(float num)
{
int *p = (int *)&num;
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 || i == 24)
std::cout << ' ';
}
return "";
}