Question

To write a C program (not C++) that converts numbers between Decimal and IEEE-754 format and...

To write a C program (not C++) that converts numbers between Decimal and IEEE-754 format and vice versa.

Inputs:

  • Number in Decimal format (including special case of 0)
  • Number in IEEE-754 format (including special cases)

Output:

  • Equivalent number in IEEE-754 format
  • Equivalent number in Decimal

Specification:

The program converts a number based on choosing from a menu of choices, where each choice calls the appropriate procedure, where the choices are:

  • Decimal to IEEE-754 conversion
  • IEEE-754 to Decimal conversion
  • Quit program

Special Cases

The program must also check for these special IEEE cases:

  • + 0
  • - 0
  • + infinity
  • - Infinity
  • NaN

What to do:

  • Make sure your code compiles with zyBooks’ zyLabs compiler--if it does not compile with zyBooks’ compiler it will be graded as not compiling, even if it compiles on your compiler on your desktop/laptop at home.

Only 3 submissions will be permitted!

  • Include all prompts and other messages as shown in the sample run that follows.
  • Case and whitespaces will be ignored
  • To mask a number to extract a field, use “variable” & “constant”
  • To print a number in hexadecimal format, use “%x”
  • To use the math library, use “include <math.h>” for functions like “pow(x,y)” to raise “x” to the “y” power

Sample test run

This sample run contains possible cases that will be tested, either individually, in their own Test Bench, or combined in a single Test Bench.

Note:

  • Inputs in the test run will not show up on the output display. The have been manually included here in BOLD for clarity.
  • Output result titles are preceded by two asterisks.
  • Whitespaces and case will be ignored by auto-grading, but all messages must be identical to those shown below in both wording and spelling. This includes any hyphens, asterisks and right-parentheses.
  • I suggest you use zyLab’s workbench in Develop Mode to pre-test your submissions. That is, make up your own input data and see if you get the correct results.

Test Inputs

These are the input test values. They do not appear on the output of the run.

1

2.5

2

4020000010

2

-126

2

FFFFFFFF

3

Test Output

Floating-point conversion:

1) Decimal to IEEE-754 conversion

2) IEEE-754 to Decimal conversion

3) Exit

Enter selection:1

Enter the decimal representation: 2.5

*** Sign: 0

** Biased exponent: 10000000

*** Mantissa: 01000000000000000000000

*** IEEE HEX: 40200000

Floating-point conversion:

1) Decimal to IEEE-754 conversion

2) IEEE-754 to Decimal conversion

3) Exit

Enter selection:2

Enter the IEEE-754 representation: 40200000

*** Sign: +

*** Unbiased exponent: 1

*** Normalized decimal: 1.250000

*** Decimal: 2.500000

Floating-point conversion:

1) Decimal to IEEE-754 conversion

2) IEEE-754 to Decimal conversion

3) Exit

Enter selection:1

Enter the decimal representation:0

*** Sign: 0

*** Biased exponent: 00000000

*** Mantissa: 00000000000000000000000

***The IEEE-754 representation is: 0.000000

Floating-point conversion:

1) Decimal to IEEE-754 conversion

2) IEEE-754 to Decimal conversion

3) Exit

Enter selection:2

Enter the IEEE-754 representation: -126

*** Sign: -

*** Special case: NaN

Floating-point conversion:

1) Decimal to IEEE-754 conversion

2) IEEE-754 to Decimal conversion

3) Exit

Enter selection: 2

Enter the IEEE-754 representation: FFFFFFFF

*** Sign: -

*** Special case: NaN

Floating-point conversion:

1) Decimal to IEEE-754 conversion

2) IEEE-754 to Decimal conversion

3) Exit Enter selection: 3

*** Program Terminated Normally

***ignore anything related to zylabs or zycompiler***

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

#include <stdio.h>

#include <limits.h>

#if UINT_MAX >= 0xFFFFFFFF

typedef unsigned uint32;

#else

typedef unsigned long uint32;

#endif

#define C_ASSERT(expr) extern char CAssertExtern[(expr)?1:-1]

// Ensure uint32 is exactly 32-bit

C_ASSERT(sizeof(uint32) * CHAR_BIT == 32);

// Ensure float has the same number of bits as uint32, 32

C_ASSERT(sizeof(uint32) == sizeof(float));

double Ieee754SingleDigits2DoubleCheat(const char s[32])

{

uint32 v;

float f;

unsigned i;

char *p1 = (char*)&v, *p2 = (char*)&f;

// Collect binary digits into an integer variable

v = 0;

for (i = 0; i < 32; i++)

v = (v << 1) + (s[i] - '0');

// Copy the bits from the integer variable to a float variable

for (i = 0; i < sizeof(f); i++)

*p2++ = *p1++;

return f;

}

double Ieee754SingleDigits2DoubleNoCheat(const char s[32])

{

double f;

int sign, exp;

uint32 mant;

int i;

// Do you really need strto*() here?

sign = s[0] - '0';

// Do you really need strto*() or pow() here?

exp = 0;

for (i = 1; i <= 8; i++)

exp = exp * 2 + (s[i] - '0');

// Remove the exponent bias

exp -= 127;

// Should really check for +/-Infinity and NaNs here

if (exp > -127)

{

// Normal(ized) numbers

mant = 1; // The implicit "1."

// Account for "1." being in bit position 23 instead of bit position 0

exp -= 23;

}

else

{

// Subnormal numbers

mant = 0; // No implicit "1."

exp = -126; // See your IEEE-54 formulas

// Account for ".1" being in bit position 22 instead of bit position -1

exp -= 23;

}

// Or do you really need strto*() or pow() here?

for (i = 9; i <= 31; i++)

mant = mant * 2 + (s[i] - '0');

f = mant;

// Do you really need pow() here?

while (exp > 0)

f *= 2, exp--;

// Or here?

while (exp < 0)

f /= 2, exp++;

if (sign)

f = -f;

return f;

}

int binary(int n, int i)

{

int k;

for (i--; i >= 0; i--)

{

k = n >> i;

if (k & 1)

printf("1");

else

printf("0");

}

}

typedef union

{

float f;

struct

{

unsigned int mantissa : 23;

unsigned int exponent : 8;

unsigned int sign : 1;

} field;

} myfloat;

void decimaltoIEEE() {

myfloat var;

printf("Enter any float number: ");

scanf("%f",&var.f);

printf("sign: %d\nExponent:\n",var.field.sign);

binary(var.field.exponent, 8);

printf("mantissa:\n");

binary(var.field.mantissa, 23);

printf("\n");

}


int main(void)

{

int c;

printf("1. IEEE 754 to decimal 2. vice versa");

while(1) {

scanf("%d",&c);

switch(c) {

case 1: decimaltoIEEE();

break;

case 2:

printf("IEEE 754 to decimal conversions\n");printf("%+g\n", Ieee754SingleDigits2DoubleCheat("110000101100010010000000000000000"));

printf("%+g\n", Ieee754SingleDigits2DoubleNoCheat("010000101100010010000000000000000"));

printf("%+g\n", Ieee754SingleDigits2DoubleCheat("000000000100000000000000000000000"));

printf("%+g\n", Ieee754SingleDigits2DoubleNoCheat("100000000100000000000000000000000"));

printf("%+g\n", Ieee754SingleDigits2DoubleCheat("000000000000000000000000000000000"));

printf("%+g\n", Ieee754SingleDigits2DoubleNoCheat("000000000000000000000000000000000"));

break;

default: break;

}

}

return 0;

}

Add a comment
Know the answer?
Add Answer to:
To write a C program (not C++) that converts numbers between Decimal and IEEE-754 format and...
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