Advertisement
Zander490

IEEE Project - Flowgarithm Export A

Jun 29th, 2020
1,177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <cmath>
  6.  
  7. using namespace std;
  8.  
  9. // Headers
  10. string toString (double);
  11. int toInt (string);
  12. double toDouble (string);
  13.  
  14. int main() {
  15.     double decimal;
  16.    
  17.     decimal = 263.3;
  18.     double div;
  19.    
  20.     div = decimal;
  21.     int sIZE;
  22.    
  23.     sIZE = 9;
  24.     int integerB[sIZE];
  25.    
  26.     for (sIZE = 0; sIZE <= 8; sIZE++) {
  27.         integerB[sIZE] = div % 2;
  28.         div = div / 2;
  29.     }
  30.     div = decimal;
  31.     int numberR;
  32.    
  33.     numberR = decimal;
  34.     cout << "Decimal = " << numberR << " = ";
  35.     for (sIZE = 8; sIZE >= 0; sIZE--) {
  36.         cout << integerB[sIZE];
  37.     }
  38.     cout << "!" << endl;
  39.     sIZE = 9;
  40.    
  41.     // past decimal point
  42.     double divD;
  43.    
  44.     divD = decimal % 2;
  45.     if (divD >= 1) {
  46.         divD = divD - 1;
  47.     }
  48.     int decimalB[sIZE];
  49.    
  50.     div = decimal;
  51.     for (sIZE = 0; sIZE <= 8; sIZE++) {
  52.         divD = divD * 2;
  53.         if (divD >= 1) {
  54.             divD = divD - 1;
  55.             decimalB[sIZE] = 1;
  56.         } else {
  57.             decimalB[sIZE] = 0;
  58.         }
  59.     }
  60.     cout << "Past point = " << decimal % 2 - 1 << " = ";
  61.     for (sIZE = 0; sIZE <= 8; sIZE++) {
  62.         cout << decimalB[sIZE];
  63.     }
  64.     cout << "!" << endl;
  65.    
  66.     // Turn both into sam real number, then multiply by number of spaces needed to reach first digit
  67.     double numB;
  68.    
  69.     numB = 0;
  70.     int start;
  71.    
  72.     start = 9;
  73.     double numStart[start];
  74.     int end;
  75.    
  76.     start = 0;
  77.     end = 8;
  78.     int temp;
  79.    
  80.     for (sIZE = 8; sIZE >= 0; sIZE--) {
  81.         cout << integerB[sIZE];
  82.         numStart[sIZE] = integerB[sIZE];
  83.         numB = numB + integerB[sIZE] / pow(10, sIZE);
  84.     }
  85.    
  86.     // Begin actual reversing of "pre decimal"
  87.     while (start < end) {
  88.         temp = numStart[start];
  89.         numStart[start] = numStart[end];
  90.         numStart[end] = temp;
  91.         start = start + 1;
  92.         end = end - 1;
  93.     }
  94.    
  95.     // ENd of pre decimal conversion
  96.     // finding exponent / bias
  97.     //
  98.     int expo;
  99.    
  100.     if (numStart[0] == 1) {
  101.         expo = 8;
  102.     } else {
  103.         if (numStart[1] == 1) {
  104.             expo = 7;
  105.         } else {
  106.             if (numStart[2] == 1) {
  107.                 expo = 6;
  108.             } else {
  109.                 if (numStart[3] == 1) {
  110.                     expo = 5;
  111.                 } else {
  112.                     if (numStart[4] == 1) {
  113.                         expo = 4;
  114.                     } else {
  115.                         if (numStart[5] == 1) {
  116.                             expo = 3;
  117.                         } else {
  118.                             if (numStart[6] == 1) {
  119.                                 expo = 2;
  120.                             } else {
  121.                                 if (numStart[7] == 1) {
  122.                                     expo = 1;
  123.                                 } else {
  124.                                     if (numStart[8] == 1) {
  125.                                         expo = 0;
  126.                                     }
  127.                                 }
  128.                             }
  129.                         }
  130.                     }
  131.                 }
  132.             }
  133.         }
  134.     }
  135.     sIZE = 9;
  136.     int expoB[sIZE];
  137.     int bias;
  138.    
  139.     bias = 127 + expo;
  140.     cout << "Bias = " << bias << endl;
  141.    
  142.     // bias as binary
  143.     int bits;
  144.    
  145.     bits = bias;
  146.     for (sIZE = 0; sIZE <= 8; sIZE++) {
  147.         expoB[sIZE] = bits % 2;
  148.         bits = (double) bits / 2;
  149.     }
  150.    
  151.     // trying to wrap up outputs ( I think)
  152.     for (sIZE = 0; sIZE <= 8; sIZE++) {
  153.         cout << numStart[sIZE];
  154.     }
  155.     cout << ".";
  156.     for (sIZE = 0; sIZE <= 8; sIZE++) {
  157.         cout << decimalB[sIZE];
  158.     }
  159.     return 0;
  160. }
  161.  
  162. // The following implements type conversion functions.
  163. string toString (double value) { //int also
  164.     stringstream temp;
  165.     temp << value;
  166.     return temp.str();
  167. }
  168.  
  169. int toInt (string text) {
  170.     return atoi(text.c_str());
  171. }
  172.  
  173. double toDouble (string text) {
  174.     return atof(text.c_str());
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement