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;
}

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