-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex2-1.c
More file actions
201 lines (151 loc) · 4.37 KB
/
Copy pathex2-1.c
File metadata and controls
201 lines (151 loc) · 4.37 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <stdio.h>
#include <float.h>
long get_maximum(unsigned int type_size, int is_signed);
int power(int m, int n);
int get_max_exponent(int bits);
long get_maximum(unsigned int type_size, int is_signed) {
if (is_signed) {
return power(2, (type_size * 8) - 1) - 1;
} else {
return power(2, type_size * 8) - 1;
}
}
int power(int m, int n) {
int result = m;
while (--n > 0) {
result = result * m;
}
return result;
}
/* unclear what type to use */
double negative_expt(int m, int n) {
double result = (double) m;
while (--n > 0) {
result = result * ((double) m);
}
return 1.0 / result;
}
float binary_power(int n) {
float result = 2.0;
float m = 2.0;
int exponent;
/* If it's a negative power then we need to make it positive */
if (n < 0) {
exponent = -n;
} else {
exponent = n;
}
while (--exponent > 0) {
result *= m;
}
/* If it's a negative power return the reciprocal */
if (n < 0) {
return 1 / result;
} else {
return result;
}
}
/* Another version of the binary power function that results in a double */
double bpowerd(int n) {
double result = 2.0;
double m = 2.0;
int exponent;
/* If it's a negative power then we need to make it positive */
if (n < 0) {
exponent = -n;
} else {
exponent = n;
}
while (--exponent > 0) {
result *= m;
}
/* If it's a negative power return the reciprocal */
if (n < 0) {
return 1 / result;
} else {
return result;
}
}
void print_float(void) {
/* First calculate 2^127; this is guaranteed to fit in a float.
The value should be 1.70e38*/
float maximum_exponent = binary_power(get_max_exponent(8));
printf("Result is %g\n", maximum_exponent);
/* Now calculate 2^-23
The result should be 1.19e-7
*/
float epsilon = binary_power(-23);
float max = (2 - epsilon) * maximum_exponent;
printf("float: Max = %g\n", max);
if (max == FLT_MAX) {
printf("This is the largest float.\n");
} else {
printf("This is not the largest float.\n");
}
float min = binary_power(-126);
printf("float: Min = %g\n", min);
if (min == FLT_MIN) {
printf("This is the smallest float.\n");
} else {
printf("This is not the smallest float.\n");
}
}
int get_max_exponent(int bits) {
/* The bias is always (2^n-1)-1.
E.g. for 8 bits this will be 127.
*/
int bias = power(2, bits - 1) - 1;
/* The top of the range of a 'normal' unsigned number is given by
(2^n)-1, e.g. for 8 bits this will be 255. */
int max_representable = power(2, bits) - 1;
/* To get the actual max we subtract one (to account for the reserved
value representing infinity) and then subtract the bias. */
return max_representable - 1 - bias;
}
void print_double(void) {
/* The size of a double is:
11 bit exponent
52 bit mantissa
*/
double maximum_exponent = bpowerd(get_max_exponent(11));
printf("double: maximum exponent is %g\n", maximum_exponent);
/* Now calculate 2^-23
The result should be 1.19e-7
*/
double epsilon = bpowerd(-52);
double max = (2 - epsilon) * maximum_exponent;
printf("Maximum: %g\n", max);
if (max == DBL_MAX) {
printf("This is the largest double.\n");
} else {
printf("This is not the largest double.\n");
}
double min = bpowerd(-(get_max_exponent(11) - 1));
printf("double: Min = %g\n", min);
if (min == DBL_MIN) {
printf("This is the smallest double.\n");
} else {
printf("This is not the smallest double.\n");
}
}
void print_size(unsigned int type_size, int is_signed) {
long maximum = get_maximum(type_size, is_signed);
if (is_signed) {
printf("Maximum: %ld\n", maximum);
printf("Minimum: %ld\n", -maximum);
} else {
printf("Maximum: %ld\n", maximum);
printf("Minimum: 0\n");
}
}
/* Determine the range of various types. */
int main(void) {
printf("UNSIGNED CHAR:\n");
print_size(sizeof(signed char), 0);
printf("SIGNED CHAR:\n");
print_size(sizeof(signed char), 1);
printf("SIGNED SHORT:\n");
print_size(sizeof(signed short), 1);
print_float();
print_double();
}