How to use math functions and constants in PHP

1 Answer

0 votes
/* Program Title: PHP Math Functions Demonstration */

/* ============================
   Constants
   ============================ */
$piValue = M_PI;          // π constant
$eValue  = M_E;           // Euler's number

echo "Constants:\n";
echo "  pi = $piValue\n";
echo "  e  = $eValue\n\n";


/* ============================
   Core Functions
   ============================ */
$numX = 2.5;              // sample value

echo "Core Functions:\n";
echo "  sqrt(2.5) = " . sqrt($numX) . "\n";     // square root
echo "  exp(2.5)  = " . exp($numX) . "\n";      // e^x
echo "  log(2.5)  = " . log($numX) . "\n";      // natural log
echo "  log10(2.5)= " . log10($numX) . "\n";    // base-10 log
echo "  pow(2.5,3)= " . pow($numX, 3) . "\n\n"; // x^3


/* ============================
   Trigonometric Functions
   ============================ */
$angle = M_PI / 4;        // 45 degrees in radians

echo "Trigonometric Functions:\n";
echo "  sin(pi/4) = " . sin($angle) . "\n";     // sine
echo "  cos(pi/4) = " . cos($angle) . "\n";     // cosine
echo "  tan(pi/4) = " . tan($angle) . "\n";     // tangent
echo "  asin(0.5) = " . asin(0.5) . "\n";       // arcsine
echo "  acos(0.5) = " . acos(0.5) . "\n";       // arccosine
echo "  atan(1.0) = " . atan(1.0) . "\n\n";     // arctangent


/* ============================
   Hyperbolic Functions
   ============================ */
echo "Hyperbolic Functions:\n";
echo "  sinh(1) = " . sinh(1) . "\n";           // hyperbolic sine
echo "  cosh(1) = " . cosh(1) . "\n";           // hyperbolic cosine
echo "  tanh(1) = " . tanh(1) . "\n\n";         // hyperbolic tangent


/* ============================
   Rounding Functions
   ============================ */
$numY = -2.7;             // negative number for rounding tests

echo "Rounding Functions:\n";
echo "  floor(-2.7) = " . floor($numY) . "\n";  // round down
echo "  ceil(-2.7)  = " . ceil($numY) . "\n";   // round up
echo "  round(-2.7) = " . round($numY) . "\n";  // nearest integer
echo "  intval(-2.7)= " . intval($numY) . "\n\n"; // truncate decimals


/* ============================
   Min / Max / Clamp
   ============================ */
$numA = 10;
$numB = 20;
$valToClamp = 15;

echo "Min/Max/Clamp:\n";
echo "  min(10,20) = " . min($numA, $numB) . "\n"; // smaller of two
echo "  max(10,20) = " . max($numA, $numB) . "\n"; // larger of two

// clamp manually
$clamped = max(0, min(10, $valToClamp));          // clamp 15 to range 0–10
echo "  clamp(15,0,10) = $clamped\n\n";


/* ============================
   Bitwise Math (Integers)
   ============================ */
$byteA = 0xAA;            // 10101010 in hex
$byteB = 0xCC;            // 11001100 in hex

echo "Bitwise Math:\n";
echo "  A & B = " . dechex($byteA & $byteB) . "\n"; // AND
echo "  A | B = " . dechex($byteA | $byteB) . "\n"; // OR
echo "  A ^ B = " . dechex($byteA ^ $byteB) . "\n"; // XOR
echo "  ~A    = " . dechex(~$byteA & 0xFF) . "\n";  // NOT (mask to 8 bits)
echo "  A << 2 = " . dechex(($byteA << 2) & 0xFF) . "\n"; // left shift
echo "  B >> 3 = " . dechex($byteB >> 3) . "\n\n";        // right shift


/* ============================
   Additional Useful Math
   ============================ */
echo "Additional Math:\n";
echo "  abs(-3.14) = " . abs(-3.14) . "\n";       // absolute value
echo "  fmod(10,3) = " . fmod(10, 3) . "\n";      // remainder
echo "  hypot(3,4) = " . hypot(3, 4) . "\n";      // sqrt(x²+y²)
echo "  deg2rad(180) = " . deg2rad(180) . "\n";   // degrees → radians
echo "  rad2deg(pi) = " . rad2deg(M_PI) . "\n";   // radians → degrees


/* 
run:

Constants:
  pi = 3.1415926535898
  e  = 2.718281828459

Core Functions:
  sqrt(2.5) = 1.5811388300842
  exp(2.5)  = 12.182493960703
  log(2.5)  = 0.91629073187416
  log10(2.5)= 0.39794000867204
  pow(2.5,3)= 15.625

Trigonometric Functions:
  sin(pi/4) = 0.70710678118655
  cos(pi/4) = 0.70710678118655
  tan(pi/4) = 1
  asin(0.5) = 0.5235987755983
  acos(0.5) = 1.0471975511966
  atan(1.0) = 0.78539816339745

Hyperbolic Functions:
  sinh(1) = 1.1752011936438
  cosh(1) = 1.5430806348152
  tanh(1) = 0.76159415595576

Rounding Functions:
  floor(-2.7) = -3
  ceil(-2.7)  = -2
  round(-2.7) = -3
  intval(-2.7)= -2

Min/Max/Clamp:
  min(10,20) = 10
  max(10,20) = 20
  clamp(15,0,10) = 10

Bitwise Math:
  A & B = 88
  A | B = ee
  A ^ B = 66
  ~A    = 55
  A << 2 = a8
  B >> 3 = 19

Additional Math:
  abs(-3.14) = 3.14
  fmod(10,3) = 1
  hypot(3,4) = 5
  deg2rad(180) = 3.1415926535898
  rad2deg(pi) = 180
  
*/

 



answered 1 day ago by avibootz
...