Math

25 entries

abs Function Returns the absolute value of a number. base_convert Function Arbitrary-base string conversion (2-36 both ways); large numbers lose precision through the internal float (manual warning, verified) - use BCMath/GMP for exactness. bindec / octdec / decbin / dechex / hexdec (fixed-base quick reference) Function Quick reference for the fixed-base conversions between binary/octal/hexadecimal and decimal: bindec/octdec/hexdec parse to decimal, decbin/dechex format from decimal. For arbitrary bases (2-36) see the base_convert entry; the single-function deep dives live in the dechex/hexdec entries. ceil Function Rounds fractions up (ceiling). clamp Function Range clamping (new in PHP 8.6): returns the value when it lies within [min, max], otherwise the nearest bound; min > max throws ValueError. dechex Function Converts a decimal number to its hexadecimal string representation. deg2rad / rad2deg Function Converts between degrees and radians: deg2rad multiplies by pi/180 and rad2deg by 180/pi — the two-way bridge in and out of trigonometric functions. the exp / log / log10 / log1p family Function The transcendental family: exp/log are mutual inverses (log(exp(2)) returns 2), log10 is the common logarithm, and log1p computes log(1+x) accurately even when x is near zero — the contrast experiment "1 + 1e-16 rounds to exactly 1.0 in double" is the heart of this entry. fdiv Function Safe float division (PHP 8.0+): division by zero and invalid input don't throw DivisionByZeroError — IEEE semantics return INF/-INF/NAN. floor Function Rounds down toward negative infinity: floor(-2.5) is -3 while an int cast truncates toward zero giving -2 - the negative-direction trap is the core teaching point paired with ceil. fmod Function Float modulo whose result keeps the sign of the dividend. hexdec Function Converts a hexadecimal string to a decimal number. hypot Function Calculates the hypotenuse of a right-angle triangle (distance from (x, y) to the origin). Mathematically equal to sqrt(x^2+y^2), but it scales before squaring so intermediate values never overflow — the extreme 3e200/4e200 contrast is the heart of this entry: the naive formula squares its way to INF while hypot still returns 5e200. is_infinite Function Checks whether a value is infinite (INF or -INF). is_nan Function Checks whether a value is NaN (not a number). max Function Returns the largest of several values. min Function Returns the smallest of several values. mt_rand Function Mersenne Twister pseudo-random integers: once mt_srand fixes the seed the sequence is fully reproducible — the only randomness entry whose output can be pre-set deterministically. pow Function Raises a number to a power (PHP 8 recommends the `**` operator). rand Function Generates a random integer within a given range. random_bytes Function Generates cryptographically secure random bytes (for tokens, keys, salts and the like). random_int Function Generates a cryptographically secure random integer (both bounds inclusive). round Function Rounds a float to a given precision. sqrt / pi Function Square root and pi: sqrt returns the float square root (negative input yields a silently propagating NAN, not an error), and pi() holds the same double-precision pi as the M_PI constant; under IEEE 754 every language's output matches exactly. the sin / cos / tan family Function Quick reference for the trigonometric family: basic (sin/cos/tan), inverse (asin/acos/atan) and hyperbolic (sinh/cosh/tanh) functions are built in across all languages — and every argument is in radians, not degrees; convert with deg2rad first.