Math Functions

Table of Contents

  • abs — Absolute value
  • acos — Arc cosine
  • acosh — Inverse hyperbolic cosine
  • asin — Arc sine
  • asinh — Inverse hyperbolic sine
  • atan2 — Arc tangent of two variables
  • atan — Arc tangent
  • atanh — Inverse hyperbolic tangent
  • base_convert — Convert a number between arbitrary bases
  • bindec — Binary to decimal
  • ceil — Round fractions up
  • cos — Cosine
  • cosh — Hyperbolic cosine
  • decbin — Decimal to binary
  • dechex — Decimal to hexadecimal
  • decoct — Decimal to octal
  • deg2rad — Converts the number in degrees to the radian equivalent
  • exp — Calculates the exponent of e
  • expm1 — Returns exp(number) - 1, computed in a way that is accurate even when the value of number is close to zero
  • floor — Round fractions down
  • fmod — Returns the floating point remainder (modulo) of the division of the arguments
  • getrandmax — Show largest possible random value
  • hexdec — Hexadecimal to decimal
  • hypot — Calculate the length of the hypotenuse of a right-angle triangle
  • intdiv — Integer division
  • is_finite — Finds whether a value is a legal finite number
  • is_infinite — Finds whether a value is infinite
  • is_nan — Finds whether a value is not a number
  • lcg_value — Combined linear congruential generator
  • log10 — Base-10 logarithm
  • log1p — Returns log(1 + number), computed in a way that is accurate even when the value of number is close to zero
  • log — Natural logarithm
  • max — Find highest value
  • min — Find lowest value
  • mt_getrandmax — Show largest possible random value
  • mt_rand — Generate a random value via the Mersenne Twister Random Number Generator
  • mt_srand — Seeds the Mersenne Twister Random Number Generator
  • octdec — Octal to decimal
  • pi — Get value of pi
  • pow — Exponential expression
  • rad2deg — Converts the radian number to the equivalent number in degrees
  • rand — Generate a random integer
  • round — Rounds a float
  • sin — Sine
  • sinh — Hyperbolic sine
  • sqrt — Square root
  • srand — Seed the random number generator
  • tan — Tangent
  • tanh — Hyperbolic tangent
add a note add a note

User Contributed Notes 52 notes

up
3
nazgul26 (at_sign) windfox dot net
16 years ago
This code will convert a decimal to it's fraction equivalent. The precision can be set by changing PRECISION.

<?php
define
(PRECISION, .01);

$count=0;
$result=array();
decimalToFraction($_REQUEST['dec'],$count,&$result);
$count = count($result);
$simp_fract = simplifyFraction($result,$count,1,$result[$count]);

echo
$simpl_fract;

// Start of functions

/*
   Converts a decimal to unsimplified fraction represented in an array
*/
function decimalToFraction($decimal,$count,$result) {
   
$a = (1/$decimal);
   
$b = ( $a - floor($a)  );
   
$count++;
    if (
$b > .01 && $count <= 5) decimalToFraction($b,$count,&$result);
   
$result[$count] = floor($a);
}

/*
    Simplifies a fraction in an array form that is returned from 
    decimalToFraction
*/
function simplifyFraction($fraction,$count,$top,$bottom) {
   
$next = $fraction[$count-1];
   
$a = ($bottom * $next) + $top;
   
$top = $bottom;
   
$bottom = $a;
   
$count--;
    if (
$count > 0) simplifyFraction($fraction,$count,$top,$bottom);
    else {
        return
"<font size=1>$bottom/$top</font>";
    }
}
?>
up
0
Anonymous
11 months ago
And the reason I needed a Factorial function is because I there were no nPr or nCr functions native to PHP, either.

function n_pick_r($n,$r){$n=(int)$n; $r=(int)$r;return (fact($n)/fact($n-$r));}
function n_choose_r($n,$r){$n=(int)$n; $r=(int)$r;return (n_pick_r($n,$r)/fact($r));}

Hope that helps someone!
exmple:https://vb.3dlat.com/
up
0
peter-stangl at t-online dot de
12 years ago
I needed to approximate an integral because i was not able to calculate it, so i wrote this function. It approximates an integral with the composite Simpson's rule.
More information on Simpson's rule: http://en.wikipedia.org/wiki/Simpson%27s_rule

<?php

function simpsonf($x){
// returns f(x) for integral approximation with composite Simpson's rule
  
return(pow((1+pow($x, (-4))), 0.5));
}
function
simpsonsrule($a, $b, $n){
// approximates integral_a_b f(x) dx with composite Simpson's rule with $n intervals
// $n has to be an even number
// f(x) is defined in "function simpsonf($x)"
  
if($n%2==0){
     
$h=($b-$a)/$n;
     
$S=simpsonf($a)+simpsonf($b);
     
$i=1;
      while(
$i <= ($n-1)){
        
$xi=$a+$h*$i;
         if(
$i%2==0){
           
$S=$S+2*simpsonf($xi);
         }
         else{
           
$S=$S+4*simpsonf($xi);
         }
        
$i++;
      }
      return(
$h/3*$S);
      }
   else{
      return(
'$n has to be an even number');
   }
}

?>
up
-1
sabry97000 at gmail dot com
5 months ago
And the reason I needed a Factorial function is because I there were no nPr or nCr functions native to PHP, either.

function n_pick_r($n,$r){$n=(int)$n; $r=(int)$r;return (fact($n)/fact($n-$r));}
function n_choose_r($n,$r){$n=(int)$n; $r=(int)$r;return (n_pick_r($n,$r)/fact($r));}

Hope that helps someone!
exmple www.pqlme.com php
up
-1
jbeardsl [found_at] gte [d0t] net
16 years ago
I was looking for a truncate function. Not finding one, I wrote my own. Since it deals with everything as a number, I imagine it's faster than the alternative of using string functions. HTH...

<?php
function truncate ($num, $digits = 0) {

   
//provide the real number, and the number of
    //digits right of the decimal you want to keep.

   
$shift = pow(1