C++ program to find Machine Epsilon - GeeksforGeeks

23 Jan, 2018
Machine Epsilon is the smallest number of EPS (epsilon) such that 1 + EPS not equal to 1. Machine Epsilon is a machine-dependent floating point value that provides an upper bound on relative error due to rounding in floating point arithmetic. Mathematically, for each floating point type, it is equivalent to the difference between 1.0 and the smallest representable value that is greater than 1.0.
In C, machine epsilon is specified in the standard header with the names FLT_EPSILON, DBL_EPSILON, and LDBL_EPSILON. Those three macros give the machine epsilon for the float, double, and long double types, respectively.
In C++, similar macros are available in the standard header. The preferred way in C++ is to use std::numeric_limits::epsilon( ) – specified in the standard header .
In Java, it is referred to as ULP (unit in last place). You can find it by using the java.lang.Math package and the Math.ulp() method.
In Python3, the information is available in sys.float_info, which corresponds to float.h in C99.This is c++ equivalent of std::numeric_limits::epsilon( ).>>> import sys>>> sys.float_info.epsilon
CPP
Java
Python3
// C++ program to find machine epsilon#include<iostream>#include <cfloat>using namespace std;  // Function for Machine Epsilon with an// initial value provided as EPS.void machineEpsilon(float EPS){    // taking a floating type variable    float prev_epsilon;      // run until condition satisfy    while ((1+EPS) != 1)    {        // copying value of epsilon into previous epsilon        prev_epsilon = EPS;          // dividing epsilon by 2        EPS /=2;    }      // print output of the program    cout << "Machine Epsilon is : " << prev_epsilon << endl;}  // Driver Codeint main(){    // calling function which calculate machine epsilon    // with initial value provided as 0.5    machineEpsilon(0.5);      return 0;}
// Java program to find machine epsilon  import java.util.*;  class Count{      // Function for Machine Epsilon with an    // initial value provided as EPS.      public static void machineEpsilon(double EPS)    {        // taking a floating type variable        double prev_epsilon = 0.0;           // run until condition satisfy        while ((1+EPS) != 1)        {            // copying value of epsilon            // into previous epsilon            prev_epsilon = EPS;               // dividing epsilon by 2            EPS /=2;        }           // print output of the program        System.out.print( "Machine Epsilon is : "                         + prev_epsilon);    }             public static void main(String[] args)    {        // calling function which        // calculate machine epsilon        // with initial value provided as 0.5        machineEpsilon(0.5);      }}  // This code is contributed by rishabh_jain
# Python program to find machine epsilon  # Function for Machine Epsilon with an# initial value provided as EPS.def machineEpsilon(EPS):      # taking a floating type variable    # run until condition satisfy    while ((1+EPS) != 1):          # copying value of epsilon        # into previous epsilon        prev_epsilon = EPS           # dividing epsilon by 2        EPS = EPS / 2       # print output of the program    print( "Machine Epsilon is : " ,            prev_epsilon )  # Driven code# calling function which# calculate machine epsilon# with initial value provided as 0.5machineEpsilon(0.5);      # This code is contributed by# "rishabh_jain".

Machine Epsilon is : 1.19209e-07

Note that the above result varies from machine to machine.
This article is contributed by Sahil Rajput. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
rand() and srand() in C/C++
Left Shift and Right Shift Operators in C/C++
Stack in C++ STL
Sorting a vector in C++
Virtual Function in C++
unordered_map in C++ STL
C++ Classes and Objects
Socket Programming in C/C++
Multidimensional Arrays in C / C++
2D Vector In C++ With User Defined Size