I'm working on a homework problem that requires me to do some floating point operations to illustrate the possible problems in assuming that floating point numbers are always completely accurate. I have the quoted program written and have managed to prove to myself that OS X does not use IEEE 754 floating point format. What format does it use?
Also in the second while loop, the printf lines end up differing in their output [also quoted] despite being mathematically equivalent. Any ideas why?
Also in the second while loop, the printf lines end up differing in their output [also quoted] despite being mathematically equivalent. Any ideas why?
int main (int argc, char * const argv[]) {
float eps = 1.0;
float epsplus1;
int step = 1;
printf("Problem 1.3.2\n\n");
while (eps != 0.0)
{
printf("After step %d, eps equals : %1.70f\n", step, eps);
eps = eps/2.0;
step+=1;
}
printf("After step %d, eps equals : %1.70f\n", step, eps);
eps = 1.0;
step = 1;
printf("Problem 1.3.3\n\n");
while ((1.0+eps) != (1.0+(eps/2.0)))
{
epsplus1=1.0+eps;
printf("After step %d, 1+eps equals : %1.70f\n", step, 1.0+eps);
printf("After step %d, 1+eps equals : %1.70f\n", step, epsplus1);
eps = eps/2.0;
step+=1;
}
printf("After step %d, 1+eps equals : %1.70f\n", step, 1.0+eps);
return 0;
}
second while loop said:.
.
. (idendical outputs until here)
After step 25, 1+eps equals : 1.0000000596046447753906250000000000000000000000000000000000000000000000
After step 25, 1+eps equals : 1.0000000000000000000000000000000000000000000000000000000000000000000000
After step 26, 1+eps equals : 1.0000000298023223876953125000000000000000000000000000000000000000000000
After step 26, 1+eps equals : 1.0000000000000000000000000000000000000000000000000000000000000000000000
After step 27, 1+eps equals : 1.0000000149011611938476562500000000000000000000000000000000000000000000
After step 27, 1+eps equals : 1.0000000000000000000000000000000000000000000000000000000000000000000000
.
.
.
After step 53, 1+eps equals : 1.0000000000000002220446049250313080847263336181640625000000000000000000
After step 53, 1+eps equals : 1.0000000000000000000000000000000000000000000000000000000000000000000000
After step 54, 1+eps equals : 1.0000000000000000000000000000000000000000000000000000000000000000000000