Skip to Content
PHP Cookbook
book

PHP Cookbook

by David Sklar, Adam Trachtenberg
November 2002
Intermediate to advanced
640 pages
16h 33m
English
O'Reilly Media, Inc.
Content preview from PHP Cookbook

2.12. 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:

$cosine = rad2deg(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.

See Also

Recipe 2.13 for trig basics; documentation on deg2rad( ) at http://www.php.net/deg2rad and rad2deg( ) at http://www.php.net/rad2deg.

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

PHP Cookbook

PHP Cookbook

Eric A. Mann
PHP Cookbook, 2nd Edition

PHP Cookbook, 2nd Edition

Adam Trachtenberg, David Sklar
PHP Cookbook, 3rd Edition

PHP Cookbook, 3rd Edition

David Sklar, Adam Trachtenberg
Programming PHP

Programming PHP

Rasmus Lerdorf, Kevin Tatroe

Publisher Resources

ISBN: 1565926811Supplemental ContentCatalog PageErrata