diff --git a/d02/README.md b/d02/README.md new file mode 100644 index 0000000..c85ea7b --- /dev/null +++ b/d02/README.md @@ -0,0 +1,308 @@ + +--------------------------------------------------------- +DECIMALE VS BINARY +--------------------------------------------------------- + +11010101 +``` +1*10000000 | (128) | 2^7 | 128 *1 | 0 *2 +1 + 1*1000000 | (64) | 2^6 | + 64 *1 | 1 *2 +1 + 0*100000 | (32) | 2^5 | | 11 *2 + 1*10000 | (16) | 2^4 | + 16 *1 | 110 *2 +1 + 0*1000 | (8) | 2^3 | | 1101 *2 + 1*100 | (4) | 2^2 | + 4 *1 | 11010 *2 +1 + 0*10 | (2) | 2^1 | | 110101 *2 + 1*1 | (1) | 2^0 | + 1 *1 | 1101010 *2 +1 + | ______ | + | 213 | +``` +213 +``` + 2*100 |(1100100)| 10^2 | 1100100 *2 | 0 *10 +2 + 1*10 | (1010)| 10^1 | + 1010 *1 | 2 *10 +1 + 3*1 | (1)| 10^0 | + 1 *3 | 21 *10 +3 + | | | ___________ | 213 + | | | 11010101 | + | + | + 1100100 11001000 1 11010010 | + * 2 + 1010 * 3 + 11 | + _________ _________ ___ _________ | + 11001000 11010010 11 11010101 | + +``` +213 -> 11010101 +``` + 213 -128 = 85 | 213 /2 = 106 r 1 + 85 - 64 = 2 | 106 /2 = 53 r 0 + | 53 /2 = 26 r 1 + 21 - 16 = 5 | 26 /2 = 13 r 0 + | 13 /2 = 6 r 1 + 5 - 4 = 1 | 6 /2 = 3 r 0 + | 3 /2 = 1 r 1 + 1 - 1 = 0 | 1 /2 = 0 r 1 +``` +11010101 -> 213 +``` + | 0*2 + 1 = 1 + | 1*2 + 1 = 3 + | 3*2 = 6 + | 6*2 + 1 = 13 + | 13*2 = 26 + | 26*2 + 1 = 53 + | 53*2 = 106 + | 106*2 + 1 = 213 +``` + + +--------------------------------------------------------- +NEGATIVS INTEGERS +--------------------------------------------------------- + + 5 0101 0 0000 0 0000 0000 0000 0000 0000 0000 0000 0000 + 1010 + 1 1 0001 1 0000 0000 0000 0000 0000 0000 0000 0001 +-5 1011 2 0010 2 0000 0000 0000 0000 0000 0000 0000 0010 + 3 0011 3 0000 0000 0000 0000 0000 0000 0000 0011 + 4 0100 4 0000 0000 0000 0000 0000 0000 0000 0100 + -> 5 0101 5 0000 0000 0000 0000 0000 0000 0000 0101 + 6 0110 6 0000 0000 0000 0000 0000 0000 0000 0110 + 7 0111 7 0000 0000 0000 0000 0000 0000 0000 0111 + ... + 0111 1111 1111 1111 1111 1111 1111 1111 MAXINT + 1000 0000 0000 0000 0000 0000 0000 0000 MININT + ... + 8 1000 -8 1111 1111 1111 1111 1111 1111 1111 1000 + 9 1001 -7 1111 1111 1111 1111 1111 1111 1111 1001 + 10 1010 -6 1111 1111 1111 1111 1111 1111 1111 1010 + -> 11 1011 -5 1111 1111 1111 1111 1111 1111 1111 1011 + 12 1100 -4 1111 1111 1111 1111 1111 1111 1111 1100 + 13 1101 -3 1111 1111 1111 1111 1111 1111 1111 1101 + 14 1110 -2 1111 1111 1111 1111 1111 1111 1111 1110 + 15 1111 -1 1111 1111 1111 1111 1111 1111 1111 1111 + + +--------------------------------------------------------- +FLOATS +--------------------------------------------------------- +https://stackoverflow.com/questions/7644699/how-are-floating-point-numbers-stored-in-memory + + 1.....................23 + 8......1 + seeeeeeeemmmmmmmmmmmmmmmmmmmmmmm meaning +31 0 bit # + signe + exponent + mantis + +1.175494351 * 10^-38 < ... > 3.40282347 * 10^+38 +2x10^-1 = 0.2x10^0 = 0.02x10^1 = 0.2 + +x 00000000 xxxxxxxx xxxxxxxx xxxxxxx special meanings (see bellow) +x 11111111 xxxxxxxx xxxxxxxx xxxxxxx // +x 00000001 xxxxxxxx xxxxxxxx xxxxxxx smallest exponent (-126) +x 01111111 xxxxxxxx xxxxxxxx xxxxxxx 0 is middle exponent (254 / 2 = 127) +x 11111110 xxxxxxxx xxxxxxxx xxxxxxx biggest exponent (+127) + +x 00000000 00000000 00000000 0000000 0 (x can still be + or -) +x 11111111 00000000 00000000 0000000 +/- infinity +x 11111111 xxxxxxxx xxxxxxxx xxxxxx1 NaN (at least one non-zero mantissa digit) +x 00000000 xxxxxxxx xxxxxxxx xxxxxx1 denormalized numbers (same for mantissa) + +--------------------------------------------------------- +CONVERSIONS +--------------------------------------------------------- + +5.75 +``` +5 : + 5 / 2 = 2 -> 1 + 2 / 2 = 1 -> 0 + 1 / 2 = 0 -> 1 +75 : + .75 *2^2 = 2,88 ~= 3 + (*2^x parce que c le moyen dont fait "bouger" la virgule + en base 2 (*10^x) en base 10) + 3 / 2 = 1 -> 1 + 1 / 2 = 0 -> 1 +101.11 = 2^2 + 2^0 + 2^-1 + 2^-2 + = 4 + 1 + 0.5 + 0.25 +``` + +0.875 +``` +.875 * 2^3 = 7 -> 0.111 * 2^3 = 111.0 +7 / 2 = 3 -> 1 +3 / 2 = 1 -> 1 +1 / 1 = 0 -> 1 +0.111 +``` + +0.1875 +``` + -1 1875 +0.1875 = 1.875 * 10^-1 = 01 11101010011 +0.1875 = 0011 +0*2^-1 + 082^-2 + 1*2^-3 + 1*2^-4 + 0 + 0 + .125 + .0625 = .1875 +``` + +-43.625 +``` +-101011.101 +fixed : +1 101011 101 +- + 101011 + 2^5 + 2^3 + 2^1 + 1 + 32 + 8 + 2 + 1 = 43 + 101 + 2^-1 + 2^-3 + .5 + .125 = .625 +``` + +-53.5 --> -110101.1 +``` +[ fixed : ] + 1 110101 1 + - + 110101 + 2^5 + 2^4 + 2^2 + 1 + 32 + 16 + 4 + 1 = 53 + 1 + 2^-1 + .5 = .5 +[ float : ] + -1.101011 * 2^5 + 11.01011 -> 1 + 110.1011 -> 2 + 1101.011 -> 3 + 11010.11 -> 4 + 110101.1 -> 5 + 1 101 101011 + - + 2^2 + 1 + 4 + 1 = 5 + 101011 + ->1101011 +``` + +.85 +``` +.85 * 2 = 1.7 [1] [.7] + .7 * 2 = 1.4 [1] [.4] + .4 * 2 = 0.8 [0] [.8] + .8 * 2 = 1.6 [1] [.6] + .6 * 2 = 1.2 [1] [.2] + .2 * 2 = 0.4 [0] [.4] + .4 * 2 = 0.8 [0] [.8] + .8 * 2 = 1.6 [1] [.6] + .6 * 2 = 1.2 [1] [.2] + .2 * 2 = 0.4 [0] [.4] + .4 * 2 = 0.8 [0] [.8] + .8 * 2 = 1.6 [1] [.6] + .6 * 2 = 1.2 [1] [.2] + .2 * 2 = 0.4 [0] [.4] + .4 * 2 = 0.8 [0] [.8] + .8 * 2 = 1.6 [1] [.6] + .6 * 2 = 1.2 [1] [.2] + .2 * 2 = 0.4 [0] [.4] + .4 * 2 = 0.8 [0] [.8] + .8 * 2 = 1.6 [1] [.6] + .6 * 2 = 1.2 [1] [.2] + .2 * 2 = 0.4 [0] [.4] + .4 * 2 = 0.8 [0] [.8] + ... +-> 11011001100110011001100... + 1 1 0 1 1 0 0 1 1 0 0 1 1 +2^-1 + 2^-2 + 0 + 2^-4 + 2^-5 + 0 + 0 + 2^-8 + 2^-9 + 0 + 0 + 2^-12 + 2^-13 + .5 + .25 + .0625 + .03125 + .00390625 + .001953125 + .000244140625 + .0001220703125 += .8499755859375 +.5 +.75 +.8125 +.84375 +.84765625 +.849609375 +.849853515625 +.8499755859375 +``` + +.453125 +``` +.453125 *2 = 0.90625 [o] [.90625] +.90625 *2 = 1.8125 [1] [.8125] +.8125 *2 = 1.625 [1] [.625] +.625 *2 = 1.25 [1] [.25] +.25 *2 = 0.5 [0] [.5] +.5 *2 = 1 [1] [] +-> .011101 +``` + +--------------------------------------------------------- +REPRESENTATION FLOATS vs INTEGERS +--------------------------------------------------------- + +``` +integer : 1 (1) +floater : 0 01111111 00000000000000000000000 (1) + +integer : 1010 (10) +floater : 0 10000010 01000000000000000000000 (10) + +integer : 1100100 (100) +floater : 0 10000101 10010000000000000000000 (100) + +integer : 1111101000 (1000) +floater : 0 10001000 11110100000000000000000 (1000) + +integer : 00000000000000000000000000000010 (2) +floater : 0 10000000 00000000000000000000000 (2) + + 100010.1 (34.5) +floater : 0 10000100 00010 100000000000000000 (34.5) + + 100010.100011001100110011 (34.55) +floater : 0 10000100 00010 100011001100110011 (34.55) + + 1.0001100110011001101 (1.1) +floater : 0 01111111 00011001100110011001101 (1.1) + + 100010.01 (34.25) +floater : 0 10000100 00010 010000000000000000 (34.25) + + 10101.0011100001010001111 (21.22) +floater : 0 10000011 0101 0011100001010001111 (21.22) + + 110.11001100110011001101 (6.8) +floater : 0 10000001 10 110011001100110011010 (6.8) +``` +--------------------------------------------------------- +FLOATS -> FIXED +--------------------------------------------------------- + +par multiplications binaires : +``` + 100010.01 (34.25) +00100010 01000000 (8768) +00000001 00000000 (256) + + 100010.01000000 34.25 +* 1 00000000.00000000 * 256.00 +----------------------- -------- + 100010 01000000.0 8768.00 + +34.25 * (1 << _frac) +``` +par decalage binaire : +``` +0 10000100 00010 010000000000000000 (34.25) float + 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); +``` + + diff --git a/d02/ex00/Fixed.hpp b/d02/ex00/Fixed.hpp index cc08f04..9e78866 100644 --- a/d02/ex00/Fixed.hpp +++ b/d02/ex00/Fixed.hpp @@ -26,228 +26,3 @@ private: #endif -// -// /// DECIMALE VS BINARY ////////////////////////////////////// -// -// 11010101 -// -// 1*10000000 | (128) | 2^7 | 128 *1 | 0 *2 +1 -// 1*1000000 | (64) | 2^6 | + 64 *1 | 1 *2 +1 -// 0*100000 | (32) | 2^5 | | 11 *2 -// 1*10000 | (16) | 2^4 | + 16 *1 | 110 *2 +1 -// 0*1000 | (8) | 2^3 | | 1101 *2 -// 1*100 | (4) | 2^2 | + 4 *1 | 11010 *2 +1 -// 0*10 | (2) | 2^1 | | 110101 *2 -// 1*1 | (1) | 2^0 | + 1 *1 | 1101010 *2 +1 -// | ______ | -// | 213 | -// 213 -// -// 2*100 |(1100100)| 10^2 | 1100100 *2 | 0 *10 +2 -// 1*10 | (1010)| 10^1 | + 1010 *1 | 2 *10 +1 -// 3*1 | (1)| 10^0 | + 1 *3 | 21 *10 +3 -// | | | ___________ | 213 -// | | | 11010101 | -// | -// | -// 1100100 11001000 1 11010010 | -// * 2 + 1010 * 3 + 11 | -// _________ _________ ___ _________ | -// 11001000 11010010 11 11010101 | -// -// -// 213 -> 11010101 -// -// 213 -128 = 85 | 213 /2 = 106 r 1 -// 85 - 64 = 2 | 106 /2 = 53 r 0 -// | 53 /2 = 26 r 1 -// 21 - 16 = 5 | 26 /2 = 13 r 0 -// | 13 /2 = 6 r 1 -// 5 - 4 = 1 | 6 /2 = 3 r 0 -// | 3 /2 = 1 r 1 -// 1 - 1 = 0 | 1 /2 = 0 r 1 -// -// 11010101 -> 213 -// -// | 0*2 + 1 = 1 -// | 1*2 + 1 = 3 -// | 3*2 = 6 -// | 6*2 + 1 = 13 -// | 13*2 = 26 -// | 26*2 + 1 = 53 -// | 53*2 = 106 -// | 106*2 + 1 = 213 -// -// -// -// /// NEGATIVS INTEGERS /////////////////////////////////////// -// -// 5 0101 0 0000 0 0000 0000 0000 0000 0000 0000 0000 0000 -// 1010 + 1 1 0001 1 0000 0000 0000 0000 0000 0000 0000 0001 -// -5 1011 2 0010 2 0000 0000 0000 0000 0000 0000 0000 0010 -// 3 0011 3 0000 0000 0000 0000 0000 0000 0000 0011 -// 4 0100 4 0000 0000 0000 0000 0000 0000 0000 0100 -// -> 5 0101 5 0000 0000 0000 0000 0000 0000 0000 0101 -// 6 0110 6 0000 0000 0000 0000 0000 0000 0000 0110 -// 7 0111 7 0000 0000 0000 0000 0000 0000 0000 0111 -// ... -// 0111 1111 1111 1111 1111 1111 1111 1111 MAXINT -// 1000 0000 0000 0000 0000 0000 0000 0000 MININT -// ... -// 8 1000 -8 1111 1111 1111 1111 1111 1111 1111 1000 -// 9 1001 -7 1111 1111 1111 1111 1111 1111 1111 1001 -// 10 1010 -6 1111 1111 1111 1111 1111 1111 1111 1010 -// -> 11 1011 -5 1111 1111 1111 1111 1111 1111 1111 1011 -// 12 1100 -4 1111 1111 1111 1111 1111 1111 1111 1100 -// 13 1101 -3 1111 1111 1111 1111 1111 1111 1111 1101 -// 14 1110 -2 1111 1111 1111 1111 1111 1111 1111 1110 -// 15 1111 -1 1111 1111 1111 1111 1111 1111 1111 1111 -// -// -// /// FLOATS VS FIXED ///////////////////////////////////////// -// -// 1.....................23 -// 8......1 -// seeeeeeeemmmmmmmmmmmmmmmmmmmmmmm meaning -// 31 0 bit # -// -// 2x10^-1 = 0.2x10^0 = 0.02x10^1 = 0.2 -// -// -// ------------------------- -// -// 5.75 -// -// 5 : -// 5 / 2 = 2 -> 1 -// 2 / 2 = 1 -> 0 -// 1 / 2 = 0 -> 1 -// 75 : -// .75 *2^2 = 2,88 ~= 3 -// (*2^x parce que c le moyen dont fait "bouger" la virgule -// en base 2 (*10^x) en base 10) -// 3 / 2 = 1 -> 1 -// 1 / 2 = 0 -> 1 -// 101.11 = 2^2 + 2^0 + 2^-1 + 2^-2 -// = 4 + 1 + 0.5 + 0.25 -// -// ------------------------- -// -// 0.875 -// -// .875 * 2^3 = 7 -> 0.111 * 2^3 = 111.0 -// 7 / 2 = 3 -> 1 -// 3 / 2 = 1 -> 1 -// 1 / 1 = 0 -> 1 -// 0.111 -// -// ------------------------- -// -// 0.1875 -// -1 1875 -// 0.1875 = 1.875 * 10^-1 = 01 11101010011 -// 0.1875 = 0011 -// 0*2^-1 + 082^-2 + 1*2^-3 + 1*2^-4 -// 0 + 0 + .125 + .0625 = .1875 -// -// -// ------------------------- -// -// -43.625 -// -// -101011.101 -// fixed : -// 1 101011 101 -// - -// 101011 -// 2^5 + 2^3 + 2^1 + 1 -// 32 + 8 + 2 + 1 = 43 -// 101 -// 2^-1 + 2^-3 -// .5 + .125 = .625 -// -// -// ------------------------- -// -// -53.5 --> -110101.1 -// -// [ fixed : ] -// 1 110101 1 -// - -// 110101 -// 2^5 + 2^4 + 2^2 + 1 -// 32 + 16 + 4 + 1 = 53 -// 1 -// 2^-1 -// .5 = .5 -// [ float : ] -// -1.101011 * 2^5 -// 11.01011 -> 1 -// 110.1011 -> 2 -// 1101.011 -> 3 -// 11010.11 -> 4 -// 110101.1 -> 5 -// 1 101 101011 -// - -// 2^2 + 1 -// 4 + 1 = 5 -// 101011 -// ->1101011 -// -// -// ------------------------- -// -// .85 -// -// .85 * 2 = 1.7 [1] [.7] -// .7 * 2 = 1.4 [1] [.4] -// .4 * 2 = 0.8 [0] [.8] -// .8 * 2 = 1.6 [1] [.6] -// .6 * 2 = 1.2 [1] [.2] -// .2 * 2 = 0.4 [0] [.4] -// .4 * 2 = 0.8 [0] [.8] -// .8 * 2 = 1.6 [1] [.6] -// .6 * 2 = 1.2 [1] [.2] -// .2 * 2 = 0.4 [0] [.4] -// .4 * 2 = 0.8 [0] [.8] -// .8 * 2 = 1.6 [1] [.6] -// .6 * 2 = 1.2 [1] [.2] -// .2 * 2 = 0.4 [0] [.4] -// .4 * 2 = 0.8 [0] [.8] -// .8 * 2 = 1.6 [1] [.6] -// .6 * 2 = 1.2 [1] [.2] -// .2 * 2 = 0.4 [0] [.4] -// .4 * 2 = 0.8 [0] [.8] -// .8 * 2 = 1.6 [1] [.6] -// .6 * 2 = 1.2 [1] [.2] -// .2 * 2 = 0.4 [0] [.4] -// .4 * 2 = 0.8 [0] [.8] -// ... -// -> 11011001100110011001100... -// 1 1 0 1 1 0 0 1 1 0 0 1 1 -// 2^-1 + 2^-2 + 0 + 2^-4 + 2^-5 + 0 + 0 + 2^-8 + 2^-9 + 0 + 0 + 2^-12 + 2^-13 -// .5 + .25 + .0625 + .03125 + .00390625 + .001953125 + .000244140625 + .0001220703125 -// = .8499755859375 -// .5 -// .75 -// .8125 -// .84375 -// .84765625 -// .849609375 -// .849853515625 -// .8499755859375 -// -// -// ------------------------- -// -// .453125 -// -// .453125 *2 = 0.90625 [o] [.90625] -// .90625 *2 = 1.8125 [1] [.8125] -// .8125 *2 = 1.625 [1] [.625] -// .625 *2 = 1.25 [1] [.25] -// .25 *2 = 0.5 [0] [.5] -// .5 *2 = 1 [1] [] -// -> .011101 -// -// diff --git a/d02/ex01/Fixed.cpp b/d02/ex01/Fixed.cpp index 00567bd..63a5fa8 100644 --- a/d02/ex01/Fixed.cpp +++ b/d02/ex01/Fixed.cpp @@ -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; } + + diff --git a/d02/ex01/fixed b/d02/ex01/fixed index 7a4f401..e147d71 100755 Binary files a/d02/ex01/fixed and b/d02/ex01/fixed differ diff --git a/d02/ex01/main.cpp b/d02/ex01/main.cpp index 50a2890..252cc46 100644 --- a/d02/ex01/main.cpp +++ b/d02/ex01/main.cpp @@ -3,8 +3,7 @@ int main( void ) { - Fixed a( 1 ); - Fixed const b( 1.5 ); + Fixed a(1105979538); // Fixed a; // Fixed const b( 10 );