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

@@ -245,7 +245,7 @@ REPRESENTATION FLOATS vs INTEGERS
---------------------------------------------------------
```
integer : 1 (1)
integer : 1 (1)
floater : 0 01111111 00000000000000000000000 (1)
integer : 1010 (10)
@@ -266,7 +266,7 @@ floater : 0 10000100 00010 100000000000000000 (34.5)
100010.100011001100110011 (34.55)
floater : 0 10000100 00010 100011001100110011 (34.55)
1.0001100110011001101 (1.1)
1.0001100110011001101 (1.1)
floater : 0 01111111 00011001100110011001101 (1.1)
100010.01 (34.25)
@@ -291,7 +291,7 @@ par multiplications binaires :
100010.01000000 34.25
* 1 00000000.00000000 * 256.00
----------------------- --------
100010 01000000.0 8768.00
100010 01000000 8768.00
34.25 * (1 << _frac)
```
@@ -301,10 +301,16 @@ par decalage binaire :
10000100 (132) decaler la virgule de 132 - 127 = 5
100010.01 (34.25) fixe
value = 34.25;
signe = (unsigned int)value;
exponent = ((unsigned int)(value << 1) >> 24) - 127;
fixedd = (unsigned int)(value << 9) >> (32 - exponent - 8);
this->_value = *((int *)&floater); // access float adress content as int
int sign = this->_value & (1 << 31); // extract sign
int exponent = ((unsigned int)(this->_value << 1) >> 24) - 127; // extract exponent
int integer = (this->_value << 8) | (1 << 31); // add left 1
integer = (unsigned int)integer >> (31 - this->_frac - exponent);// align to right
if (sign != 0)
integer = (~integer + 1); // reverse negatif
integer = (integer << (30 - this->_frac - exponent)) | sign; // add sign
integer >>= (30 - this->_frac - exponent); // align right
std::cout << "integer : " << printBitsInt(integer) << " (" << integer << ")\n";
```

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 "";
}