The <cmath> library in C++ (<math.h> in C) has functions to calculate tangent and arctangent values. The tan function takes an angle measured in radians and returns the tangent value. The atan function takes a tangent value and returns an angle in radians. Both functions use floating-point precision numbers.
Click here for a quick refresher on degrees to radians.
But what is tangent?
The tangent of an angle is defined as the ratio of the opposite side of a right-angle triangle over the adjacent side.
The graph shows the tangent values from -180 to 180 degrees (-pi/2 to pi/2 radians). The range is all real numbers.
Tan and Atan Functions in C/C++.
The sample code below defines several one-dimensional arrays to hold:
fDegAngle angle in degrees (given value).
fRadAngle angle in radians (calculated from degrees).
fTangent the tangent value from the C/C++ functions.
fArcTangent the arctangent value from the C/C++ function.
fRevDegAngle the angle in degree determined from the arctangent value.
The starting point is to define 5 angles in degrees: 0, 90, 180, 270, 360.
The sample code was created in MS Visual C++ 2010 as a Windows Form Application. It contains a form (Form1) that displays when the program runs. The sample code, contained in the Form Load Event, executes and adds text to a RichTextBox named rtOut.
// Math functions library.
// Create constant for PI.
#define PI 3.14159265
// tan & atan functions
rtOut->Text = "The tan and atan functions.\n\n";
rtOut->Text += "The range of tangent values is: all real numbers.\n";
rtOut->Text += "The range of arcsine values is: -PI/2 to PI/2 radians or -90 to 90 degrees.\n";
rtOut->Text += "\n";
int i = 0;
// 360 degrees = 2 x PI x radians
// 180 degrees = PI x radians
// 1 degree = PI / 180 radians
// 90 degree = 90 x PI / 180 radians = 1.570796
// 1 radian = 180 / PI degree
// Loop through for degrees: 0, 90, 180, 270, 360.
for (i; i < 5; i++)
// Set degrees.
fDegAngle[i] = 90 * i;
// Need radians.
fRadAngle[i] = fDegAngle[i] * PI / 180.0;
// tan(0.785398 rads) = 1
// pi/4 or 45 degrees
fTangent[i] = tan(fRadAngle[i]);
// Returns angle in radians.
fArctangent[i] = atan(fTangent[i]);
// Find degree of angle based on arctangent.
fRevDegAngle[i] = fArctangent[i] * 180 / PI;
} // for
// Display values.
for (i = 0; i < 5; i++)
rtOut->Text += fDegAngle[i] + " degrees is " + fRadAngle[i] + " radians.\n";
rtOut->Text += "The tangent of " + fRadAngle[i] + " radians is " + fTangent[i] + ".\n";
rtOut->Text += "The arctangent of " + fTangent[i] + " is " + fArctangent[i] + " radians.\n";
rtOut->Text += "The arctangent of " + fTangent[i] + " is " + fRevDegAngle[i] + " degrees.\n";
rtOut->Text += "\n";
} // for
The tan and atan functions.
The range of tangent values is: all real numbers.
The range of arcsine values is: -PI/2 to PI/2 radians or -90 to 90 degrees.
0 degrees is 0 radians.
The tangent of 0 radians is 0.
The arctangent of 0 is 0 radians.
The arctangent of 0 is 0 degrees.
90 degrees is 1.570796325 radians.
The tangent of 1.570796325 radians is 557135115.020977.
The arctangent of 557135115.020977 is 1.570796325 radians.
The arctangent of 557135115.020977 is 90 degrees.
180 degrees is 3.14159265 radians.
The tangent of 3.14159265 radians is -3.58979347393082E-09.
The arctangent of -3.58979347393082E-09 is -3.58979347393082E-09 radians.
The arctangent of -3.58979347393082E-09 is -2.05680015614866E-07 degrees.
270 degrees is 4.712388975 radians.
The tangent of 4.712388975 radians is 185711735.639238.
The arctangent of 185711735.639238 is 1.57079632141021 radians.
The arctangent of 185711735.639238 is 89.99999979432 degrees.
360 degrees is 6.2831853 radians.
The tangent of 6.2831853 radians is -7.17958694786164E-09.
The arctangent of -7.17958694786164E-09 is -7.17958694786164E-09 radians.
The arctangent of -7.17958694786164E-09 is -4.11360031229732E-07 degrees.
The ISO C Standard specifies separate functions depending on the argument data type (double, float, long double): tan, tanf, tanl, atan, atanf and atanl. The images below show the C standard documentation.
In C++ both the tan and atan functions are overloaded. They accept float, double or long double and return the same data type.