fyre/math

A math utility library.

v2.0.9 2024-10-26 03:06 UTC

This package is auto-updated.

Last update: 2024-10-26 03:07:17 UTC


README

FyreMath is a free, open-source math library for PHP.

Table Of Contents

Installation

Using Composer

composer require fyre/math

In PHP:

use Fyre\Utility\Math;

Methods

Abs

Get the absolute value of a number.

  • $number is the input number.
$abs = Math::abs($number);

Acos

Get the arc cosine of a number.

  • $number is the input number.
$acos = Math::acos($number);

Acosh

Get the inverse hyperbolic cosine of a number.

  • $number is the input number.
$acosh = Math::acosh($number);

Asin

Get the arc sine of a number.

  • $number is the input number.
$asin = Math::asin($number);

Asinh

Get the inverse hyperbolic sine of a number.

  • $number is the input number.
$asinh = Math::asinh($number);

Atan

Get the arc tangent of a number.

  • $number is the input number.
$atan = Math::atan($number);

Atan2

Get the arc tangent of 2 numbers.

  • $x is a number representing the dividend.
  • $y is a number representing the divisor.
$atan2 = Math::atan2($x, $y);

Atanh

Get the inverse hyperbolic tangent of a number.

  • $number is the input number.
$atanh = Math::atanh($number);

Binary To Decimal

Convert a binary number to decimal.

  • $binaryString is the binary string.
$decimal = Math::binaryToDecimal($binaryString);

Ceil

Round a number up.

  • $number is the input number.
$rounded = Math::ceil($number);

Clamp

Clamp a number between a min and max.

  • $number is the input number.
  • $min is a number representing the minimum of the clamped value, and will default to 0.
  • $max is a number representing the maximum of the clamped value, and will default to 1.
$clamped = Math::clamp($number, $min, $max);

Clamp Percent

Clamp a number between 0 and 100.

  • $number is the input number.
$clamped = Math::clampPercent($number);

Convert Base

Convert a number between bases.

  • $number is the input number.
  • $fromBase is a number representing the base of the number.
  • $toBase is a number representing the base to convert to.
$newNumber = Math::convertBase($number, $fromBase, $toBase);

Cos

Get the cosine of a number.

  • $number is the input number.
$cos = Math::cos($number);

Cosh

Get the hyperbolic cosine of a number.

  • $number is the input number.
$cosh = Math::cosh($number);

Decimal To Binary

Convert a decimal number to binary.

  • $number is the input number.
$binary = Math::decimalToBinary($number);

Decimal To Hex

Convert a decimal number to hex.

  • $number is the input number.
$hex = Math::decimalToHex($number);

Decimal To Octal

Convert a decimal number to octal.

  • $number is the input number.
$octal = Math::decimalToOctal($number);

Degrees To Radians

Convert a number of degrees to radians.

  • $number is the input number.
$radians = Math::degreesToRadians($number);

Distance

Calculate the distance between 2 points.

  • $x1 is a number representing the first X co-ordinate.
  • $y1 is a number representing the first Y co-ordinate.
  • $x2 is a number representing the second X co-ordinate.
  • $y2 is a number representing the second Y co-ordinate.
$distance = Math::dist($x1, $y1, $x2, $y2);

Exp

Calculate the exponent of e.

  • $number is the input number.
$exp = Math::exp($number);

Exp Minus 1

Calculate the exponent of e-1.

  • $number is the input number.
$expMinus1 = Math::expMinus1($number);

Floor

Round a number down.

  • $number is the input number.
$rounded = Math::floor($number);

Fmod

Calculate the modulo of a number with a divisor.

  • $number is the input number.
  • $divisor is a number representing the divisor.
$modulo = Math::fmod($number, $divisor);

Hex To Decimal

Convert a hex number to decimal.

  • $hexString is the hex string.
$decimal = Math::hexToDecimal($hexString);

Hypot

Calculate the length of a point.

  • $x is a number representing the X co-ordinate.
  • $y is a number representing the Y co-ordinate.
$length = Math::hypot($x, $y);

Inverse Linear Interpolation

Inverse linear interpolation from one value to another.

  • $v1 is a number representing the minimum of the range.
  • $v2 is a number representing the maximum of the range.
  • $value is a number representing the value to inverse interpolate.
$amount = Math::inverseLerp($v1, $v2, $value);

Is Numeric

Determine if the value is numeric.

  • $value is the value to test.
$isNumeric = Math::isNumeric($value);

Linear Interpolation

Linear interpolation from one value to another.

  • $v1 is a number representing the minimum of the range.
  • $v2 is a number representing the maximum of the range.
  • $amount is a number representing the amount to interpolate.
$value = Math::lerp($v1, $v2, $amount);

Log

Calculate the logarithm.

  • $number is the input number.
  • $base is a number representing the logarithmic base, and will default to E.
$log = Math::log($number, $base);

Log 10

Calculate the base-10 logarithm.

  • $number is the input number.
$log = Math::log10($number);

Log Plus 1

Calculate the logarithm of a number + 1.

  • $number is the input number.
$log = Math::logPlus1($number);

Map

Map a value from one range to another.

  • $number is the input number.
  • $fromMin is a number representing the minimum value of the current range.
  • $fromMax is a number representing the maximum value of the current range.
  • $toMin is a number representing the minimum value of the target range.
  • $toMax is a number representing the maximum value of the target range.
$mapped = Math::map($number, $fromMin, $fromMax, $toMin, $toMax);

Max

Find the highest value.

All arguments supplied to this method will be compared.

$highest = Math::max(...$numbers);

Min

Find the lowest value.

All arguments supplied to this method will be compared.

$lowest = Math::min(...$numbers);

Octal To Decimal

Convert an octal number to decimal.

  • $octalString is the octal string.
$decimal = Math::octalToDecimal($octalString);

Pow

Raise a number to the power of exponent.

  • $number is the input number.
  • $exponent is a number representing the exponent.
$pow = Math::pow($number, $exponent);

Product

Calculate the product of values.

All arguments supplied to this method will be multiplied.

$product = Math::product(...$numbers);

Radians To Degrees

Convert a number of radians to degrees.

  • $number is the input number.
$degrees = Math::radiansToDegrees($number);

Random

Return a random floating-point number.

  • $a is a number representing the minimum value (inclusive).
  • $b is a number representing the maximum value (exclusive).
$random = Math::random($a, $b);

If $b is omitted, this function will return a random number between 0 (inclusive) and $a (exclusive).

If both arguments are omitted, this function will return a random number between 0 (inclusive) and 1 (exclusive).

Random Int

Return a random integer.

  • $a is a number representing the minimum value (inclusive).
  • $b is a number representing the maximum value (exclusive).
$randomInt = Math::randomInt($a, $b);

If $b is omitted, this function will return a random number between 0 (inclusive) and $a (exclusive).

Round

Round a number.

  • $number is the input number.
  • $precision is a number representing the number of decimal digits to use, and will default to 0.
  • $mode is a number representing the rounding mode to use, and will default to Math::ROUND_HALF_UP.
$rounded = Math::round($number, $precision, $mode);

Sin

Get the sine of a number.

  • $number is the input number.
$sin = Math::sin($number);

Sinh

Get the hyperbolic sine of a number.

  • $number is the input number.
$sinh = Math::sinh($number);

Sqrt

Get the square root of a number.

  • $number is the input number.
$sqrt = Math::sqrt($number);

Sum

Calculate the sum of values.

All arguments supplied to this method will be added.

$sum = Math::sum(...$numbers);

Tan

Get the tangent of a number.

  • $number is the input number.
$tan = Math::tan($number);

Tanh

Get the hyperbolic tangent of a number.

  • $number is the input number.
$tanh = Math::tanh($number);

To Step

Round a number to a specified step-size.

  • $number is the input number.
  • $step is a number representing the minimum step-size, and will default to 0.01.
$toStep = Math::toStep($number, $step);