PHP Numbers Doing Trigonometry in Degrees, Not Radians - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Numbers Doing Trigonometry in Degrees, Not Radians - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Friday, May 10, 2019

PHP Numbers Doing Trigonometry in Degrees, Not Radians

PHP Numbers




Doing Trigonometry in Degrees, Not Radians


Problem

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

Solution

Use deg2rad() and rad2deg() on your input and output:

             $degree = 90;
             // cosine of 90 degrees is 0
             $cosine = cos(deg2rad($degree));


Discussion

By definition, 360 degrees is equal to 2π radians, so it’s easy to manually convert between the two formats. However, these functions use PHP’s internal value of π, 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.


No comments:

Post a Comment

Post Top Ad