PHP Math 数学简介

数学 (Math) 函数能处理 integer 和 float 范围内的值。


安装

数学 (Math) 函数是 PHP 核心的组成部分。无需安装即可使用这些函数。


PHP Math 函数

函数 描述
abs() 绝对值。
acos() 反余弦。
acosh() 反双曲余弦。
asin() 反正弦。
asinh() 反双曲正弦。
atan() 反正切。
atan2() 两个参数的反正切。
atanh() 反双曲正切。
base_convert() 在任意进制之间转换数字。
bindec() 把二进制转换为十进制。
ceil() 向上舍入为最接近的整数。
cos() 余弦。
cosh() 双曲余弦。
decbin() 把十进制转换为二进制。
dechex() 把十进制转换为十六进制。
decoct() 把十进制转换为八进制。
deg2rad() 将角度转换为弧度。
exp() 返回 Ex 的值。
expm1() 返回 Ex - 1 的值。
floor() 向下舍入为最接近的整数。
fmod() 返回除法的浮点数余数。
getrandmax() 显示随机数最大的可能值。
hexdec() 把十六进制转换为十进制。
hypot() 计算直角三角形的斜边长度。
intdiv() Performs integer division
is_finite() 判断是否为有限值。
is_infinite() 判断是否为无限值。
is_nan() 判断是否为合法数值。
lcg_value() 返回范围为 (0, 1) 的一个伪随机数。
log() 自然对数。
log10() 以 10 为底的对数。
log1p() 返回 log(1 + number)。
max() 返回最大值。
min() 返回最小值。
mt_getrandmax() 显示随机数的最大可能值。
mt_rand() 使用 Mersenne Twister 算法返回随机整数。
mt_srand() Seeds the Mersenne Twister random number generator
octdec() 把八进制转换为十进制。
pi() 返回圆周率的值。
pow() 返回 x 的 y 次方。
rad2deg() 把弧度数转换为角度数。
rand() 返回随机整数。
round() 对浮点数进行四舍五入。
sin() 正弦。
sinh() 双曲正弦。
sqrt() 平方根。
srand() Seeds the random number generator
tan() 正切。
tanh() 双曲正切。

PHP Math 常量

常量 描述
INF INF The infinite
M_E 2.7182818284590452354 Returns e
M_EULER 0.57721566490153286061 Returns Euler constant
M_LNPI 1.14472988584940017414 Returns the natural logarithm of PI: log_e(pi)
M_LN2 0.69314718055994530942 Returns the natural logarithm of 2: log_e 2
M_LN10 2.30258509299404568402 Returns the natural logarithm of 10: log_e 10
M_LOG2E 1.4426950408889634074 Returns the base-2 logarithm of E: log_2 e
M_LOG10E 0.43429448190325182765 Returns the base-10 logarithm of E: log_10 e
M_PI 3.14159265358979323846 Returns Pi
M_PI_2 1.57079632679489661923 Returns Pi/2
M_PI_4 0.78539816339744830962 Returns Pi/4
M_1_PI 0.31830988618379067154 Returns 1/Pi
M_2_PI 0.63661977236758134308 Returns 2/Pi
M_SQRTPI 1.77245385090551602729 Returns the square root of PI: sqrt(pi)
M_2_SQRTPI 1.12837916709551257390 Returns 2/square root of PI: 2/sqrt(pi)
M_SQRT1_2 0.70710678118654752440 Returns the square root of 1/2: 1/sqrt(2)
M_SQRT2 1.41421356237309504880 Returns the square root of 2: sqrt(2)
M_SQRT3 1.73205080756887729352 Returns the square root of 3: sqrt(3)
NAN NAN Not A Number
PHP_ROUND_HALF_UP 1 Round halves up
PHP_ROUND_HALF_DOWN 2 Round halves down
PHP_ROUND_HALF_EVEN 3 Round halves to even numbers
PHP_ROUND_HALF_ODD 4 Round halves to odd numbers