In: Computer Science
In Python, write a program that computes machine epsilon (macheps) in:
Single precision (32 bits) and Double precision (64 bits)
I write Code in Python.
Q:write a program that computes machine epsilon (macheps) in:
Single precision (32 bits) and Double precision (64 bits)
Answer:
---------------------------------------------------------
Code:
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
-----------------------------------------------------------
Now execute the code
Input and Output:
Input: >>> machineEpsilon(float) Output: 2.220446049250313e-16 Input:>>> import numpy >>> machineEpsilon(numpy.float64) #double precision Output: 2.2204460492503131e-16 Input: >>> machineEpsilon(numpy.float32) #single precision Output: 1.1920929e-07
-----------------------------------------------------------------------
I used Python Compilers for executing above code.