I have a simple program to figure out the single machine precision (epsilon). According to C, float precision is 1.2e-8, but I'm getting the double precision, 2.2e-16. I can't figure out how else to force single precision?

void main(void)
{
    float eps = 1.0f;
    while( eps + 1.0f != 1.0f )
		eps /= 2.0f;	
     eps *= 2.0f;
     printf("Epsilon is %g \n", eps);

}

Perhaps something like this:
http://www.eskimo.com/~scs/C-faq/q14.5.html

#include <stdio.h>
#include <math.h>

int main(void)
{
   float a = 1.0F, epsilon = 1.19209e-07F;
   while ( (float)fabs(a) > epsilon )
   {
      printf("a = %g\n", a);
      a /= 2.0f;   
   }
   return 0;
}

/* my output
a = 1
a = 0.5
a = 0.25
a = 0.125
a = 0.0625
a = 0.03125
a = 0.015625
a = 0.0078125
a = 0.00390625
a = 0.00195312
a = 0.000976562
a = 0.000488281
a = 0.000244141
a = 0.00012207
a = 6.10352e-05
a = 3.05176e-05
a = 1.52588e-05
a = 7.62939e-06
a = 3.8147e-06
a = 1.90735e-06
a = 9.53674e-07
a = 4.76837e-07
a = 2.38419e-07
a = 1.19209e-07
*/

I'm trying not to used the predefined values for epsilon and find it through the program alone, using the definition that epsilon is the smallest number greater than 1. (that e+1 > 1 )

I tried doing the same thing for finding the smallest floating point too, (ie min+0 > 0), and that's also giving me huge values.. I'm really not sure what went wrong.

C89 has this:

The values of operands and of the results of expressions may be represented in greater precision and range than that required by the type; the types are not changed thereby.

And C99 has this:

The values of operations with floating operands and values subject to the usual arithmetic conversions and of floating constants are evaluated to a format whose range and precision may be greater than required by the type.

I have a simple program to figure out the single machine precision (epsilon). According to C, float precision is 1.2e-8, but I'm getting the double precision, 2.2e-16. I can't figure out how else to force single precision?

void main(void)
{
    float eps = 1.0f;
    while( eps + 1.0f != 1.0f )
		eps /= 2.0f;	
     eps *= 2.0f;
     printf("Epsilon is %g \n", eps);

}

I've encountered the same problem. The reason that you're getting the machine epsilon for double is that the expression eps+1.0f, like any computed floating point expression, is evaluated as a double precision number. It's only when you store it in a single precision variable that it becomes float. What you have to do to your program is declare a variable, say y, of type float, set y=eps+1.0f in your while loop and test to see whether y != 1.0f.

This article has been dead for over six months. Start a new discussion instead.