Crea sito

Machine epsilon ANSI C


Let’s start programming posts collection, with an important but very easy topic: Machine Epsilon (or Machine Zero, or Machine Precision).It represents the smallest floating point value belonging to F (set of floating point values), different in absolute value from 0. The algorithm to get this value consists of the following steps:

  • init a floating point variable u1=1
  • until 1+u1 is different from 1
    • divide u1 by 2
    • check if 1+u1 is equal to 1

The check 1+u1==1 is done because halving iteratively u1, it will achieve a so little value, that, when adding it to 1, the machine will consider it like 0 value, and so 1+epsilon = 1!

ANSI C code for Machine Epsilon calculation:

double MachineEpsilon()
{
 double u,u1,u2;
  u1=1;
  do
   {
    u=u1;
    u1/=2;
    u2=u1+1;
   }
  while (u2 != 1);

 return u;
}
more info on wikipedia.