Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Machine Epsilon in Python

A manual that I am currently studying (I am a newbie) says:

"Numbers which differ by less than machine epsilon are numerically the same"

With Python, machine epsilon for float values can be obtained by typing

eps = numpy.finfo(float).eps

Now, If I check

1 + eps/10 != 1

I obtain False.

But If I check

0.1 + eps/10 != 0.1

I obtain True.

My latter logical expression turns to be False if I divide eps by 100. So, how does machine epsilon work? The Python documentation just says

"The smallest representable positive number such that 1.0 + eps != 1.0. Type of eps is an appropriate floating point type."

Thank you in advance.

like image 287
Charlie Avatar asked Jan 05 '16 12:01

Charlie


People also ask

What is machine epsilon Python?

Epsilon doesn't have any special meaning in Python. It is not a keyword, and you can use the term epsilon to define variable names. In Python, the semantic meaning of epsilon has been derived from its mathematical meaning. Thus, we can define epsilon as the smallest possible positive floating-point number.

What is the value of machine epsilon?

The machine epsilon (in double precision) is eps =2.220446049250313e−016. It is obtained when the number of terms is n =53.

How do I get the epsilon machine?

Machine epsilon ϵ is the distance between 1 and the next floating point number. Machine precision u is the accuracy of the basic arithmetic operations. This number is also know as the unit roundoff. When the precision is p and the radix is β we have ϵ=β1−p.


1 Answers

Floating point numbers have a certain precision, to a few decimal places in scientific notation. The larger the number, the larger the least significant digit in that representation, and thus the larger the "epsilon" that could contribute to that number.

Thus, the epsilon is relative to the number it is added to, which is in fact stated in the documentation you cited: "... such that 1.0 + eps != 1.0". If the "reference" number is smaller by, e.g. one order of magnitude, then eps is smaller, too.

If that was not the case, you could not calculate at all with numbers smaller than eps (2.2e-16 in my case).

like image 120
tobias_k Avatar answered Oct 23 '22 14:10

tobias_k