Saturday 25 February 2012

The cosine & arccosine Functions in C & C++

The <cmath> library in C++ (<math.h> in C) has functions to calculate cosine and arccosine values. The cos function takes an angle measured in radians and returns the cosine value. The acos function takes a cosine 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 cosine?

The cosine of an angle is defined as the ratio of the adjacent side of a right-angle triangle over the hypotenuse.




The graph shows the cosine values from 0 to 360 degrees (0 to 2 pi radians). The range is -1 to 1.

 

Cos and Acos Functions in C/C++.

The sample code below defines several one-dimensional arrays to hold:

fDegAngle[5]            angle in degrees (given value).
fRadAngle[5]            angle in radians (calculated from degrees).
fCosine [5]                the sine value from the C/C++ functions.
fArccosine [5]           the arccosine value from the C/C++ function.
fRevDegAngle[5]      the angle in degree determined from the arccosine value.

The starting point is to define 5 angles in degrees: 0, 90, 180, 270, 360.


Sample Code.

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.
#include <cmath>

// Create constant for PI.
#define PI 3.14159265

...

// cosine function

rtOut->Text = "The cosine and arccosine functions.\n\n";
rtOut->Text += "The range of cosine values is: 1 to -1.\n";
rtOut->Text += "The range of arccosine values is: 0 to PI radians or 0 to 180 degrees.\n";
rtOut->Text += "\n";

int i = 0;
double fCosine[5];
double fArccosine[5];
double fRadAngle[5];
double fDegAngle[5];
double fRevDegAngle[5];

// 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

// Calculations
// 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;

  // cosin(1.570796 rads) = 0
  fCosine[i] = cos(fRadAngle[i]);

  // Returns angle in radians.
  fArccosine[i] = acos(fCosine[i]);

  // Find degree of angle based on arccosine.
  fRevDegAngle[i] = fArccosine[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 cosine of " + fRadAngle[i] + " radians is " + fCosine[i] + ".\n";
  rtOut->Text += "The arccosine of " + fCosine[i] + " is " + fArccosine[i] + " radians.\n";
  rtOut->Text += "The arccosine of " + fCosine[i] + " is " + fRevDegAngle[i] + " degrees.\n";
  rtOut->Text += "\n";

} // for

rtOut->Text += "The small floating point numbers (e.g., 1.79489673696541E-09) are effectively zero.\n";
rtOut->Text += "\n";


Output.

The cosine and arccosine functions.

The range of cosine values is: 1 to -1.
The range of arccosine values is: 0 to PI radians or 0 to 180 degrees.

0 degrees is 0 radians.
The cosine of 0 radians is 1.
The arccosine of 1 is 0 radians.
The arccosine of 1 is 0 degrees.

90 degrees is 1.570796325 radians.
The cosine of 1.570796325 radians is 1.79489673696541E-09.
The arccosine of 1.79489673696541E-09 is 1.570796325 radians.
The arccosine of 1.79489673696541E-09 is 90 degrees.

180 degrees is 3.14159265 radians.
The cosine of 3.14159265 radians is -1.
The arccosine of -1 is 3.14159265358979 radians.
The arccosine of -1 is 180.00000020568 degrees.

270 degrees is 4.712388975 radians.
The cosine of 4.712388975 radians is -5.38468932271781E-09.
The arccosine of -5.38468932271781E-09 is 1.57079633217959 radians.
The arccosine of -5.38468932271781E-09 is 90.00000041136 degrees.

360 degrees is 6.2831853 radians.
The cosine of 6.2831853 radians is 1.
The arccosine of 1 is 0 radians.
The arccosine of 1 is 0 degrees.

The small floating point numbers (e.g., 1.79489673696541E-09) are effectively zero.

C/C++ Standards.

The ISO C Standard specifies separate functions depending on the argument data type (double, float, long double): cos, cosf, cosl, acos, acosf and acosl. The images below show the C standard documentation.

In C++ both the cos and acos functions are overloaded. They accept float, double or long double and return the same data type.



No comments:

Post a Comment