Skip to content

Instantly share code, notes, and snippets.

@natebenes
Created March 11, 2010 01:50
Show Gist options
  • Save natebenes/328710 to your computer and use it in GitHub Desktop.
Save natebenes/328710 to your computer and use it in GitHub Desktop.
SUBROUTINE quadraticFormula(a,b,c,root1,root2,success)
IMPLICIT NONE
! Programmer: Nate Benes
! Purpose: Subroutine to calculate the real roots of a function
REAL, INTENT(IN) :: a,b,c ! Store the inputs of the function
REAL, INTENT(OUT) :: root1, root2 ! Store the outputs of the function
LOGICAL, INTENT(OUT) :: success ! variable to indicate whether we can find the root
REAL :: discriminant
discriminant = b**2-4*a*c
! Do some basic validation
IF((a == 0) .OR. (discriminant < 0)) THEN ! We can't solve this function
success = .FALSE.
WRITE(*,*) "Program either has complex roots or is not quadratic."
ELSE
root1 = (-b + SQRT(discriminant))*0.5/a
root2 = (-b - SQRT(discriminant))*0.5/a
END IF
END SUBROUTINE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment