Exercise 6.1: Quadratic Roots Function

Given: three real values, a, b, and c, and the furmula for the roots of a quadratic equation
x1 = b + (b**2-4ac)**0.5/2a and
x2 = b - (b**2-4ac)**0.5/2a;
Compose: a function of a, b, and c that calculates and returns the roots or some flag values if the roots cannot be calculated.
function quad.roots( a, b, c : real returns real, real )
let denom = 2.0 * a;
    discrim := b * b - 4.0 * a * c;

in  if discrim >= 0.0 then
       b + sqrt( discrim ) / denom,
       b - sqrt( discrim ) / denom
    else -9.99e99, -9.99e99
    end if
end let
end function


Previous Section