• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Untitled

a guest Oct 16th, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include <stdio.h>
2. #include <math.h>
3. #include <stdbool.h>
4.
5. /*************************/
6. void decToIEEE()
7. {
8.     /* declare local variables */
9. float decimal;
10. int sign;
11. int exponent;
12. int mantissa = 0;
13. int ieee = 0;
14.
15.     /* prompt for floating point decimal number */
16. printf("\nEnter Decimal Representation: ");
17. scanf("%f", &decimal);
18.
19.     /* Check for 0--if so, print result */
20. if (decimal == 0.0)
21.     printf("\n\nSign: 0\nBiased Exponent: 00000000\nMantissa: 0000000000000000000000\nIEEE-754: 0000 0000\n\n");
22. else {
23.         /* Print sign: if number>0, sign is 0, else 1 */
24.     sign = (decimal > 0) ? 0 : 1;
25.     printf("\n\nSign: %d\n", sign);
26.
27.         /* take absolute value of number before generating significand */
28.     decimal = (decimal < 0) ? decimal*-1 : decimal;
29.
30.         /* Normalize number:
31.         while number >2, divide by 2, increment exponent
32.         while number <1, multiply by 2, decrement exponent
33.         */
34.     exponent = 0;
35.     while(decimal >= 2) {
36.         decimal = decimal / 2.0;
37.         exponent++;
38.     }
39.
40.     while(decimal < 1) {
41.         decimal *= 2.0;
42.         exponent--;
43.     }
44.
45.
46.             /* Bias exponent by 127 and print each bit in binary with 8-iteration for-loop*/
47.     exponent += 127;
48.
49.     printf("Exponent: ");
50.
51.     float temp_exp = exponent;
52.     for (int i = 0; i < 8; i++) {
53.         if (temp_exp > 128) {
54.             printf("1");
55.             temp_exp  -= 128;
56.         }
57.         else
58.             printf("0");
59.         temp_exp *= 2;
60.     }
61.
62.
63.             /* Hide 1 and print significand in binary with 23-iteration for-loop*/
64.     decimal -= 1; // decrement to remove the 1
65.
66.     printf("\nMantissa: ");
67.
68.     float temp_dec = decimal;
69.
70.     for (int i = 0; i < 23; i++) {
71.         if (temp_dec < 0.5) {
72.             printf("0");
73.             temp_dec *= 2;
74.         }
75.         else {
76.             printf("1");
77.             temp_dec = temp_dec*2 -1;
78.         }
79.     }
80.
81.
82.         /* Print IEEE-754 representation */
83.     ieee = sign * pow(2,31) + exponent*pow(2,23) + decimal*pow(2,23);
84.
85.     printf("\nIEEE format: %x", ieee);
86.
87. }
88.   return;
89. }
90.
91.
92. /***********************************************************************/
93. void ieeeToDec()
94. {
95.   /* declare local variables */
96.
97.     int ieee_number;
98.
99.   /* prompt for IEEE-754 representation */
100.
101.     printf("\nEnter IEEE-754 Representation: \n");
102.     scanf("%x", &ieee_number);
103.
104.     /* check for special cases: 0, -0, +infinity, -infinity, NaN,
105.         if so, print and return */
106.
107.     if (ieee_number == 0x00000000) // 0
108.             printf("Special case: +0\n");
109.     else if (ieee_number == 0x80000000) // -0
110.             printf("Special case: -0\n");
111.     else if(ieee_number == 0x7F800000) // + inf
112.             printf("Special case: Positive Infinity\n");
113.     else if(ieee_number == 0xFF800000) // - inf
114.             printf("Special case: Negative Infinity\n");
115.     else if ((ieee_number & 0x7FFFFFFF) > (0x7F800000)) // NaN, force the first number to be a 0
116.             printf("Special case: NaN\n");
117.     else {
118.
119.     // FOR OTHER NUMBERS, CHECK POSITIVE OR NEGATIVE AND VALUES WITH MASKING: USING '&' WITH 0's
120.
121.     /* Mask sign from number: if sign=0, print "+", else print "-" */
122.
123.
124.     /* Mask biased exponent and significand from number */
125.
126.     int exp = ((ieee_number & 0x7FFFFFFF) >> 23) - 127;
127.     float mantissa = ((ieee_number << 9) * pow(2,-32));
128.
129.     if (ieee_number >> 31 == 0) {
130.             printf("\nSign: +");
131.         }
132.         else {
133.             printf("\nSign: -");
134.             mantissa *= -1;
135.         }
136.
137.     /* If biased exponent=0, number is denormalized with unbiased exponent of -126,
138.         print denormalized number as fraction * 2^(-126), return */
139.
140.     if (exp == -127) {
141.         printf("\nUnbiased Exponent: -126\nDenormalized Decimal: %f*2^(-126)", mantissa);
142.     }
143.     else {
144.     /* Unbias exponent by subtracting 127 and print */
145.         printf("\nUnbiased Exponent: %d", exp);
146.     /* Add hidden 1 and print normalized decimal number */
147.         printf("\nNormalized decimal: %f", mantissa + 1);
148.     /* Print decimal number */
149.         printf("\nDecimal: %f", (mantissa + 1)*pow(2, exp));
150.     }
151. }
152.  return;
153. }
154.
155. int main()
156. {
157.   /* declare local variables */
158. int selection;
159.
160. bool exit = false;
161. while(!exit) {
162.
163.     printf("\n\nFloating-point conversion: \n__________________________\n");
164.     printf("1) Decimal to IEEE-754 conversion \n2) IEEE-754 to Decimal conversion \n3) Exit\n\nEnter Selection: ");
165.
166.     scanf("%d", &selection);
167.     switch (selection) {
168.         case 1: decToIEEE(); break;
169.         case 2: ieeeToDec(); break;
170.         case 3: exit = true; break;
171.     }
172. }
173.
174. /* until user chooses to quit, prompt for choice and select appropriate function */
175.
176.   return 0;
177. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top