Web Database Applications with PHP \& MySQLWeb Database Applications with PHP \& MySQLSearch this book

2.9. Integer and Float Functions

Apart from the basic operators +, -, /, *, and %, PHP provides the usual array of mathematical library functions. In this section, we present some of the library functions that are used with integer and float numbers.

2.9.1. Absolute Value

The absolute value of an integer or a float can be found with the abs( ) function:

integer abs(integer number)
float abs(float number)

The following examples show the result of abs( ) on floats and integers:

echo abs(-1);       // prints 1
echo abs(1);        // prints 1
echo abs(-145.89);  // prints 145.89
echo abs(145.89);   // prints 145.89

2.9.2. Ceiling and Floor

The ceil( ) and floor( ) functions can return the integer value above and below a fractional value, respectively:

float ceil(float value)
float floor(float value)

The return type is a float because an integer may not be able to represent the result when a large value is passed as an argument. Consider the following examples:

echo ceil(27.3);   // prints 28
echo floor(27.3);  // prints 27

2.9.3. Rounding

The round( ) function uses 4/5 rounding rules to round up or down a value to a given precision:

float round(float value [, integer precision])

Rounding by default is to zero decimal places, but the precision can be specified with the optional precision argument. The 4/5 rounding rules determine if a number is rounded up or down based on the digits that are lost due to the rounding precision. For example, 10.4 rounds down to 10, and 10.5 rounds up to 11. The following examples show rounding at various precisions:

echo round(10.4);           // prints 10
echo round(10.5);           // prints 11
echo round(2.40964, 3);     // prints 2.410
echo round(567234.56, -3);  // prints 567000
echo round(567234.56, -4);  // prints 570000

2.9.4. Number Systems

PHP provides the following functions that convert numbers between integer decimal and the commonly used number systems, binary, octal, and hexadecimal:

string decbin(integer number)
integer bindec (string binarystring)
string dechex(integer number)
integer hexdec(string hexstring)
string decoct(integer number)
integer octdec(string octalstring)

The decimal numbers are always treated as integers, and the numbers in the other systems are treated as strings. When converting to decimal, care must be taken that the source number isn't greater than the maximum value an integer can hold. Here are some examples:

echo decbin(45);        // prints "101101"
echo bindec("1001011"); // prints 75
echo dechex(45);        // prints "2D"
echo hexdec("5a7b");    // prints 23163
echo decoct(45);        // prints "55"
echo octdec("777");     // prints 511

2.9.5. Basic Trigonometry Functions

PHP supports the basic set of trigonometry functions and are listed in Table 2-4.

Table 2-4. Trigonometry functions supported by PHP

Function

Description

float sin(float arg)

Sine of arg in radians

float cos(float arg)  

Cosine of arg in radians

float tan(float arg)

Tangent of arg in radians

float asin(float arg)

Arc sine of arg in radians

float acos(float arg)  

Arc cosine of arg in radians

float atan(float arg)

Arc tangent of arg in radians

float atan2(float y, float x)

Arc tangent of x/y where the sign of both arguments determines the quadrant of the result

float pi( )

Returns the value 3.1415926535898

float deg2rad(float arg)

Converts arg degrees to radians

float rad2deg(float arg)

Converts arg radians to degrees

2.9.6. Powers and Logs

The PHP mathematical library includes the exponential and logarithmic functions listed in Table 2-5.

Table 2-5. Exponential and logarithmic functions

Function

Description

float exp(float arg)

e to the power of arg

float pow(float base, number exp)  

Exponential expression base to the power of exp

float sqrt(float arg)

Square root of arg

float log(float arg)

Natural logarithm of arg

float log10(float arg)  

Base-10 logarithm of arg

2.9.7. Random Number Generation

PHP provides the function rand( ), which returns values from a generated sequence of pseudo-random numbers. Well-known algorithms generate sequences that appear to have random behavior but aren't truly random. The srand( ) function seeds the algorithm and needs to be called before the first use of the rand( ) function in a script. Otherwise, the function returns the same numbers each time a script is called. The prototypes of the functions are:

void srand(integer seed)
integer rand( )
integer rand(integer min, integer max)

The srand( ) function is called by passing an integer seed that is usually generated from the current time. When called with no arguments, rand( ) returns a random number between 0 and the value returned by getrandmax( ). When rand( ) is called with two arguments—the min and max values—the returned number is a random number between min and max. Consider this example:

// Generate a seed.
$seed = (float) microtime( ) * 100000000;

// Seed the pseudo-random number generator
srand($seed);

// Generate some random numbers
print rand();      // between 0 and getmaxrand( )
print rand(1, 6);  // between 1 and 6 (inclusive)




Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.