Friday, June 22, 2012

Trigonometric and Hyperbolic Functions in ANSI C

To use trigonometric and hyperbolic functions in ANSI C, you will need to include the mathematics header:
#include<math.h>
1. Trigonometric Functions
The mathematics header provides support for most trigonometric functions.

The non-inverse trigonometric functions implemented in the mathematics header are: cos, sin and tan. Using these functions you can easily implement cot, sec and csc.

Also, in order to receive the results you want, the value of the angle you send should be specified in radians, not degrees.
Example:
 /*The value of angle in radians*/
 double valInRadians = 3.14/2.0;
 /*sin(val)*/
 double valSin  = sin(valInRadians);
 /*cos(val)*/
 double valCos  = cos(valInRadians);
 /*tan(val)*/
 double valTan  = tan(valInRadians);
 /*ctan(val)*/
 double valCtan = 1/valTan;
 /*sec(val)*/
 double valSec  = 1/valCos;
 /*csc(val)*/
 double valCsc  = 1/valSin;
2. Inverse Trigonometric Functions
The mathematics header also provides support for most inverse trigonometric functions. The same rules apply as in the case of trigonometric functions (the values which are returned are in radians).

Example:
 #define PI 3.1417
 double value  = -0.5;
 double value2 =  0.5;
 /* arcsin(val)
  * val must be in the range [-1, 1]*/
 double valArcsin = asin(value);
 /* arccos(val)
  * val must be in the range [-1, 1]*/
 double valArccos = acos(value);
 /* arctan(val) */
 double valAtan   = atan(value);
 /* arctan(val1, val2) */
 double valAtan2  = atan2(value,value2);
 /* arccot(val) */
 double valArccot = PI/2 - atan(value);
 /* arcsec(val) */
 double valArcsec = acos(1.0/value);
 /* arccsc(val) */
 double valArccsc = asin(1.0/value);
3. Hyperbolic functions
The mathematics header also provides support for most hyperbolic functions. The same rules apply as in the case of trigonometric functions.

Example:
 /*The value of the hyperbolic angle*/
 double value = 0.5;
 /*sinh(val)*/
 double valSinh  = sinh(value);
 /*cosh(val)*/
 double valCosh  = cosh(value);
 /*tanh(val)*/
 double valTanh  = tanh(value);
 /*ctanh(val)*/
 double valCtanh = 1/valTanh;
 /*sech(val)*/
 double valSech  = 1/valCosh;
 /*csch(val)*/
 double valCsch  = 1/valSinh;
4. Inverse Hyperbolic functions
The mathematics header also provides support for most inverse hyperbolic functions. The same rules apply as in the case of trigonometric functions.

Example:
 double value  = -0.5;
 /* arcsinh(val)*/
 double valArcsinh = asinh(value);
 /* arccosh(val) */
 double valArccosh = acosh(value);
 /* arctanh(val) */
 double valAtanh   = atanh(value);
 /* arccoth(val) */
 double valArccoth = atanh(1/value);
 /* arcsech(val) */
 double valArcsech = acosh(1.0/value);
 /* arccsch(val) */
 double valArccsch = asinh(1.0/value);
Note:
All compilers compliant with C99 should have functions that allow you to use the float and long double data type in the same way we used double.

All functions that operate with float will have have the same name as their double counterparts plus the -f suffix
Example: float sinf(float arg), float atanhf(float arg)

All functions that operate with long double will have have the same name as their double counterparts plus the -l suffix
Example: long double sinl(long double arg), long double atanhl(long double arg)

Mathematical notions:

2 comments:

  1. Very well discussion about trigonometry, inverse trigonometry and Hyperbolic functions, Hyperbolic functions occur in the results of some linear differential equations, for example the equation defining a catenary, of some cubic equations, and of Laplace's equation in Cartesian coordinates.The hyperbolic functions take real values for a real argument called a hyperbolic angle. In complex analysis, they are rational functions of exponentials and meromorphic.

    ReplyDelete

Got a question regarding something in the article? Leave me a comment and I will get back at you as soon as I can!

Related Posts Plugin for WordPress, Blogger...
Recommended Post Slide Out For Blogger