To write a C program (not C++) that converts numbers between Decimal and IEEE-754 format and vice versa.
Inputs:
Output:
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:
Special Cases
The program must also check for these special IEEE cases:
What to do:
Only 3 submissions will be permitted!
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:
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***
#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;
}
To write a C program (not C++) that converts numbers between Decimal and IEEE-754 format and...
Watching a YouTube tutorial on how to convert decimal to floating point numbers (IEEE 754) and normalisation may prove to be beneficial. Watching a YouTube tutorial on how to convert decimal to floating point numbers (IEEE 754) may prove to be beneficial Convert the decimal number to 32 bits I Decimal number 18 to its binary equivalent I. 18 normalized in binary: 1.-2刈2n) II Biased exponent: 10 IV. Conversion to EE 754 16 I: 10, For ii please normalize the...
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.
This problem covers floating-point IEEE format. (a) Assuming single precision IEEE 754 format, what is the binary pattern for decimal number -6.16? (b) Assuming single precision IEEE 754 format, what decimal number is represented by this word: 0 01111100 01100000000000000000000 (Hint: remember to use the biased form of the exponent.)
5, [points] This problem covers floating-point IEEE format. (a) Assuming single precision IEEE 754 format, what is the binary pattern for decimal number -6.16? (b) Assuming single precision IEEE 754 format, what decimal number is represented by this word: 0 01111100 01100000000000000000000 (Hint: remember to use the biased form of the exponent.)
Inspired of the IEEE 754 standard, a floating point format that is only 10 bits wide is defined for a special computer. The leftmost bit is still the sign bit, the exponent is 5 bits wide and has a bias of 15, and the fractions is 4 bits long. A hidden 1 is assumed for the normal number, but not for the denormalized number. c) Construct a case to show that floating point addition is not associative
(2 pts) Express the base 10 numbers 16.75 in IEEE 754 single-precision floating point format. Express your answer in hexadecimal. Hint: IEEE 754 single-precision floating-point format consists of one sign bit 8 biased exponent bits, and 23 fraction bits) Note:You should show all the steps to receive full credits) 6.7510 Type here to search
2. Represent 25.28255 in 32 bit IEEE-754 floating point format as shown in the following format discussed in class. Sign Bit BIT 31 Exponent BITS 30:23 Mantissa BITS 22:0 BYTE 3+1 bit 7 Bits BYTE 1 BYTE O
Assignment (show how you calculate these): 1) According to the IEEE 754 protocols, compute the following values: i) The greatest, positive, normal, floating-point number has a binary representation of Mantissa Sign Exponent 0 1 1 1 1 1 1 1 1 Derive its decimal value? ii) The smallest, positive, normal, floating-point number has a binary representation of Sign Exponent O O O Mantissa Jo Jo O 1 O O O O O Derive its decimal value? iii) The greatest, positive,...
Question 19 In representing the decimal number 11/8 in IEEE 754 representation, the 8-bit binary value of the exponent plus bias for single precision is 01111111 01111110 01111101 10000000 Question 13 In representing the decimal number 11/8 in IEEE 754 representation, the 23-bit binary value of the fraction for single precision is 110 0000 0000 0000 0000 0000 111 0000 0000 0000 0000 0000 100 0000 0000 0000 0000 0000 011 0000 0000 0000 0000 0000 Question 12 The point...
6. The exponent in IEEE format floating point numbers are not represented in 2's complement format. Why not? What number is indicated if the value stored in the exponent is zero? What exponent and fraction are used to represent "not-a-number"? 7. This question deals with two numbers in IEEE format (A - 0x3F400000, B 0x3DB00000 (a) Calculate A+B using the floating-point addition procedure discussed in class. Determine the single precision result and express your answer in IEEE floating-point format. Convert...