www.
linuxforu.
com | LINUX FOR YOU | FEBRUARY 2007
C M Y K
119
TTTTT
he discussion assumes that the underlying platform
supports the IEEE 754 standard for floating point
arithmetic.
Determine the output of the following
two programs:
// Program I
#include...
More
www.
linuxforu.
com | LINUX FOR YOU | FEBRUARY 2007
C M Y K
119
TTTTT
he discussion assumes that the underlying platform
supports the IEEE 754 standard for floating point
arithmetic.
Determine the output of the following
two programs:
// Program I
#include <assert.
h>
int main() {
float f = 0.
0;
int i;
for(i = 0; i < 1000; i++)
f += 0.
001;
assert(f == 1.
000);
}
// Program II
#include <stdio.
h>
int main() {
printf(“loop starts now.
.
.
\n”);
for(float f = 0.
0; f < 20000000.
0; f++)
; // just loop over, do nothing
printf(“.
.
.
done!”);
}
“Ah, easy.
.
.
” you say! But let’s see the answers first.
Program I fails with the assertion failure and Program II
gets into an infinite loop after printing:
“loop starts now.
.
.
\n”.
Before seeing the explanation, let’s have a quick
tutorial on floating point numbers.
The types that can represent the numbers with decimal
points (where the position of the decimal point is not
fixed, or in other words, is floating) are known as floating
point
Less