News flash: I'm going to Australia in February, so this Web site may be switched off at any time.


A quadratic root function in Icon

Here is the function used in the quadratic root solver program:

procedure Quadratic_Roots ( a, b, c )
#--
# Generates the real roots of ax^2+bx+c=0.  Depending on the sign
# of d = b^2 - 4*a*c, there are three cases:
#   1. d < 0: no real roots
#	2. d = 0: one root, -b/2*a
#	3. d > 0: two roots, (-b+sqrt(d))/2*a and (-b-sqrt(d))/2*a
#--
  local d	# Discriminant

  d  :=  b ^ 2 - 4.0 * a * c;		# Compute the discriminant

  if  d > 0  then
  { #-- Two roots
    suspend ( - b + sqrt ( d ) ) / ( 2.0 * a );
    suspend ( - b - sqrt ( d ) ) / ( 2.0 * a );
  } #-- Two roots
  else if  d = 0  then
    suspend  - b / ( 2.0 * a );

  fail;		# No more roots
end # -- quadratic.icn --

Next: Using the basic Icon types
See also: Using the Icon programming language
Previous: A quadratic root solver in Icon

John W. Shipman, john@nmt.edu