Skip to content

Commit a3451c2

Browse files
committed
This is the working version of the snafu program. Don't peek
1 parent 946a257 commit a3451c2

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

week-02/snafu_3.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#! /usr/bin/env python
2+
#
3+
# snafu.py
4+
# This program is hopelessly screwed up - it is an exercise in using the
5+
# debugger
6+
#
7+
#
8+
import cmath
9+
import pdb
10+
pdb.set_trace() # for ipython, cpython doesn't need it
11+
12+
def discriminant(A, B, C) :
13+
"""Return the discriminant of the quadratic: B^2-4AC"""
14+
r = cmath.sqrt(B*B - 4*A*C)
15+
return r
16+
17+
def abs_err(a, b):
18+
return abs(a-b)
19+
20+
def test_function (A, B, C, x, yt, err_limit ) :
21+
y = A*x*x+B*x+C
22+
assert abs_err(y, yt) <= err_limit,\
23+
"The test function returns an incorrect value %f should be %f" % (y.real, yt)
24+
return
25+
26+
# This is a useless comment
27+
print "Good values to try include 32,16,0, 1,4,3 and 0,16,32"
28+
while True:
29+
A = float(raw_input("Enter A "))
30+
if A != 0.0 :
31+
break
32+
print "Do not enter the value 0.0 for A"
33+
34+
B = float(raw_input("Enter B "))
35+
C = float(raw_input("Enter C "))
36+
37+
d = discriminant(A, B, C)
38+
39+
40+
r1 = (-B + d)/(2*A)
41+
r2 = (-B - d)/(2*A)
42+
test_function( A, B, C, r1, 0.0, 1.0E-15 )
43+
test_function( A, B, C, r2, 0.0, 1.0E-15 )
44+
45+
46+
print "The roots are %s and %s" % ( str(r1), str(r2) )
47+
48+
49+

0 commit comments

Comments
 (0)