A
A
Anatoly2014-12-06 10:36:01
Python
Anatoly, 2014-12-06 10:36:01

What is machine epsilon?

Learning Python from Summerfield's book Programming in Python 3 (A Complete Introduction to the Python Language)
I got to machine epsilon.
Python has sys.float_info.epsilon and this value is the native epsilon. I understood it.
I also realized that there are values ​​less than the machine epsilon, but the computer does not work accurately with these values.
In the last line of the code below, I divide the machine epsilon by 2 and get a value less than the machine epsilon, then I add this value with one and as a result I get a number a little more than one, but this is "slightly", less than the number that the computer can represent correctly and therefore the computer rounds this "slightly" to zero, and as a result, in the last line, I get a unit without changes.

x = sys.float_info.epsilon
'''
this is how to work machine epsilon
1.0 + x != 1.0
1.0 + x/2 == 1.0
'''
print (1.0 + x) # output will be 1.0000000000000002  
print (1.0 + x/2) # output will be 1.0

In total, I have two questions:
1) Do I understand correctly that machine epsilon is the minimum difference between two numbers that a computer can work with without errors?
2) Are machine epsilon and machine zero synonyms?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
tsarevfs, 2014-12-06
@tsarevfs

The correct definition is:
The difference between 1 and the minimum number representable in a float greater than 1. To say that:

A machine epsilon is the smallest difference between numbers that a computer can distinguish.

not correct, because as you noticed, numbers less than FLT_EPSILON are quite representable in float and you can work with them.
The fact is that the density of numbers representable in float is not constant. The distance between 2 such adjacent numbers may be less than FLT_EPSILON around zero. Conversely, for values ​​close to MAX_FLOAT, the difference between adjacent values ​​can be greater than 1.
In python, the float type corresponds to double precision (64 bits) and all integers are represented in it. If you use the 32-bit version, you can get "strange" behavior. The following code loops.
import sys
from numpy import float32
a = float32(sys.float_info.max)
while a > sys.float_info.max - 10000:
   a -= 1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question