Python numpy epsilon machine

I'm trying to understand what machine epsilon is. According to Wikipedia, it can be calculated as follows:

def machineEpsilon(func=float): machine_epsilon = func(1) while func(1)+func(machine_epsilon) != func(1): machine_epsilon_last = machine_epsilon machine_epsilon = func(machine_epsilon) / func(2) return machine_epsilon_last 

However, it is only suitable for double precision numbers. I am interested in modifying it to support numbers with one precision. I read that you can use numpy, especially the numpy.float32 class. Can someone help with changing the function?

+42
python numpy epsilon
02 Oct '13 at 16:03
source share
3 answers

An easier way to get epsilon machines for this type of float is to use np.finfo() :

 print(np.finfo(float).eps) # 2.22044604925e-16 print(np.finfo(np.float32).eps) # 1.19209e-07 
+89
Oct 02 '13 at 16:16
source share

Another easy way to get epsilon:

 In [1]: 7./3 - 4./3 -1 Out[1]: 2.220446049250313e-16 
+50
Aug 6 '14 at 8:24
source share

It will work as David pointed out!

 >>> def machineEpsilon(func=float): ... machine_epsilon = func(1) ... while func(1)+func(machine_epsilon) != func(1): ... machine_epsilon_last = machine_epsilon ... machine_epsilon = func(machine_epsilon) / func(2) ... return machine_epsilon_last ... >>> machineEpsilon(float) 2.220446049250313e-16 >>> import numpy >>> machineEpsilon(numpy.float64) 2.2204460492503131e-16 >>> machineEpsilon(numpy.float32) 1.1920929e-07 
+11
Oct 02 '13 at 16:12
source share



All Articles