CFLib.org – Common Function Library Project

ComplexNumAdd(First, Second)

Last updated November 15, 2001

author

Matthew Walker

Version: 1 | Requires: CF5 | Library: MathLib

Description:
Add two complex numbers that have been stored as a structure using the ComplexNum() UDF. You can also use this function to add a complex and a real number, or even two real numbers. The result is stored in a structure.

Return Values:
Returns a structure.

Example:

<cfset a = ComplexNum(3,4)>
<cfset b = ComplexNum(1,-10)>
<cfset c = ComplexNumAdd(a,b)>
<cfset d = ComplexNumAdd(a,3)>
<cfoutput>
	#ComplexNumToString(a)# + #ComplexNumToString(b)# = #ComplexNumToString(c)#<br>
	#ComplexNumToString(a)# + 3 = #ComplexNumToString(d)#
</cfoutput>

Parameters:

Name Description Required
First Structure containing a complex number or a real number. Yes
Second Structure containing a complex number or a real number. Yes

Full UDF Source:

/**
 * Adds two complex numbers.
 * Note that this function uses complex numbers stored as structures by the ComplexNum() UDF also available in this library.  The ComplexNum() function is also required for this UDF to function.
 * 
 * @param First 	 Structure containing a complex number or a real number. 
 * @param Second 	 Structure containing a complex number or a real number. 
 * @return Returns a structure. 
 * @author Matthew Walker (matthew@electricsheep.co.nz) 
 * @version 1, November 15, 2001 
 */
function ComplexNumAdd(First,Second) {
	var ComplexSum = StructNew();
	var ComplexFirst = 0;
	var ComplexSecond = 0;
	var R = 0;
	var I = 0;
			
	if ( IsStruct(First) )
		ComplexFirst = First;
	else	
		ComplexFirst = ComplexNum(First,0);	
	if ( IsStruct(Second) )
		ComplexSecond = Second;
	else	
		ComplexSecond = ComplexNum(Second,0);
				
	R = ComplexFirst.R + ComplexSecond.R;
	I = ComplexFirst.I + ComplexSecond.I;
	StructInsert(ComplexSum, "R", R);
	StructInsert(ComplexSum, "I", I);
	return ComplexSum;
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson