Notice: With the launch of Adobe Cookbooks, this site will no longer be accepting new entries or posting new content. Thanks to everyone who submitted content!

How do I perform trigonometric calculations?

ColdFusion provides functions for all of the common trigonometric calculations.

The pi() function returns the mathematical constant Pi, accurate up to 15 digits:

<cfset testVar = pi()>
<cfoutput>#testVar#</cfoutput>
3.14159265359

The sin() function takes an angle (in radians), and returns the sine of the angle (in radians):

<cfset testVar = sin(100)>
<cfoutput>#testVar#</cfoutput>
-0.50636564111

The cos() function takes an angle (in radians), and returns the cosine of the angle (in radians):

<cfset testVar = cos(100)>
<cfoutput>#testVar#</cfoutput>
0.862318872288

The tan() function takes an angle (in radians), and returns the tangent of the angle (in radians):

<cfset testVar = tan(100)>
<cfoutput>#testVar#</cfoutput
-0.587213915157

The asin() function takes a number between -1 and 1, and then returns the arcsine of that number.

<cfset testVar = asin(1)>
<cfoutput>#testVar#</cfoutput>
1.57079632679

The acos() function takes a number between -1 and 1, and then returns the arccosine of that number.

<cfset testVar = acos(1)>
<cfoutput>#testVar#</cfoutput>
0

The atn() function takes a number, and then returns the arctangent of that number.

<cfset testVar = atn(1)>
<cfoutput>#testVar#</cfoutput>
0.785398163397

To convert degrees to radians, multiply degrees by pi/180.

<cfset myDegreesVar = .5>
<cfoutput>Radians = #myDegreesVar * pi()/180#</cfoutput>
Radians = 0.00872664625997

To convert radians to degrees, multiply radians by 180/pi.

<cfset myRadiansVar = .5>
<cfoutput>Degrees = #myRadiansVar * 180/pi()#</cfoutput>
Degrees = 28.6478897565


This question was written by Jeremy Petersen.
It was last updated on January 24, 2006 at 4:44:12 PM EST.

CFML Referenced

Cos()
Sin()
ASin()
<cfoutput>
Tan()
ACos()
Atn()
Pi()

Categories

Numbers

Comments

Comment made by Rick Osborne on January 24, 2006 at 4:01 PM
You may also want to show how you can convert from Degrees to Radians and back.


Comment made by Jeremy Petersen on January 24, 2006 at 4:44 PM
Done.


Comment made by Rob Brooks-Bilson on January 24, 2006 at 5:35 PM
Don't forget that there are almost two dozen additional trig functions that are not native to ColdFusion over at cflib.org:

http://www.cflib.org/library.cfm?ID=6