init repo
This commit is contained in:
BIN
chapter-03/interval/__pycache__/interval.cpython-313.pyc
Normal file
BIN
chapter-03/interval/__pycache__/interval.cpython-313.pyc
Normal file
Binary file not shown.
25
chapter-03/interval/interval.py
Normal file
25
chapter-03/interval/interval.py
Normal file
@@ -0,0 +1,25 @@
|
||||
class REAL: # nested.py
|
||||
def __init__(self, l, u):
|
||||
self.lower = l
|
||||
self.upper = u
|
||||
|
||||
def asString(self, n):
|
||||
l = self.lower(n)
|
||||
u = self.upper(n)
|
||||
return ("[ " + l.numerator.digits() + "/" + l.denominator.digits() +
|
||||
", " + u.numerator.digits() + "/" + u.denominator.digits() + "]")
|
||||
|
||||
def __add__(self, y):
|
||||
return REAL_add(self, y)
|
||||
|
||||
|
||||
class REAL_add(REAL):
|
||||
def __init__(self, x, y):
|
||||
self.x = x
|
||||
self.y = y
|
||||
|
||||
def lower(self, n):
|
||||
return self.x.lower(n) + self.y.lower(n)
|
||||
|
||||
def upper(self, n):
|
||||
return self.x.upper(n) + self.y.upper(n)
|
17
chapter-03/interval/main.py
Normal file
17
chapter-03/interval/main.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from interval import REAL
|
||||
from gmpy2 import mpq # rationale Zahlen
|
||||
|
||||
|
||||
def lower_one(n):
|
||||
return mpq(1,1)-mpq(1,n)
|
||||
def upper_one(n):
|
||||
return mpq(1,1)+mpq(1,n)
|
||||
|
||||
x = REAL(lower_one,upper_one); y = x + x
|
||||
|
||||
print ("x: "+x.asString(10))
|
||||
print ("y: "+y.asString(10))
|
||||
|
||||
|
||||
print ("x: "+x.asString(10000))
|
||||
print ("y: "+y.asString(10000))
|
Reference in New Issue
Block a user