License Public Domain
Lines 38
Keywords
machine precision (1) numerical analysis (5)
Permissions
Viewable by Everyone
Editable by All Siafoo Users
Hide
Writing an article is easy - try our reStructured Text demo Join Siafoo Now or Learn More

Finding Epsilon Atom Feed 0

In Brief Find the machine epsilon of a computer, that is, the smallest number E such that 1+E != 1 . (using single precision only)
# '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

over 6 years ago (04 Feb 2010 at 08:17 PM) by Stou S.
Wow a 533Mhz Power Mac G4 you must be a real "Power User" (Who remembers the Mac Performa commercials from the late 90s?). For my machine I get 5.960465e-08 but if I change the datatypes to doubles I get 1.110223e-16
over 6 years ago (04 Feb 2010 at 08:23 PM) by David Isaacson
Well, good to know that doubles really give you double precision...
updated over 6 years ago (06 Feb 2010 at 01:57 AM) by Stou S.
If I use long double I get 1.991227e-314
over 6 years ago (04 Feb 2010 at 08:26 PM) by David Isaacson
holy crap... i can't imagine needing much better precision than that. I mean I'm sure there is some case, but wow.
over 6 years ago (04 Feb 2010 at 08:24 PM) by David Isaacson
and hey, that was a $1400 computer when i bought it, remember when computers cost that much? And that didn't even include the monitor. I used the whole $1900 computer stipend the school gave us.
over 6 years ago (04 Feb 2010 at 08:27 PM) by Stou S.
Man I remember when I bought a 350MB Hard Drive from circuit city for around $300...