Common php mathematical functions

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

Definition and usage
The abs () function returns the absolute value of a number.

Syntax
Abs (x)
*/
$ Abs = abs (-3.2); // $ abs = 3.2
$ Abs2 = abs (5); // $ abs2 = 5
$ Abs3 = abs (-5); // $ abs3 = 5

/*

Definition and usage
The ceil () function rounds up to the nearest integer.

Syntax
Ceil (x) parameter description
X is required. A number.

Description
Returns the next integer of no less than x. If x has a decimal part, the return value is one digit. The type returned by ceil () is still float, because the float value range is usually larger than integer.

*/


Echo ceil (5); // 5
Echo "<br> ";
Echo ceil (3.3); // 4
Echo "<br> ";
Echo ceil (6.999); // 7

/*
The floor () function rounds down to the nearest integer.

Syntax
Floor (x) parameter description
X is required. A number.

Description
Returns the next integer not greater than x and rounds the decimal part of x. The return type of floor () is still float, because the float value range is usually larger than that of integer.

*/

Echo floor (4); // 4
Echo "<br> ";
Echo floor (3.3); // 3
Echo "<br> ";
Echo floor (6.999); // 6

/*
Definition and usage
The fmod () function returns the remainder of the floating point number of the division.

Syntax
Fmod (x, y) parameter description
X is required. A number.
Y is required. A number.

Description
Returns the remainder of a floating point number obtained by dividing divide by divisor (y. The remainder (r) is defined as x = I * y + r, where I is an integer. If y is a non-zero value, the symbols of r and x are the same and the number of values is less than y.

*/

$ X = 4.7; // defines the value 1.
$ Y = 1.3; // defines the value 2.
$ R = fmod ($ x, $ y); // perform the remainder operation.
The remainder of echo $ x. "divided by". $ y. "is:". $ r; // output result

/*
The base-10 logarithm of log10.

Syntax
Log10 (x) parameter description
X is required. A number.

Description
Returns the base-10 logarithm of parameter x.

*/

$ Num1 = 100;
$ Num2 = 1000;
$ Num3 = 3;
$ Result1 = log10 ($ num1 );
$ Result2 = log10 ($ num2 );
$ Result3 = log10 ($ num3 );
Echo "$ num1 base-10 logarithm $ result1 ";
Echo "<br> ";
Echo "$ num2 base-10 logarithm $ result2 ";
Echo "<br> ";
Echo "$ num3 base-10 logarithm $ result3 ";
Echo "<br> ";

/*
Log () returns the natural logarithm.

Syntax
Log (x, base) parameter description
X is required. A number.
Base is optional. If this parameter is specified, logbasex is returned.

Description
If the optional parameter base is specified, log () returns logbasex; otherwise, log () returns the natural logarithm of parameter x.


*/

Echo log (2.7183); // returns the natural logarithm of the specified value.
Echo "<br/> ";
Echo log (2); // returns the natural logarithm of the specified value.
Echo "<br/> ";
Echo log (1); // returns the natural logarithm of a specified value.
Echo "<br/> ";
Echo log (0); // returns the natural logarithm of the specified value.
Echo "<br/> ";
Echo log (-1); // returns the natural logarithm of the specified value.

//

Echo sqrt (9); // output 3
Echo "<br> ";
Echo sqrt (10); // 16227766...

//

Var_dump (pow (2, 8); // 256 output
Echo "<br> ";
Echo pow (-1, 20); // output 1
Echo "<br> ";
Echo pow (0, 0); // output 1
Echo "<br> ";
Echo pow (-1, 4.5); // An error is returned.

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.