-
Notifications
You must be signed in to change notification settings - Fork 0
/
exploring_properties_of_floating_point_numbers.c
292 lines (215 loc) · 7.45 KB
/
exploring_properties_of_floating_point_numbers.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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
This program explores the properties of floating-point numbers
*/
#include<stdio.h>
#include<math.h>
//this function expects to accept the void pointer to float
//it prints the bits' values of the single-precision floating-point number
//the sign bit goes on the left, then goes the exponent, the mantissa (fraction) appears on the right
void showBitsInFloat (const void * pointer);
//this function expects to accept the void pointer to double
//it prints the bits' values of the double-precision floating-point number
//the sign bit goes on the left, then goes the exponent, the mantissa (fraction) appears on the right
void showBitsInDouble (const void * pointer);
int main()
{
printf("\nadding a small value to a large value can be dangerous\n");
float var1 = 1000000;
float var2 = 1000000;
printf("\nvar1 befor looping %f\n", var1);
printf("var2 befor looping %f\n\n", var2);
for (int i = 0; i < 1000000; i++)
{
var1 += 1;
var2 += 0.01;
}
printf("var1 after looping 1000000 times and adding 1 = %f\n", var1);
printf("var2 after looping 1000000 times and adding 0.01 = %f\n\n", var2);
printf("--------------------------------------------------\n\n");
printf("comparing floating-point numbers can be dangerous\n\n");
//comparing floating-point numbers can be dangerous
float var3 = 0.1;
double var4 = 0.1;
printf("double var4 = %f | float var3 = %f \n\n", var4, var3);
if (var3 == var4)
{
printf("var3 and var4 are equal \n");
}
else if (var4 > var3)
{
printf("var4 is greater than var3 \n");
}
else if (var3 > var4)
{
printf("var3 is greater than var4 \n");
}
printf("\n");
printf("var3 in binary: ");
showBitsInFloat(&var3);
printf("var4 in binary: ");
showBitsInDouble(&var4);
/////////////////////////////////////////////
printf("---------\n\n");
float var5 = 0.2;
double var6 = 0.2;
printf("double var6 = %f | float var5 = %f \n\n", var6, var5);
if (var6 == var5)
{
printf("var6 and var5 are equal \n");
}
else if (var6 > var5)
{
printf("var6 is greater than var5 \n");
}
else if (var5 > var6)
{
printf("var5 is greater than var6 \n");
}
printf("\n");
printf("va5 in binary: ");
showBitsInFloat(&var5);
printf("var6 in binary: ");
showBitsInDouble(&var6);
//////////////////////////////////////
printf("---------\n\n");
float var7 = 0.5;
double var8 = 0.5;
printf("double var8 = %f | float var7 = %f \n\n", var8, var7);
if (var7 == var8)
{
printf("var7 and var8 are equal \n");
}
else if (var8 > var7)
{
printf("var8 is greater than var7 \n");
}
else if (var7 > var8)
{
printf("var7 is greater than var8 \n");
}
printf("\n");
printf("var7 in binary: ");
showBitsInFloat(&var7);
printf("var8 in binary: ");
showBitsInDouble(&var8);
printf("--------------------------------------------------\n\n");
printf("the distributive rule does not necessarily apply to floating-point numbers\n\n");
double y = 0.11;
double x = 0.22;
double z = 0.33;
double r1 = y * x + y * z;
double r2 = y * (x + z);
printf("r1 = y * x + y * z = %f\n", r1);
printf("r2 = y * (x + z) = %f\n\n", r2);
if (r1 != r2)
printf("r1 and r2 are NOT equal\n\n");
printf("r1 in binary: ");
showBitsInDouble(&r1);
printf("r2 in binary: ");
showBitsInDouble(&r2);
printf("--------------------------------------------------\n\n");
printf("the associative rule does not necessarily apply to floating-point numbers\n\n");
double a = 0.1111;
double b = 0.2222;
double c = 0.3333;
double res1 = (a + b) + c;
double res2 = a + (b + c);
printf("res1 = (a + b) + c = %f\n", res1);
printf("res2 = a + (b + c) = %f\n\n", res2);
if (res1 != res2)
printf("res1 and res2 are NOT equal\n\n");
printf("res1 in binary: ");
showBitsInDouble(&res1);
printf("res2 in binary: ");
showBitsInDouble(&res2);
printf("--------------------------------------------------\n\n");
printf("*****infinity in calculations******\n");
double infin = INFINITY;
printf("Positive infinity: ");
showBitsInDouble(&infin);
double dv1 = pow (10, 308);
double dv2 = dv1;
printf ("dv1 = dv2 = 10^308 in binary(double): ");
showBitsInDouble(&dv1);
double dv3 = dv1 * dv2; //infinity due to overflow
printf("dv1 * dv2: ");
showBitsInDouble(&dv3);
double dv4 = 1.0 / 0.0; //infinity due to division by zero
printf("division of a positive number by zero: ");
showBitsInDouble(&dv4);
double dv55 = 1.0 / INFINITY;
printf("division of 1.0 by infinity: ");
showBitsInDouble(&dv55);
double dv66 = INFINITY / INFINITY;
printf("Infinity over infinity: ");
showBitsInDouble(&dv66);
double dv77 = INFINITY / pow(10, 300);
printf("Infinity divided by 10^300: ");
showBitsInDouble(&dv77);
double dv88 = INFINITY - INFINITY;
printf("Infinity - Infinity: ");
showBitsInDouble(&dv88);
double dv99 = INFINITY * INFINITY;
printf("Infinity * Infinity: ");
showBitsInDouble(&dv99);
printf("--------------------------------------------------\n\n");
printf("******* NaN in calculations *********\n");
double nan = NAN;
printf("NaN: ");
showBitsInDouble(&nan);
double dv5 = 0.0 / 0.0; //nan due to zero over zero
printf("zero over zero: ");
showBitsInDouble(&dv5);
double dv6 = 1.0 / NAN;
printf("1 divided by NaN: ");
showBitsInDouble(&dv6);
double dv7 = 0.0 / NAN;
printf("0.0 divided by NaN: ");
showBitsInDouble(&dv7);
double dv8 = 1.0 + NAN;
printf("1 + NaN: ");
showBitsInDouble(&dv8);
double dv9 = NAN + INFINITY;
printf("NaN + Infinity: ");
showBitsInDouble(&dv9);
double dv10 = NAN * INFINITY;
printf("NaN * Infinity: ");
showBitsInDouble(&dv10);
return 0;
}
void showBitsInFloat (const void * pointer)
{
unsigned int * intPoint = (unsigned int *) pointer;
unsigned int bitPosition = 0x80000000;
unsigned char iteration = 0;
while(iteration<32)
{
if(bitPosition & *intPoint)
printf("1");
else
printf("0");
if(iteration == 0 || iteration == 8)
printf(" ");
bitPosition >>= 1;
iteration++;
}
printf("\n\n");
}
void showBitsInDouble (const void * pointer)
{
unsigned long int * longIntPoint = (unsigned long int *) pointer;
unsigned long int bitPosition = 0x8000000000000000;
unsigned char iteration = 0;
while(iteration<64)
{
if(bitPosition & *longIntPoint)
printf("1");
else
printf("0");
if(iteration == 0 || iteration == 11)
printf(" ");
bitPosition >>= 1;
iteration++;
}
printf("\n\n");
}