Kapitel 21
Fehler finden und vermeiden
596
Hinweis: Die quadratische Gleichung x
2
+px+q=0 hat die Lösungen:
x
1
= -p/2 + $$$wurzel$$$(p/2)
2
-q $$$EndeWurzel$$$ und
x
2
= -p/2 - $$$wurzel$$$(p/2)
2
-q $$$EndeWurzel$$$
21.9 Lösung
Skript:
Testaufrufe:
# q_gleichung.py
from math import *
def q_gleichung(p, q):
# Vorbedingung
assert ((p/2)**2 - q) >= 0
x1 = -p/2.0 + sqrt((p/2.0)**2 - q)
x2 = -p/2.0 - sqrt((p/2.0)**2 - q)
# Nachbedingung
assert (x1**2 + p*x1 + q == 0) and (x2**2 + p*x2 + q == 0)
return x1, x2
>>> q_gleichung(-1, -2)
(2.0, -1.0)
>>> q_gleichung (1, 1)
File "<pyshell#1>", line 1, in <module>
q_gleichung(1, 1)
... q_gleichung.py", line 14, in q_gleichung
assert ((p/2)**2 - q) >= 0
AssertionError>>>

Get Python 3 - Lernen und professionell anwenden now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.