title besides title

 

Saturday, November 24, 2012

PHP : Numbers - [2.13] Doing Trigonometry in Degrees, not Radians

2.13.1 Problem

You have numbers in degrees but want to use the trigonometric functions.

2.13.2 Solution

Use deg2rad( ) and rad2deg( ) on your input and output:
$cosine = rad2deg(cos(deg2rad($degree)));

2.13.3 Discussion

By definition, 360 degrees is equal to 2figs/U03C0.gif radians, so it's easy to manually convert between the two formats. However, these functions use PHP's internal value of figs/U03C0.gif, so you're assured a high-precision answer. To access this number for other calculations, use the constant M_PI, which is 3.14159265358979323846.
There is no built-in support for gradians. This is considered a feature, not a bug.