PHP Common Math function Summary _php skill

Source: Internet
Author: User
Tags abs function definition mathematical functions natural logarithm pow

This article summarizes and analyzes the common mathematical functions of PHP. Share to everyone for your reference. The specific analysis is as follows:

ABS () function definition and usage: Returns the absolute value of a number.

Syntax: ABS (x), the code is as follows:

Copy Code code as follows:
$abs =abs (-3.2); $abs =3.2
$abs 2=abs (5); $abs 2=5
$abs 3=abs (-5); $abs 3=5

Ceil () function definition and usage: rounded up to the nearest integer.

Syntax Ceil (x)

Parameters Describe
X Required, a number.

Description: Returns the next integer not less than x, if X has a decimal part, the type returned by Ceil () is still float, because the float value is usually larger than the integer, and the instance code is as follows:

Copy Code code as follows:
Echo ceil (5); 5
echo "<br>";
echo ceil (3.3); 4
echo "<br>";
echo ceil (6.999); 7

The floor () function is rounded down to the nearest integer.

Syntax: Floor (x)

Parameters Describe
X Required, a number.

Note: Returns the next integer less than x, rounding up the decimal part of X, the type returned by floor () is still float, because the float value is usually larger than the integer.

Copy Code code as follows:
Echo Floor (4); 4
echo "<br>";
echo Floor (3.3); 3
echo "<br>";
echo Floor (6.999); 6

Definitions and usage

The Fmod () function returns the floating-point number remainder of a division.

Syntax: Fmod (x,y)

Parameters Describe
X Required, a number.
Y Required, a number.

Note: Returns the floating-point number remainder of the divisor (x) divided by the divisor (y). The remainder (r) is defined as: x = i * y + R, where I is an integer, and if Y is a non-0 value, the symbol for R and X is the same and the quantity value is less than Y, and the code is as follows:

Copy Code code as follows:
$x = 4.7; Define value 1
$y = 1.3; Define Value 2
$r =fmod ($x, $y); To perform the remainder operation
echo $x. " Divided by ". $y."   The remainder of the floating-point number is: ". $r; Output results

LOG10 () with a 10-base logarithm.

Syntax: log10 (x)

Parameters Describe
X Required, a number.

Note: Returns the logarithm of the parameter x with a 10 base, the following code:

Copy Code code as follows:
$num 1=100;
$num 2=1000;
$num 3=3;
$result 1=log10 ($num 1);
$result 2=log10 ($num 2);
$result 3=log10 ($num 3);
echo "$num 1 with a logarithm of 10 as the base $RESULT1";
echo "<br>";
echo "$num 2 with a logarithm of 10 as the base $RESULT2";
echo "<br>";
echo "$num 3 with a logarithm of 10 as the base $RESULT3";
echo "<br>";

Log () returns the natural logarithm.

Syntax: Log (x,base)

Parameters Describe
X Required, a number.

Base optional, if this parameter is specified, return logbasex.

Note: If an optional parameter Base,log () is specified to return logbasex, log () returns the natural logarithm of the parameter x, the instance code is as follows:

Copy Code code as follows:
echo log (2.7183); Returns the natural logarithm of a specified number
echo "<br/>";
echo log (2); Returns the natural logarithm of a specified number
echo "<br/>";
echo log (1); Returns the natural logarithm of a specified number
echo "<br/>";
echo log (0); Returns the natural logarithm of a specified number
echo "<br/>";
Echo log (-1); Returns the natural logarithm of a specified number
//
echo sqrt (9); Output 3
echo "<br>";
echo sqrt (10); 3.16227766..
//
Var_dump (POW (2, 8)); Output 256
echo "<br>";
Echo Pow ( -1,20); Output 1
echo "<br>";
Echo Pow (0,0); Output 1
echo "<br>";
Echo Pow (-1, 4.5); return error

I hope this article will help you with your PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.