Question

Write a program in C++ that converts decimal numbers to IEEE Standard 754 Floating Point Single...

Write a program in C++ that converts decimal numbers to IEEE Standard 754 Floating Point Single Precision.

Please include code that converts to single precision and double precision as a second option.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE

#include <iostream>

using namespace std;

void printBinary(int n, int i)

{

  // Prints the binary representation

  // of a number n up to i-bits.

  int k;

  for (k = i - 1; k >= 0; k--) {

    if ((n >> k) & 1)

      printf("1");

    else

      printf("0");

  }

}

typedef union {

  float f;

  struct

  {

    // Order is important.

    // Here the members of the union data structure

    // use the same memory (32 bits).

    // The ordering is taken

    // from the LSB to the MSB.

    unsigned int mantissa : 23;

    unsigned int exponent : 8;

    unsigned int sign : 1;

  } raw;

} myfloat1;

typedef union {

  float f;

  struct

  {

    // Order is important.

    // Here the members of the union data structure

    // use the same memory (32 bits).

    // The ordering is taken

    // from the LSB to the MSB.

    unsigned long mantissa : 52;

    unsigned int exponent : 11;

    unsigned int sign : 1;

  } raw;

} myfloat2;


// Function to convert real value

// to IEEE foating point representation

void printIEEE(myfloat1 var)

{

  // Prints the IEEE 754 representation

  // of a float value (32 bits)

  printf("%d | ", var.raw.sign);

  printBinary(var.raw.exponent, 8);

  printf(" | ");

  printBinary(var.raw.mantissa, 23);

  printf("\n");

}

// Function to convert real value

// to IEEE foating point representation

void printIEEE(myfloat2 var)

{

  // Prints the IEEE 754 representation

  // of a float value (32 bits)

  printf("%d | ", var.raw.sign);

  printBinary(var.raw.exponent, 11);

  printf(" | ");

  printBinary(var.raw.mantissa, 52);

  printf("\n");

}

// Driver Code

int main()

{

  // Instantiate the union

  myfloat1 var1;

myfloat2 var2;

  printf("Enter a decimal number: ");

float num;

scanf("%f", &num);

printf("1. For single precision\n2. For double precision\nEnter your choice: ");

int choice;

scanf("%d", &choice);

if (choice == 1) {

// Get the real value

  var1.f = num;

  // Get the IEEE floating point representation

  printf("IEEE 754 single precision representation of %f is : \n",

    var1.f);

  printIEEE(var1);

}

else if (choice == 2) {

// Get the real value

  var2.f = num;

  // Get the IEEE floating point representation

  printf("IEEE 754 double precision representation of %f is : \n",

    var2.f);

  printIEEE(var2);

}


  return 0;

}

Add a comment
Know the answer?
Add Answer to:
Write a program in C++ that converts decimal numbers to IEEE Standard 754 Floating Point Single...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT