Common php mathematical functions

Source: Internet
Author: User
Tags mathematical functions natural logarithm
Definitions and usage of common php functions: abs () returns the absolute value of a number.

Syntax: abs (x). The code is as follows:

$ 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. it is a number.

Note: the return value is the next integer not less than x. If x has a decimal part, the return value of ceil () is still float, because the float value range is usually larger than integer, the instance code is as follows:

Echo ceil (5); // 5

Echo"
";

Echo ceil (3.3); // 4

Echo"
";

Echo ceil (6.999); // 7

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

Syntax: floor (x)

Parameter description

X is required. it is a number.

Note: If the return value is not greater than the next integer of x, the fractional part of x is rounded up, and the type returned by floor () is still float, because the float value range is usually larger than integer.

Echo floor (4); // 4

Echo"
";

Echo floor (3.3); // 3

Echo"
";

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. it is a number.

Y is required. it is a number.

Description: return the remainder of the floating point number obtained by dividing the devisor (x) by the devisor (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. the code is as follows:

$ 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. it is a number.

Description: returns the base-10 logarithm of parameter x. The code is as follows:

$ Num1 = 100;

$ Num2 = 1000;

$ Num3 = 3;

$ Result1 = log10 ($ num1 );

$ Result2 = log10 ($ num2 );

$ Result3 = log10 ($ num3 );

Echo "$ num1 base-10 logarithm $ result1 ";

Echo"
";

Echo "$ num2 base-10 logarithm $ result2 ";

Echo"
";

Echo "$ num3 base-10 logarithm $ result3 ";

Echo"
";

Log () returns the natural logarithm.

Syntax: log (x, base)

Parameter description

X is required. it is a number.

Base (optional). If this parameter is specified, logbasex is returned.

Note: if the optional parameter base is specified, log () returns logbasex; otherwise, log () returns the natural logarithm of parameter x. The instance code is as follows:

Echo log (2.7183); // returns the natural logarithm of the specified value.

Echo"
";

Echo log (2); // returns the natural logarithm of the specified value.

Echo"
";

Echo log (1); // returns the natural logarithm of a specified value.

Echo"
";

Echo log (0); // returns the natural logarithm of the specified value.

Echo"
";

Echo log (-1); // returns the natural logarithm of the specified value.

//

Echo sqrt (9); // output 3

Echo"
";

Echo sqrt (10); // 16227766...

// Open source code phpfensi.com

//

Var_dump (pow (2, 8); // 256 output

Echo"
";

Echo pow (-1, 20); // output 1

Echo"
";

Echo pow (0, 0); // output 1

Echo"
";

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.