Solve a problem – Filter by language, license, keyword, owner, or search text to find code & info fast.
|In Brief||Find the machine epsilon of a computer, that is, the smallest number E such that 1+E != 1 . (using single precision only)|
1/* By David Isaacson, 2003
2 * Released into the Public Domain
4 * epsilon.c
5 * The puropse of this program is to find the machine epsilon of this computer, that is,
6 * the smallest number E such that 1+E != 1 . (using single precision only)
13 float max = 1; //This is the lowest value that we know works.
14 //(By works, I mean that 1+E!=1)
15 float min = 0; //This is the highest value that we know doesn't work.
16 float test = 0; //This is our test varible. Duh.
20 test=(max+min)/2; //Set test to avg of max & min
22 if((1+test)!=1) //If we're too high, set the max to test and try again
23 max=test; //(We're trying to center in on the right value)
24 if((1+test)==1) //If we're too low, set the min to test and try again
27 }while(test!=((max+min)/2)); //If max and min are not changing, they must be so
28 //close that there is no number in between that the
29 //computer can represent.
30 //So, either test=min= the highest value that
31 //doesn't work, or test=max= the lowest
32 //value that does work. Either way we're done.
34 printf("The epsilon for your machine is %e.\n",max);
35 //max is the lowest value that works.
37 return 0;
41/* When running this program on my computer (Power Mac G4 533 MHz running Mac OS X 10.2), I
42 * recieved the result:
44 * The epsilon for your machine is 5.960465e-08.
Find the machine epsilon of a computer, that is, the smallest number E such that 1+E != 1 . (using single precision only)