January 25th, 2013, 08:32 PM
/*--- PROGRAM USING COSINE FUNCTION ---*/
#define PI 3.1416
#define MAX 180
main ( )
angle = 0;
printf(“ Angle Cos(angle)\n\n”);
while(angle <= MAX)
x = (PI/MAX)*angle;
y = cos(x);
printf(“%15d %13.4f\n”, angle, y);
angle = angle + 10;
In the above program why do we assign x=(PI/MAX) * angle instead of finding the cos (angle) directly??? i.e. angle = 30
cos (angle) is the answer , then why are using cos(x)? Any help would be appreciated...
January 25th, 2013, 09:05 PM
The cos function takes its arguments in radians. The calculation of x is to convert the value in degrees to radians.
This would be covered in most intro to trigonometry courses that are given in the last couple of years of high school or early in college.
If you need To understand more detail there are lots of tutorials. Search for intro trig radians and you are bound to find something.
January 25th, 2013, 10:57 PM
A common way to generate the cos is with a Maclaurin series, which is a special form of a Taylor series. Maclaurin series for trig functions are based on radians, not degrees, so trig functions need radians.
You'll cover power series in third-semester calculus. For that matter, throughout calculus we used radians, not degrees.
BTW, tan(45 degrees) is 1.0. 45 degrees is PI/4 radians. You can use these relationships to generate a value for PI:
PI = 4.0 * atan(1.0);
January 26th, 2013, 04:35 AM
If you are writing code that deals extensively with degrees rather than radians, then you might render teh code more readable and maintainable by writing wrappers for teh standard trig functions eg:
Note the definition of PI to 15 significant figures - this is the precision afforded by the double precision floating point data type on most systems - using fewer digits will lead to larger than necessary errors. You should always use the highest precision values possible in calculation and only truncate to the precision needed by the application on presentation of results.
#define PI 3.14159265358979
double dcos( double deg )
return cos( deg * PI) / 180 ) ;
Note also that complex expressions using these "degree trig" wrappers will involve multiple conversions between degrees and radians which will effect both precision and performance which is some cases may be critical. For serious number crunching and scientific applications, you should stick to radians, possibly converting only at the human interface (input and output). In some application domains such as signal processing, radians is the natural and common trig unit in any case.