Solutions of a quadratic equation

From Director Online Wiki
Jump to: navigation, search

Copy and paste on a movie script:


----------------------------------------
on quadraticEqSolutions a, b, c
  -- a, b, c are floats or integers, the parameters of a quadratic equation (ax2 + bx + c = 0)
  -- returns:
  --    void if no real solution
  --    #float if double solution
  --    #list of 2 floats if 2 different solution
  -- by Iñigo de Gracia
  ----------------------------------------
  if a = 0 then 
    if b = 0 then return void
    else return - c/ float (b)
  end if
  -- a <> 0
  a = float (a) -- work with floats
  discrim = b * b - 4 * a * c
  if discrim < 0 then return void
  if discrim = 0 then 
    if a <> 0 then return - b / 2 * a
    else return void
  end if
  sqr = sqrt (discrim)
  s1 = - (b + sqr) / (2 * a)
  s2 = - (b - sqr) / (2 * a)
  return [s1, s2]
end

--Luna1999 09:30, 26 September 2008 (UTC)