• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Mathematical Functions

Mathematical Functions

You may not need to do much mathematical computation as part of your ColdFusion applications, but CFML includes a large library of math functions for you to use when the need arises. Some of them include the following:

  • Abs(number)— Returns the absolute value of number. The output from the following example would be 7:

    <cfoutput>#Abs(-7)#</cfoutput>
    						
  • Ceiling(number)— Returns the closest integer bigger than number. The following example would output the value 3:

    <cfoutput>#Ceiling(2.01)#</cfoutput>
    						
  • DecrementValue(number)— Subtracts one from the value of number. In the following example, the function would return 9:

    <cfoutput>#DecrementValue(10)#</cfoutput>
    						
  • IncrementValue(number)— Adds one to the value of number. The following code would return the value 11:

    <cfoutput>#IncrementValue(10)#</cfoutput>
    						
  • Int(number)— Returns the closest integer smaller than number. The following example would print the value 5:

    <cfoutput>#Int(5.89)#</cfoutput>
    						
  • Log(number)— Returns the natural logarithm (base e where e is approximately 2.718) of number. The following code would return a value of approximately 4.605:

    <cfoutput>#Log(100)#</cfoutput>
    						
  • Log10(number)— Returns the base-10 logarithm of number. The following code would return a value of 2:

    <cfoutput>#Log10(100)#</cfoutput>
    						
  • Max(number1,number2)— Returns the larger of number1 and number2. The following example would return the value 5:

    <cfoutput>#Max(-3,5)#</cfoutput>
    						
  • Min(number1,number2)— Returns the smaller of number1 and number2. The following example would return the value -3:

    <cfoutput>#Min(-3,5)#</cfoutput>
    						
  • pi()— Returns the value of the constant pi, 3.14159265358979. pi represents the ratio of the circumference of a circle to its diameter. For example:

    <cfset areaOfCircle = pi() * radius * radius> 
    
  • Rand()— Returns a random number between 0 and 1. For best results, use Rand() only after you have seeded the random number generator with Randomize(). For example:

    <cfset temp = Randomize(Second(Now()))> 
    <cfset probabilityValue = Rand()>
    						
  • Randomize(seed)— Activates ColdFusion's random number generation algorithm with the starter value seed. Returns TRUE if successful. Many developers use a value from the system clock to seed the random number generator as shown in the following example:

    <cfset temp = Randomize(Second(Now()))> 
    
  • RandRange(number1,number2)— Returns a random number between number1 and number2. For best results, use RandRange() only after you have seeded the random number generator with Randomize(). For example:

    <cfset temp = Randomize(Second(Now()))> 
    <cfset diceRoll = RandRange(1,6)>
    						
  • Round(number)— Rounds number to the closest integer. The following code would output the value 9:

    <cfoutput>#Round(9.49)#</cfoutput>
    						
  • Sgn(number)— Returns -1 if number is negative, 0 if number is zero, and 1 if number is positive. The output from the following example would be -1:

    <cfoutput>#Sgn(-4)#</cfoutput> 
    
  • Sqr(number)— Returns the positive square root of number, where number is positive or zero. In the following example, the output would be 3:

    <cfoutput>#Sqr(9)#</cfoutput>
    						

    TIP

    To get the other square root of a number, multiply the positive square root by -1.



PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint