PHP Numbers
Calculating Trigonometric Functions
Problem
You want to use trigonometric functions, such as sine, cosine, and tangent.Solution
PHP supports many trigonometric functions natively: sin(), cos(), and tan():
$result = cos(2 * M_PI);
You can also use their inverses: asin(), acos(), and atan():
// arctan of pi/4 is about 0.665773
$result = atan(M_PI / 4);
Discussion
These functions assume all angles are in radians, not degrees.The function atan2() takes two variables $x and $y, and computes atan($x/$y). However, it always returns the correct sign because it uses both parameters when finding the quadrant of the result.
For secant, cosecant, and cotangent, you should manually calculate the reciprocal values of sin(), cos(), and tan():
$n = 0.707;
// secant of 0.707 is about 1.53951
$secant = 1 / sin($n);
// cosecant of 0.707 is about 1.31524
$cosecant = 1 / cos($n);
// cotangent of 0.707 is about 1.17051
$cotangent = 1 / tan($n);
You can also use hyperbolic functions: sinh(), cosh(), and tanh(), plus, of course, asinh(), acosh(), and atanh(). The inverse functions, however, aren’t supported on Windows for PHP versions before 5.3.0.
No comments:
Post a Comment