Exercises
Ex. 1 → Write a method simplify
to the class RationalNumber
. This method should return the simplified version of the fraction as a tuple.
Ex. 2 → To provide results with confidence intervals a special calculus, so-called interval arithmetic is introduced in numerical mathematics; (refer to [3, 14]). Define a class called Interval
and provide it with methods for addition, subtraction, division, multiplication, and power (with positive integers only). These operations obey the following rules:
.
Provide this class with methods that allow operations of the type a + I, a I, I + a, I a, where I is an interval and a an integer or float. Convert an integer or float to an interval [a,a]
first. (Hint: you may want to use function decorators for this; (refer to section Function as decorators in Chapter 7, Functions). Furthermore, implement the __contains__
method, which enables you to check if a given number belongs to the interval using the syntax x in I
for an object...