CFLib.org – Common Function Library Project

Quadratic(a, b, c)

Last updated July 18, 2001

author

Joel Cox

Version: 1 | Requires: CF5 | Library: MathLib

Description:
Returns a structure containing the two real roots of a polynomial in the form: ax^2 + bx + c = 0

Return Values:
Returns a structure.

Example:

<CFSET a = 1>
  <CFSET b = -3>
  <CFSET c = -4>
  <CFSET Quadratic=#Quadratic(a, b, c)#> 

  <CFOUTPUT>
  Given a = 1, b = -3, c = -4
  The first root is #Quadratic.Root1#
  The second root is #Quadratic.Root2#
  </CFOUTPUT> 

<P>
Here's the Quadratic structure:
<P>
<CFDUMP VAR="#Quadratic#">

Parameters:

Name Description Required
a Coefficient of x^2 term Yes
b Coefficient of x term Yes
c Constant Yes

Full UDF Source:

/**
 * Returns the two real roots of a polynomial in the form: ax^2 + bx + c = 0
 * 
 * @param a      Coefficient of x^2 term 
 * @param b      Coefficient of x term 
 * @param c      Constant 
 * @return Returns a structure. 
 * @author Joel Cox (jlcox@goodyear.com) 
 * @version 1, July 18, 2001 
 */
function Quadratic(a, b, c)
{
  Var mRoots = StructNew();
  Var first = 0;
  Var second = 0;
  Var denom = 2 * a;
  Var arg1 = b^2 - 4 * a * c;
  if (arg1 LT 0) {
    mRoots["Root1"] = "not real";
      mRoots["Root2"] = "not real";
  }
  else {   
      first = -b / denom;
      second = Sqr(arg1) / denom;
      mRoots["Root1"] = first + second;
      mRoots["Root2"] = first - second; 
  }
  Return mRoots;
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson