CFLib.org – Common Function Library Project

ComplexNumAdd(First, Second)

Last updated November 15, 2001

Version: 1 | Requires: ColdFusion 5 | Library: MathLib

 
Rated 0 time(s). Average Rating: 0

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:

view plain print about
<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:

view plain print about
<cfscript>
/**
 * 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;
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Dave Anderson Dave Anderson added
iniToStruct
20 day(s) ago

Dave Anderson Dave Anderson added
deDupeArray
20 day(s) ago

Richard Richard added
dice
22 day(s) ago

Isaac Dealey Isaac Dealey added
getRelative
a while ago

Top Rated

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 22 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Raymond Camden highlightAndCrop
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson