Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <sstream>
- #include <string>
- #include <cstdlib>
- #include <cmath>
- using namespace std;
- // Headers
- string toString (double);
- int toInt (string);
- double toDouble (string);
- int main() {
- double decimal;
- decimal = 263.3;
- double div;
- div = decimal;
- int sIZE;
- sIZE = 9;
- int integerB[sIZE];
- for (sIZE = 0; sIZE <= 8; sIZE++) {
- integerB[sIZE] = div % 2;
- div = div / 2;
- }
- div = decimal;
- int numberR;
- numberR = decimal;
- cout << "Decimal = " << numberR << " = ";
- for (sIZE = 8; sIZE >= 0; sIZE--) {
- cout << integerB[sIZE];
- }
- cout << "!" << endl;
- sIZE = 9;
- // past decimal point
- double divD;
- divD = decimal % 2;
- if (divD >= 1) {
- divD = divD - 1;
- }
- int decimalB[sIZE];
- div = decimal;
- for (sIZE = 0; sIZE <= 8; sIZE++) {
- divD = divD * 2;
- if (divD >= 1) {
- divD = divD - 1;
- decimalB[sIZE] = 1;
- } else {
- decimalB[sIZE] = 0;
- }
- }
- cout << "Past point = " << decimal % 2 - 1 << " = ";
- for (sIZE = 0; sIZE <= 8; sIZE++) {
- cout << decimalB[sIZE];
- }
- cout << "!" << endl;
- // Turn both into sam real number, then multiply by number of spaces needed to reach first digit
- double numB;
- numB = 0;
- int start;
- start = 9;
- double numStart[start];
- int end;
- start = 0;
- end = 8;
- int temp;
- for (sIZE = 8; sIZE >= 0; sIZE--) {
- cout << integerB[sIZE];
- numStart[sIZE] = integerB[sIZE];
- numB = numB + integerB[sIZE] / pow(10, sIZE);
- }
- // Begin actual reversing of "pre decimal"
- while (start < end) {
- temp = numStart[start];
- numStart[start] = numStart[end];
- numStart[end] = temp;
- start = start + 1;
- end = end - 1;
- }
- // ENd of pre decimal conversion
- // finding exponent / bias
- //
- int expo;
- if (numStart[0] == 1) {
- expo = 8;
- } else {
- if (numStart[1] == 1) {
- expo = 7;
- } else {
- if (numStart[2] == 1) {
- expo = 6;
- } else {
- if (numStart[3] == 1) {
- expo = 5;
- } else {
- if (numStart[4] == 1) {
- expo = 4;
- } else {
- if (numStart[5] == 1) {
- expo = 3;
- } else {
- if (numStart[6] == 1) {
- expo = 2;
- } else {
- if (numStart[7] == 1) {
- expo = 1;
- } else {
- if (numStart[8] == 1) {
- expo = 0;
- }
- }
- }
- }
- }
- }
- }
- }
- }
- sIZE = 9;
- int expoB[sIZE];
- int bias;
- bias = 127 + expo;
- cout << "Bias = " << bias << endl;
- // bias as binary
- int bits;
- bits = bias;
- for (sIZE = 0; sIZE <= 8; sIZE++) {
- expoB[sIZE] = bits % 2;
- bits = (double) bits / 2;
- }
- // trying to wrap up outputs ( I think)
- for (sIZE = 0; sIZE <= 8; sIZE++) {
- cout << numStart[sIZE];
- }
- cout << ".";
- for (sIZE = 0; sIZE <= 8; sIZE++) {
- cout << decimalB[sIZE];
- }
- return 0;
- }
- // The following implements type conversion functions.
- string toString (double value) { //int also
- stringstream temp;
- temp << value;
- return temp.str();
- }
- int toInt (string text) {
- return atoi(text.c_str());
- }
- double toDouble (string text) {
- return atof(text.c_str());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement