[Tutor] faulty code (maths)

David ldl08 at gmx.net
Sun Nov 23 12:37:09 CET 2008


Hello everybody,

I recently came across a book by Prof. Langtangen: Indroduction to 
Computer Programming: http://folk.uio.no/hpl/INF1100/INF1100-ebook-Aug08.pdf

I am trying to solve exercise 1.18 ("Why does the following program not 
work correctly?"), but I don't find the mistake: why does the line

q = sqrt(b*b - 4*a*c)

cause an error? I was playing around with the code, but got nowhere.

Here the entire code:

a = 2; b = 1; c = 2
from math import sqrt
q = sqrt(b*b - 4*a*c)
x1 = (-b + q)/2*a
x2 = (-b - q)/2*a
print x1, x2


Many thanks for a pointer!

David


More information about the Tutor mailing list