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.
Next chapter: Other mathematical conversion functions >>
Previous chapter: Randomisation
Home: Table of Contents
Copyright ©2015 Paul Hudson. Follow me: @twostraws.