Finding Epsilon
| In Brief | Find the machine epsilon of a computer, that is, the smallest number E such that 1+E != 1 . (using single precision only) |
| Language | C |
# 's
1/* By David Isaacson, 2003
2 * Released into the Public Domain
3 *
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)
7 */
8
9#include <stdio.h>
10
11int main()
12{
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.
17
18 do
19 {
20 test=(max+min)/2; //Set test to avg of max & min
21
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
25 min=test;
26
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.
33
34 printf("The epsilon for your machine is %e.\n",max);
35 //max is the lowest value that works.
36
37 return 0;
38
39}
40
41/* When running this program on my computer (Power Mac G4 533 MHz running Mac OS X 10.2), I
42 * recieved the result:
43 *
44 * The epsilon for your machine is 5.960465e-08.
45 */
Find the machine epsilon of a computer, that is, the smallest number E such that 1+E != 1 . (using single precision only)
Comments