Task 1: Machine Epsilon (10 pts)
Machine epsilon εm is a characteristic of the CPU in one’s computer. This machine constant is used extensively when writing computer code to help make one’s algorithms CPU insensitive.
Machine epsilon εm is the smallest number ε such that 1 + ε > 1 in floating-point arithmetic. For any smaller value of ε, round-off error would return 1 + ε = 1. Machine epsilon is defined by the formula εm = b1-m where b is the base number
used by your computer in its construction of a floating-point number (b = 2 in computers that run Intel chips and their clones), and m is the total number of digits in the mantissa (adjustable and fixed). Note: 1-m is the power of base bbecause the first bit in a mantissa is fixed (cf. Homework 2). The fixed bit does not appear explicitly in the bit array representing a floating-point number.
1) Write a Python function that:
Computes machine epsilon (εm). Use a while loop to accomplish this. Start with εm equaling 1. In each successive loop compute εm = εm / 2. Exit the loop when variable 1 + εm equals 1. Multiply the result coming out of this loop by 2 and you have computed machine epsilon for your computer. This function should have the following structure:
def machEps(): epsilon = 1.0
... return epsilon
Script files are executable files.
2) Write a Python script that:
Calls your function for computing machine epsilon, and then prints out (to the command window in Spyder) the answer in a readable format; specifically, the written output this script is to produce should look like:
machine epsilon = <the number>
The above output is what your grader will grade!
This script file must have the interface:
def runTask1():
in which you will call your function machEps and print out the
answer in the
format specified above.
def machEps():
epsilon = 1.0
while(epsilon>0):
value = epsilon
epsilon= epsilon/2
return value
Now what is happening here is, epsilon is the smallest value that computer can differentiate from 0. If a number is anything smaller then epsilon the computer treat it as 0.
So what we are doing is dividing the number /2 till we reach a point where that is 0 for the computer and when it happens the loop is over the value of epsilon right before the last division is what the computer can store ( smallest value) so we return the 'value' having that.
Now to write a script let's say, epsilon.py
interface script:
def runTask1():
epsilon = machEps()
print("machine epsilon = ",epsilon)
Hope it helps.
Get Answers For Free
Most questions answered within 1 hours.