The <cmath> library in C++ (<math.h> in C) has functions to calculate sine and arcsine values. The sin function takes an angle measured in radians and returns the sine value. The asin function takes a sine value and returns an angle in radians. Both functions use floating-point precision numbers.
Background: Trigonometry.
Back to high school for a minute in case you aren’t sure about radians.
Normally we think of angles in degrees such as a right-angle being 90 degrees or a circle with its 360 degrees. A radian is another measure of angles. The definition of a radian is the angle in a circle where its arc is equal to the radius. As the image shows, 1 radian has an angle theta such that 1 radius = arc.
What is the relationship between degrees and radians? It starts with the formula for calculating the circumference of a circle: 2 pi r. The circumference, C, is the entire circle of 360 degrees while 1 radian has an arc of 1 radius. Since C = 2 pi r, there’s 2 pi radians in a circle.
Since we know 2 pi rads is the same as 360 degrees, we can move back and forth between the two.
But what is sine?
The sine of an angle is defined as the ratio of the opposite side of a right-angle triangle over the hypotenuse.
The graph shows the sine values from 0 to 360 degrees (0 to 2 pi radians). The range is -1 to 1.
Sin and Asin 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).
fSine[5] the sine value from the C/C++ functions.
fArcsine[5] the arcsine value from the C/C++ function.
fRevDegAngle[5] the angle in degree determined from the arcsine 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
...
rtOut->Text = "The sine and arcsine functions.\n\n";
rtOut->Text += "The range of sine values is: -1 to 1.\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;
double fSine[5];
double fArcsine[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;
// sin(1.570796 rads) = 1
fSine[i] = sin(fRadAngle[i]);
// Returns angle in radians.
fArcsine[i] = asin(fSine[i]);
// Find degree of angle based on arcsine.
fRevDegAngle[i] = fArcsine[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 sine of " + fRadAngle[i] + " radians is " + fSine[i] + ".\n";
rtOut->Text += "The arcsine of " + fSine[i] + " is " + fArcsine[i] + " radians.\n";
rtOut->Text += "The arcsine of " + fSine[i] + " is " + fRevDegAngle[i] + " degrees.\n";
rtOut->Text += "\n";
} // for
rtOut->Text += "The small floating point numbers (e.g., 3.58979347393082E-09) are effectively zero.\n";
rtOut->Text += "\n";
Output.
The sine and arcsine functions.
The range of sine values is: -1 to 1.
The range of arcsine values is: -PI/2 to PI/2 radians or -90 to 90 degrees.
0 degrees is 0 radians.
The sine of 0 radians is 0.
The arcsine of 0 is 0 radians.
The arcsine of 0 is 0 degrees.
90 degrees is 1.570796325 radians.
The sine of 1.570796325 radians is 1.
The arcsine of 1 is 1.5707963267949 radians.
The arcsine of 1 is 90.00000010284 degrees.
180 degrees is 3.14159265 radians.
The sine of 3.14159265 radians is 3.58979347393082E-09.
The arcsine of 3.58979347393082E-09 is 3.58979347393082E-09 radians.
The arcsine of 3.58979347393082E-09 is 2.05680015614866E-07 degrees.
270 degrees is 4.712388975 radians.
The sine of 4.712388975 radians is -1.
The arcsine of -1 is -1.5707963267949 radians.
The arcsine of -1 is -90.00000010284 degrees.
360 degrees is 6.2831853 radians.
The sine of 6.2831853 radians is -7.17958694786164E-09.
The arcsine of -7.17958694786164E-09 is -7.17958694786164E-09 radians.
The arcsine of -7.17958694786164E-09 is -4.11360031229732E-07 degrees.
The small floating point numbers (e.g., 3.58979347393082E-09) are effectively zero.
C/C++ Standards.
The ISO C Standard specifies separate functions depending on the argument data type (double, float, long double): sin, sinf, sinl, asin, asinf and asinl. The images below show the C standard documentation.
In C++ both the sin and asin functions are overloaded. They accept float, double or long double and return the same data type.
No comments:
Post a Comment