Other mathematical conversion functions

number abs ( number value)

float sqrt ( float value)

number pow ( number base, number exponent)

float hypot ( float num1, float num2)

There are several key functions you should know that manipulate numbers in non-trigonometrical ways, of which the most important are abs(), sqrt(), and pow().

Abs() is the most basic function, and returns the absolute value of the parameter you pass to it. By "absolute", I mean that it leaves positive values untouched, and converts negative values into positive values. Thus:

<?php
    abs(50);
    abs(-12);
?>

The first line returns 50, and the second returns 12. Note that abs() can take either integers or floats as its parameter, and returns the same type:

<?php
    abs(50.1);
    abs(-12.5);
?>

The first line returns 50.1, and the second returns 12.5 - as you can see, abs() works with integers and floats smoothly. If you cannot see why you would want to use abs(), consider a PHP script to handle user input for values, such as "How many t-shirts would you like to buy?" While it is possible - and certainly not hard - to write code to check for values equal to or under 0 and issue warnings if appropriate, it is much easier to simply put all quantity input through abs() to make sure it is positive then add the resulting number to the shopping basket.

Moving on, sqrt() is short for square root, and takes just one parameter - the value you wish to calculate the square root of. It is very simple to use:

<?php
    print sqrt(25);
    print sqrt(26);
?>

That will output 5 as the result of line one, then 5.0990195135928 for line two.

Pow() is the other key function in this group of three, and takes two parameters - a base and a power to raise it by. That is, supplying 2 as parameter two will multiply parameter one by itself, supplying 3 will multiply parameter one by itself twice, like this:

<?php
    print pow(10,2); // 100
    print pow(10,3); // 1000
    print pow(10,4); // 10000
    print pow(-10, 4); // 10000
?>

The first three lines show the result of 10 * 10, 10 * 10 * 10, then 10 * 10 * 10 * 10. Notice that on line four we have -10 as the first parameter - this is not a problem, as pow() can handle both positive and negative numbers for the first parameter. However, note that it is converted to a positive number in the result - this is basic mathematical theory, "negative multiplied by negative makes a plus".

Pow() is also capable of handling negative powers for the second parameter, in which case it does not generate minus numbers, it just generates smaller numbers. For example, pow(10, -1) is 0.1, pow(10, -2) is 0.01, pow(10, -3) is 0.001, etc.

There is one "gotcha" here that revolves around passing pow() -1 and 0.5 as its parameters, but this is not a bug in PHP - just in how we have constructed our numbering system. If you try calculating the square root of any negative number, you will see it is impossible - the square root of Y returns a number X that, when multiplied by itself, gives Y again. However, if Y is negative, X would need to be negative also, and as we know a negative number squared (that is, multiplied by itself) becomes a positive number, the square root of Y where Y is a negative number is impossible.

This is important because supplying 0.5 as the second parameter to pow() is the equivalent of calculating the square root of the first parameter, which means that using any negative number as parameter one and 0.5 as the second parameter will generate an error, because negative numbers have no square root.

If all this mathematical theory does not make sense, do not worry - pow() is most often used with positive numbers, e.g. to calculate what 8 * 8 * 8 * 8 * 8 is in one function call.

One other function that may be of use to you now and then, although it is unlikely to be used that often, is hypot(). Hypot() takes two parameters, we will call them X and Y, and returns the value sqrt((X * X) + (Y * Y)). If you are familiar with Pythagoras' theorem, you will see that hypot() is designed to calculate the length of a vector (or the hypotenuse of a triangle, depending on how you look at it).

 

Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Base conversion >>

Previous chapter: Trigonometrical conversion

Jump to:

 

Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.