Skip to content

Instantly share code, notes, and snippets.

@AndrewBarfield
Created April 30, 2012 10:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndrewBarfield/2557034 to your computer and use it in GitHub Desktop.
Save AndrewBarfield/2557034 to your computer and use it in GitHub Desktop.
C#: Calculating machine epsilon for a Float and Double
using System;
namespace MachineEpsilon
{
class Program
{
static void Main( string[] args )
{
// Print Banner
Console.WriteLine(
"\nUNCW - CSC 340 - Scientific Computing\n" +
"Machine Epsilon Homework\nAndrew M. Barfield\n" +
DateTime.Now.ToLongDateString() + "\n" +
DateTime.Now.ToLongTimeString() +
"\n\n" );
// Functions that calclulate and print epsilon
CalculateMachineEpsilonForFloat();
Console.WriteLine("\n");
CalculateMachineEpsilonForDouble();
// Wait for key press
Console.Read();
}
static void CalculateMachineEpsilonForFloat()
{
Console.WriteLine( "Float:" );
float machineEpsilon = 1.0f;
float x = 0.0f;
int loopCount = 0;
do
{
machineEpsilon /= 2.0f;
x = 1.0f + machineEpsilon;
loopCount++;
Console.WriteLine( "\t" + loopCount.ToString( "00" ) + ") " + machineEpsilon.ToString() );
}
while ( x > 1.0 );
Console.WriteLine( "\n\tMantissa Bit Count: " + loopCount );
Console.WriteLine( "\tMachine epsilon for float: " + 2 * machineEpsilon );
}
static void CalculateMachineEpsilonForDouble()
{
Console.WriteLine( "Double:" );
double machineEpsilon = 1.0;
double x = 0.0;
int loopCount = 0;
do
{
machineEpsilon /= 2.0;
x = 1.0 + machineEpsilon;
loopCount++;
Console.WriteLine( "\t" + loopCount.ToString( "00" ) + ") " + machineEpsilon.ToString() );
}
while ( x > 1.0 );
Console.WriteLine( "\n\tMantissa Bit Count: " + loopCount );
Console.WriteLine( "\tMachine epsilon for double: " + 2 * machineEpsilon );
}
}
}
CSC 340 Scientific Computing
Machine Epsilon Homework
Andrew M. Barfield
Wednesday, January 14, 2009
3:15:29 PM
Float:
01) 0.5
02) 0.25
03) 0.125
04) 0.0625
05) 0.03125
06) 0.015625
07) 0.0078125
08) 0.00390625
09) 0.001953125
10) 0.0009765625
11) 0.0004882813
12) 0.0002441406
13) 0.0001220703
14) 6.103516E-05
15) 3.051758E-05
16) 1.525879E-05
17) 7.629395E-06
18) 3.814697E-06
19) 1.907349E-06
20) 9.536743E-07
21) 4.768372E-07
22) 2.384186E-07
23) 1.192093E-07
24) 5.960464E-08
Mantissa Bit Count: 24
Machine epsilon for float: 1.192093E-07
Double:
01) 0.5
02) 0.25
03) 0.125
04) 0.0625
05) 0.03125
06) 0.015625
07) 0.0078125
08) 0.00390625
09) 0.001953125
10) 0.0009765625
11) 0.00048828125
12) 0.000244140625
13) 0.0001220703125
14) 6.103515625E-05
15) 3.0517578125E-05
16) 1.52587890625E-05
17) 7.62939453125E-06
18) 3.814697265625E-06
19) 1.9073486328125E-06
20) 9.5367431640625E-07
21) 4.76837158203125E-07
22) 2.38418579101563E-07
23) 1.19209289550781E-07
24) 5.96046447753906E-08
25) 2.98023223876953E-08
26) 1.49011611938477E-08
27) 7.45058059692383E-09
28) 3.72529029846191E-09
29) 1.86264514923096E-09
30) 9.31322574615479E-10
31) 4.65661287307739E-10
32) 2.3283064365387E-10
33) 1.16415321826935E-10
34) 5.82076609134674E-11
35) 2.91038304567337E-11
36) 1.45519152283669E-11
37) 7.27595761418343E-12
38) 3.63797880709171E-12
39) 1.81898940354586E-12
40) 9.09494701772928E-13
41) 4.54747350886464E-13
42) 2.27373675443232E-13
43) 1.13686837721616E-13
44) 5.6843418860808E-14
45) 2.8421709430404E-14
46) 1.4210854715202E-14
47) 7.105427357601E-15
48) 3.5527136788005E-15
49) 1.77635683940025E-15
50) 8.88178419700125E-16
51) 4.44089209850063E-16
52) 2.22044604925031E-16
53) 1.11022302462516E-16
Mantissa Bit Count: 53
Machine epsilon for double: 2.22044604925031E-16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment