Question
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:
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 with AI-Powered 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