Question

In: Computer Science

I'm trying to convert between different number representations in C++ , I have the prototype but...

I'm trying to convert between different number representations in C++ , I have the prototype but im not sure what do do from here

bool convertF(double & x, const string & bits);

bits is supposed to be an 8-bit (not 5-bit) value, each char being '0' or '1'; if not, return

false. The first bit of bits represents the sign bit, the next 3 bits represent the exponent,

and the next 4 bits represent the significand.

convertF may assume without checking that the 3 exponent bits are not all the same.

convertF's job is to set x to the floating-point number that bits represents and return

true.

For example, convertF(x, "101011000") should set x to -7/8 == -0.875

Solutions

Expert Solution

double helper(double a){
  
   string aString = to_string(a);
   int decimalPlace=aString.length();
   for(int i=0;i<aString.length();i++){
       if(aString[i]=='.'){
           decimalPlace = i;
           break;
       }
   }
   double ans = 0;
   double p = 1;
   for(int i=decimalPlace-1;i>=0;i--){
       if(aString[i]=='1')
           ans+=p;
       p = p*2;
   }
   p = 0.5;
   for(int i=decimalPlace+1;i<aString.length();i++){
       if(aString[i]=='1')
           ans+=p;
       p = p/2.0;
   }
   return ans;
}

bool convertF(double &x, const string & bits){

   if(bits.length()==8){
       for(int i=0;i<8;i++){
           if(bits[i]=='0' || bits[i]=='1')continue;
           else return false;
       }
       bool isPositive = true;
       if(bits[0]==1)isPositive = false;
       string exponent = bits[1]+bits[2]+bits[3];
       int valExp = binaryOf(exponent)-3;
       double mantissa = 0.0;
       if(bits[4]){
           mantissa+=0.1;      
       }
       if(bits[5]){
           mantissa+=0.01;
       }  
       if(bits[6]){
           mantissa+=0.001;
       }
       if(bits[7]){
           mantissa+=0.0001;
       }
       double val = 1.0;
       val+=mantissa;
       val = val*power(10,valExp);
       x = helper(val);

       if(!isPositive)x = x*(-1);
       return true;
   }
       return false;
}

The above is a complete implementation for given an 8 bit floating point representation , to find the floating point number . In the above code , two functions have been considered that they can be easily computed namely binaryOf() which takes in as input a string and finds the corresponding decimal value , and power() which calculates x^y even when y is negative.These two functions can be easily implemented but if difficulty is encountered,then do comment.


Related Solutions

I'm trying to convert between different number representations in C++ , I have the prototype but...
I'm trying to convert between different number representations in C++ , I have the prototype but im not sure what do do from here bool convert2(int & n, const string & bits); bits should have size exactly 5, and each char of bits should be '0' or '1'; otherwise return false. If bits is ok, set n to the number that bits represents as a 2's complement number and return true. For example, convertU(n, "10011") should set n to -13...
I'm trying to convert between different number representations in C++ , I have the prototype but...
I'm trying to convert between different number representations in C++ , I have the prototype but im not sure what do do from here bool convertU(unsigned & n, const string & bits); bits should have size exactly 5, and each char of bits should be '0' or '1'; otherwise return false. If bits is ok, set n to the number that bits represents as an unsigned and return true. For example, convertU(n, "0101") and convertU(n, "10210") should return false; convertU(n,...
I'm trying to work with this code that I have. i want to kick the user...
I'm trying to work with this code that I have. i want to kick the user back if they don't enter a number in the specified range but I haven't been able to get a while loop working right. Write a program name Blackjack_Jr that allows a human user to play a single hand of "blackjack" against a dealer. Pick two values from 1-10 for the player. These are the player's "cards". These two values must be user inputs Generate...
The source code I have is what i'm trying to fix for the assignment at the...
The source code I have is what i'm trying to fix for the assignment at the bottom. Source Code: #include <iostream> #include <cstdlib> #include <ctime> #include <iomanip> using namespace std; const int NUM_ROWS = 10; const int NUM_COLS = 10; // Setting values in a 10 by 10 array of random integers (1 - 100) // Pre: twoDArray has been declared with row and column size of NUM_COLS // Must have constant integer NUM_COLS declared // rowSize must be less...
I'm trying to use Jupyter (python) to convert the contents of a pkl file into a...
I'm trying to use Jupyter (python) to convert the contents of a pkl file into a dictionary, WITHOUT using pandas. I'm able to import pickle and can open my file...but I'm not sure how to create the dictionary.
I'm trying to make this C++ loan calculator but I can't get the program to output...
I'm trying to make this C++ loan calculator but I can't get the program to output what I need. For instance I know the formula is right and when I enter 100000 1.5 20 as my cin variables I should get 482.55 but I get 1543.31. What am I not seeing as the issue? Also this should cout only 2 decimal places right? I'm not allowed to alter the #include and add any fixed setprecision. This is what I have....
I'm trying to solve a problem where I have an object resting on an inclined plane,...
I'm trying to solve a problem where I have an object resting on an inclined plane, with the angle of the plan being alpha, and the weight being w. I'm having trouble figuring out how I can calculate the component of the weight parallel to the plane. I also want to find out the weight component perpendicular to the plane. I don't want an outright answer, more of an explanation to help me understand. Thanks!
Java I'm trying to create a program that replicates a theater ticket reservation system. I have...
Java I'm trying to create a program that replicates a theater ticket reservation system. I have a Seat class with members row(integer), seat(character) and ticketType(character). Where a ticket type can be 'A' adult, 'C' child, 'S' senior, or '.' recorded as empty. I have a Node generic class that points to other nodes(members): up, Down. Left, Right. It also has a generic payload. Lastly, I have an Auditorium class which is also generic. its member is a First Node<T>. As...
I have a decimal fraction: 76.234567x10^-14 I need to convert this number to binary. I know...
I have a decimal fraction: 76.234567x10^-14 I need to convert this number to binary. I know you can multiply the number by 2 constantly to get the binary number, but that will take forever to do, even converting it to hex with x16 takes a long time, is there any easier way to convert this? Show all steps and work please.
C++ program please, can you show me where I did wrong. I'm trying to print the...
C++ program please, can you show me where I did wrong. I'm trying to print the counter-clockwise spiral form using int *p, but my output turned out weird, thank you void makeSpiral(int *p, int rows, int cols) { int left = 0, value = 1, top = 0; while(left < cols && top < rows) { for(int i = top;i < rows;++i) { *(p+i*cols+left) = value++; } left++; for(int i = left;i < cols;++i) { *(p+(rows-1)*cols+i) = value++; } rows--;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT