Question
1) Consider a 1-byte floating-point system with 1 sign bit, 4 precision bits (for the mantissa), and 3 exponent bits corresponding to: $beta=2, p=4, e
1) Consider a 1-byte floating-point system with 1 sign bit, 4 precision bits (for the mantissa), and 3 exponent bits corresponding to: $\beta=2, p=4, e \in \{-4,-2,-1,0,1,2,3\}$.
In the template file, insert code to implement the `digits_to_num` function that computes the represented number corresponding to a specific set of parameter values. Assume that all input values are of the appropriate type (i.e., `digits` is an array of integers and the other arguments are integers).
The `p1()` function will call your implementation to produce values including the largest and smallest representable values? What should those values be? Check that your code produces the appropriate values.
2a) Insert code in the template file to implement the `mini_gamut` function to produce the array of representable values associated with exponent `e=0` (for base `beta=2`).
2b) Insert code to implement the `positive_gamut(e_min, e_max)` function to produce the gamut of positive representable values. (Use `e_min=-4` and `e_max=3` as in Problem 1.)
2c) Insert code to implement `gamut(e_min, e_max)` that returns a sorted array of all representable values for the system of Problem 1 (for base `beta=2`).
Function `p2()` will execute your functions to test how they work.
3) Fill in code in the template to implement the function `round_value(val, gamut)`. Given the gamut of representable values as a numpy array, the function should return the value in the gamut array that is closest to the input `val`. Use the gamut generated by the function you implemented in Problem 2c.
The function `p3()` will execute your function to test how it works.
4a) Insert code in the template file to implement the function `gamut_linspace(count, gamut)`. This function should return an array of `count` values equally spaced over the range from the minimum value to the maximum value in the gamut.
4b) Insert code in the template file to implement `round_values(vals, gamut)` that accepts an input array and returns a corresponding array of rounded values in the gamut.
4c) Use `plot_rounded_error(vals, rounded_vals)` along with the functions from 4a and 4b to generate an array of 1000 values and 1000 rounded values and plot the relative and absolute errors between the rounded and unrounded results. `plot_rounded_error` is mostly implemented, you only need to
implement the error computations. Use the gamut generated from problem 2. Function p4()` will execute and test your code.
5) What is the value of $\epsilon_M$ for this floating-point system. Explain how it plays a prominent role in one of the plots. Insert your response as a string to replace "YOUR ANSWER HERE" in the `p5()` function.
6) Do problem 3.4 from Schorghofer. Insert you answer as a string in a print statement in function `p6()`.
##Template
import numpy as np
import matplotlib.pyplot as plt
def digits_to_num(sign, exponent, digits, beta = 2):
'''
args:
sign: 0 for positive, 1 for negative
exponent: integer exponent value
digits: array of integers each in range (0, beta - 1)
beta: positive integer representing the base
return:
value of represented number
'''
pass
def mini_gamut():
'''
args:
return:
an array of all posive representable values for exponent = 0 and beta = 2
'''
pass
def positive_gamut(e_min, e_max):
'''
args:
e_min: minimum exponent
e_max: maximum exponent
return:
an array of all posive representable values in sorted order given the max and min exponent for beta = 2
'''
pass
def gamut(e_min, e_max):
'''
args:
e_min: minimum exponent
e_max: maximum exponent
return:
an array of all representable values in sorted order given the max and min exponent for beta = 2
'''
pass
def round_value(val, gamut):
'''
args:
val: input float value
gamut: array of representable numbers
return:
the closest representable number from gamut to val
'''
pass
def gamut_linspace(count, gamut):
'''
args:
count: number of entries to generate
gamut: array of representable numbers
return:
an array with `count` entries linearly spaced between the minimum and maximum representable number
'''
pass
def round_values(vals, gamut):
'''
args:
vals: array of float values
gamut: array of representable numbers
return:
the closest representable number from gamut to each val in vals
'''
pass
def plot_rounded_error(vals, rounded_vals):
'''
args:
vals: array of float values
rounded_vals: array of float values rounded to the closest representable number
return:
'''
# #uncomment when `e_abs` and `e_rel` are implemented
# e_abs =
# e_rel =
# fig, ax = plt.subplots(figsize = (12, 6))
# ax.plot(vals, e_abs, color = 'blue', label = 'Absolute error')
# ax.plot(vals, e_rel, color = 'black', label = 'Relative error')
# plt.legend()
# plt.ylim([0,0.6])
# plt.show()
pass
def p1():
"""
1) Consider a 1-byte floating-point system with 1 sign bit, 4 precision bits (for the mantissa), and 3 exponent bits corresponding to:
beta = 2, p = 4, e in {-4,-3,-2,-1,0,1,2,3}.
Implement `digits_to_num`. It should return the represented number corresponding to the specific set of input values.
The function calls below should produce the largest and smallest numbers the system can represent. What should these values be? Check
that your function produces the appropriate values.
"""
sign = 0
e = -4
digits = [1,1,1,1]
print("largest: ", digits_to_num(sign, e, digits))
e = 3
digits = [1,0,0,0]
print("smallest: ", digits_to_num(sign, e, digits))
def p2():
"""
2a) Implement `mini_gamut()`. It should return an array of the representable positive numbers for e = 0 and beta = 2.
"""
"""
2b) Implement `positive_gamut(e_min, e_max)`. It should return a sorted array of all positive representable values. Use e_min and e_max
from problem 1.
"""
"""
2c) Implement `gamut(e_min, e_max)`. It shoud return a sorted array of all representable values for the example system from problem 1. Use
e_min and e_max from problem 1.
"""
pass
def p3():
"""
3) Implement `round_value(val, gamut)`. It should return the closest value from `gamut` to the numerical input value, `val`. Use the gamut generated
from problem 2.
"""
pass
def p4():
"""
4a) Implement `gamut_linspace(count, gamut)`. It should return an array with `count` values linearly spaced between the min and max gamut value.
"""
"""
4b) Implement `round_values(vals, gamut)`. It should return an array of values which have been rounded.
"""
"""
4c) Use `plot_rounded_error(vals, rounded_vals)` along with the functions from 4a and 4b to generate an array of 1000 values and 1000 rounded values
and plot the relative and absolute errors between the rounded and unrounded results. `plot_rounded_error` is mostly implemented, you only need to
implement the error computations. Use the gamut generated from problem 2.
"""
pass
def p5():
"""
5) What is the value of machine epsilon for the floating-point system from problem 1? Explain how it plays a prominent role in one of the error plots.
"""
return """YOUR ANSWER HERE"""
def p6():
"""
6) Do Problem 3.4 in Schorghofer
"""
pass
if __name__ == "__main__":
p1()
p2()
p3()
p4()
p5()
p6()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started