Re: lisp function questions



>>>>> "Pascal" == Pascal Bourguignon <spam@xxxxxxxxxxxxxxxx> writes:

Pascal> Jon Harrop <usenet@xxxxxxxxxxxxxx> writes:
>> dan655t@xxxxxxxxx wrote:
>>> Thank you for the help, I will continue working on this. Also if there
>>> are any other suggestions please feel free to continue posting.
>>
>> I'm no Lisp programmer but:

Pascal> Indeed.


>> 1. The algorithm used to compute the roots of a quadratic is different for
>> different number types. In particular, different forms are used for
>> floating point arithmetic if you want numerical stability.

Pascal> There's no loop so there's no concern about numerical stability.
Pascal> In lisp, arithmetic operators are polymorphic:

Pascal> (defun solve-quadratic (a b c)
Pascal> (if (zerop a)
Pascal> (solve-linear b c)
Pascal> (let ((delta (- (* b b) (* 4 a c))))
Pascal> (delete-duplicates (list (/ (- (- b) (sqrt delta)) 2 a)
Pascal> (/ (+ (- b) (sqrt delta)) 2 a))
Pascal> :test (function =)))))

If the coefficients are real and the roots are real, there is a
problem with numerical stability. If 4*a*c is small, delta will be
close to b^2. When you compute -b+sqrt(delta) (for b > 0) , you'll be
subtracting two numbers that are nearly equal. Thus, most books
suggest computing this second root as c/<first root>. The first root
doesn't have a round-off issue.

Ray
.