-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpws24-hw7.c
105 lines (85 loc) · 2.25 KB
/
pws24-hw7.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* pws24-hw7.c
*
* Created on: May 30, 2016
* Author: Patrick Stevens
*/
#include <stdio.h>
typedef union {
float num;
unsigned int bits : 32;
} Float;
typedef unsigned char BYTE;
void fraction(Float fbin) {
BYTE b;
double num = 1.0;
double den = 2.0;
int expon, unbias, sign;
float fraction = 1.0, result = 1.0;
unbias = ((fbin.bits & 0x7F800000) >> 23) - 127;
sign = (fbin.bits & 0x80000000) >> 31;
fbin.bits = fbin.bits << 9;
printf("\nCreating the fraction:\n");
printf("fraction = 1.000000 (the implicit 1)\n");
for (int i = 0; i < 23; i++) {
b = (fbin.bits & 0x80000000) >> 31;
fraction = num / den;
if (b == 1) {
result += fraction;
printf("fraction = %f, after adding %f\n", result, fraction);
}
else
printf("fraction = %f, after skipping %f\n", result, fraction);
fbin.bits = fbin.bits << 1;
den += den;
}
printf("\nApplying the exponent:\n");
printf("unbiased exponent = %d\n", unbias);
if (unbias > 0) {
for (int i = 0; i < unbias; i++)
printf("times 2 = %f\n", result *= 2);
}
else {
for (int i = 0; i > unbias; i--)
printf("divided by 2 = %f\n", result /= 2);
}
if (sign == 1)
result -= (result * 2);
printf("\nFinal Answer: %f\n", result);
}
void printFB(Float fbin) {
int i;
printf("Your float in 32 bits: ");
for (i = 0; i < 32; i++) {
printf("%d", (fbin.bits & 0x80000000) >> 31);
fbin.bits = fbin.bits << 1;
}
printf("\n");
}
void printGen(Float fbin) {
for (int i = 0; i < 32; i++) {
if (i == 0) {
printf("Sign: ");
} else if (i == 1) {
printf("\nExponent: ");
} else if (i == 9) {
printf("\nFraction: ");
}
printf("%d", (fbin.bits & 0x80000000) >> 31);
fbin.bits = fbin.bits << 1;
}
printf("\n");
}
int main (void) {
float fn;
printf("Enter a float: ");
//input
scanf("%f", &fn);
printf("Your float was read as: %f\n", fn);
Float fbin;
fbin.num = fn;
printFB(fbin);
printGen(fbin);
fraction(fbin);
return 0;
}