Question: Python class from goody import type_as_str from math import sqrt class Interval: pass if __name__ == '__main__': g = Interval.mid_err(9.8,.05) print(repr(g)) g = Interval.min_max(9.75,9.85) print(repr(g))
Python class





from goody import type_as_str from math import sqrt
class Interval: pass
if __name__ == '__main__': g = Interval.mid_err(9.8,.05) print(repr(g)) g = Interval.min_max(9.75,9.85) print(repr(g)) d = Interval.mid_err(100,1) t = (d/(2*g)).sqrt() print(t,repr(t),t.relative_error())
import driver driver.default_file_name = 'bsc2.txt' # driver.default_show_exception=True # driver.default_show_exception_message=True # driver.default_show_traceback=True driver.driver()
Problem #2 : Interval Class Problem Summary: Write a class that represents and defines operators for Interval numbers, which are represented by a pair nunerie values: int, float or mixed. We use intervals to represent approximate numbers, whose exact value we do not know. For example, in physics we might know that the acceleration produced by the the force of gravity (g at sea level is 9.8 m/s2 +/- .05 m/s2, which we will write as 9. 8(+/-.05) m/s". With this class we can perform meric calculations on Interval objects, which automatically keep track of the precision for each calculated value For example, he formula sqrt (d/ (2*)) comutes the aount oftine it takes for a object at sea level to fall a given distance (d) in a vacuum. Given our approximation for g, and a distance that is 100 (t/-1) m, we can use the Interval class to compute the amount of time it takes for an object to drop this amount as follows, including the precision with which we know the answer Interval. mid-err (9.8, . Ob) = g d - Interval. miderr (100, 1) t (d/ (2*g) ) , sqrt () print(t) So, with g knon +/-. 05 m/s2, and d known +/-1 m, the results print as 2. 2587923826805945 (+/-0. 017056289680373204)) which indicates that the time will be somewhere between about 2.21 and 2.28 seconds, having a relative error of about 7.6%. Note that each Interval ob,ject will storeminimum and maximum value in the interval. So 9.8(1/-.05) is stored as an Interval with a minimum of 9. 75 and a maximum of 9.85. Dctails 1. Dcfine a class named Intcrval in a module named interval. py 2. Defin init method that has two parameters; their arguments specify th minimum and maximum values in the interval respectively. Store them in the self variables min and max. Programmers wi1l not use this method directly to construct Interval ohjects instead they will use the static Interval.min_max and Tnterval.mid erT methods (described below)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
