Hudzilla.org - the homepage of Paul Hudson
Contents > Functions > Mathematics Wish List | Report Bug | About Me ]

4.6.3     Trigonometrical conversion: sin(), cos(), tan(), asin(), acos(), and atan(), deg2rad(), rad2deg()

This is NOT the latest copy of this book; click here for the latest version.

float sin ( float value)

float cos ( float value)

float tan ( float value)

float asin ( float value)

float acos ( float value)

float atan ( float value)

float deg2rad ( float value)

float rad2deg ( float value)

The bulk of the built-in mathematical functions convert one value to another, and are basically lifted from mathematics books - here you can calculate sines and cosines, logarithms, and exponents. There are also a number of functions that convert different bases of numbers, e.g. from decimal to binary, binary to hexadecimal, etc. To make things easier to read, I have split three conversions into three sub-categories, starting with trigonometry in this section.

Calculating the basic trigonometry values of sine, cosine, and tangent is as simple as calling the functions sin(), cos(), and tan(), and calculating arc sine, arc cosine, and arc tangent is done using asin(), acos(), and atan(). All six functions take just one parameter - the first three take the number of radians to calculate the value for, and the last three take the output of the first three and return the number of radians.

Radians, if you were not sure, are calculated as being $degrees multiplied by the mathematical constant Pi (p), then divided by 180. PHP has a function you can call, deg2rad(), that performs the same conversion quickly.

So, here are some examples of these functions in use:

<?php
    $sin1
= sin(10);
    
$sin2 = sin(deg2rad(80));
    
$cos1 = cos(89);
    
$cos2 = cos(deg2rad(9));
?>

As mentioned, there are the functions asin(), acos(), and atan() to invert sin(), cos(), and tan(). That is, if you atan() a number, then tan() it, you will end up with the original number again.

<?php
    $sin1
= sin(deg2rad(80));
    
$asin1 = rad2deg(asin($sin1));
?>

Given that code above, $asin1 will be set to 80. As you can imagine, rad2deg() is the opposite of deg2rad(), and converts radians back into degrees. Now try changing the code to this:

<?php
    $sin1
= sin(deg2rad(80));
    print
$sin1 . "\n";
    
$asin1 = rad2deg(asin(XXXXX));
    print
$asin1;
?>

Where I have marked XXXXX, you should type in the output of print $sin1. I get 0.98480775301221 for sin(deg2rad(80)), so I change the third line to asin( 0.98480775301221) and then check the output. If you have got the default PHP set up and followed properly you should get the result 80.000000000001 in line four, as opposed to 80. Why the discrepancy?

Well, if you do a search for "precision" in your php.ini, you will find that by default it is set to 14, which means that PHP will, by default, display floating-point using 14 points of precision, despite storing it using higher precision. What this means is that when you we get 0.98480775301221 outputted, it has been rounded from the full value, which means when we type the same number back into the script we get a discrepancy.

This also explains why the script before returned 80 as predicted - because PHP passed the full value into the asin() function without rounding.





<< 4.6.2 Randomisation: rand(), mt_rand(), getrandmax(), mt_getrandmax(), srand(), and mt_srand()   4.6.4 Other mathematical conversion functions: abs(), sqrt(), pow, and hypot() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
deathgod - 07 Sep 2008

Makes me wish i concentrated in maths class instead of reading Lord of The Rings :)
Will this ever be useful?
BTW i think i'm going to forget this sooner rather than later so its good to know that this book is here for refernce

A PHP User - 07 Sep 2008

so where ar the convertions? i am interested in converting this product cosWcos2W into summ with cosW and sinW powerd 1!!!

A PHP User - 07 Sep 2008

My head is starting to hurt at this stage of the book :) good though!

A |\| 3 \/\/ 13 | E PHP User - 07 Sep 2008

I didn't understand a word of this :(



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is five plus zero?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow