LXVII. Math 数学函数

简介

这些数学函数仅能处理在你计算机上 integerfloat 范围内的值(目前这对应于 C 类型中的 long 和 double)。如果要处理更大的数字,参见高精度数学函数

请参阅手册中算术运算符的有关章节。

需求

要编译本扩展模块不需要外部库文件。

安装

本函数库作为 PHP 内核的一部分,不用安装就能使用。

运行时配置

本扩展模块在 php.ini 中未定义任何配置选项。

资源类型

本扩展模块未定义任何资源类型。

预定义常量

以下常量作为 PHP 核心的一部分一直有效。

表格 1. 数学常量

常量名常量值描述
M_PI3.14159265358979323846Pi
M_E2.7182818284590452354e
M_LOG2E1.4426950408889634074log_2 e
M_LOG10E0.43429448190325182765log_10 e
M_LN20.69314718055994530942log_e 2
M_LN102.30258509299404568402log_e 10
M_PI_21.57079632679489661923pi/2
M_PI_40.78539816339744830962pi/4
M_1_PI0.318309886183790671541/pi
M_2_PI0.636619772367581343082/pi
M_SQRTPI1.77245385090551602729sqrt(pi) [4.0.2]
M_2_SQRTPI1.128379167095512573902/sqrt(pi)
M_SQRT21.41421356237309504880sqrt(2)
M_SQRT31.73205080756887729352sqrt(3) [4.0.2]
M_SQRT1_20.707106781186547524401/sqrt(2)
M_LNPI1.14472988584940017414log_e(pi) [4.0.2]
M_EULER0.57721566490153286061Euler constant [4.0.2]
只有常量 M_PI 在 PHP 4.0.0 之前就存在,其它所有常量从 PHP 4.0.0 起开始存在。用 [4.0.2] 标记的常量是在 PHP 4.0.2 中增加的。

目录
abs -- 绝对值
acos -- 反余弦
acosh -- 反双曲余弦
asin -- 反正弦
asinh -- 反双曲正弦
atan2 -- 两个参数的反正切
atan -- 反正切
atanh -- 反双曲正切
base_convert -- 在任意进制之间转换数字
bindec -- 二进制转换为十进制
ceil -- 进一法取整
cos -- 余弦
cosh -- 双曲余弦
decbin -- 十进制转换为二进制
dechex -- 十进制转换为十六进制
decoct -- 十进制转换为八进制
deg2rad --  将角度转换为弧度
exp -- 计算 e(自然对数的底)的指数
expm1 --  返回 exp(number) - 1,甚至当 number 的值接近零也能计算出准确结果
floor -- 舍去法取整
fmod -- 返回除法的浮点数余数
getrandmax -- 显示随机数最大的可能值
hexdec -- 十六进制转换为十进制
hypot --  计算一直角三角形的斜边长度
is_finite -- 判断是否为有限值
is_infinite -- 判断是否为无限值
is_nan -- 判断是否为合法数值
lcg_value -- 组合线性同余发生器
log10 -- 以 10 为底的对数
log1p --  返回 log(1 + number),甚至当 number 的值接近零也能计算出准确结果
log -- 自然对数
max -- 找出最大值
min -- 找出最小值
mt_getrandmax -- 显示随机数的最大可能值
mt_rand -- 生成更好的随机数
mt_srand -- 播下一个更好的随机数发生器种子
octdec -- 八进制转换为十进制
pi -- 得到圆周率值
pow -- 指数表达式
rad2deg --  将弧度数转换为相应的角度数
rand -- 产生一个随机整数
round -- 对浮点数进行四舍五入
sin -- 正弦
sinh -- 双曲正弦
sqrt -- 平方根
srand -- 播下随机数发生器种子
tan -- 正切
tanh -- 双曲正切

add a note add a note User Contributed Notes
tim at durge dot org
27-Oct-2006 08:51
In Evan's ordinal function, the line:

<?php
  $tens
= substr($cardinal, -2, 1);
?>

needs to be replaced by:

<?php
  $tens
= round($cardinal/10);
?>

or similar. At least on PHP 4.3.10,  substr("1", -2, 1)  returns '1' - so Evan's function gives "1th", as well as "11th".  This is contrary to the documentation, but is noted in the comments on the substr manual page.
Evan Broder
27-Jul-2006 02:42
A slightly more complex but much more accurate cardinal=>ordinal function (the one below doesn't account for 11th, 12th, and 13th, which don't follow the usual rules):

<?php

  
function ordinal($cardinal)
   {
      
$cardinal = (int)$cardinal;
      
$digit = substr($cardinal, -1, 1);
      
$tens = substr($cardinal, -2, 1);
       if(
$tens == 1)
       {
           return
$cardinal.'th';
       }
      
       switch(
$digit)
       {
       case
1:
           return
$cardinal.'st';
       case
2:
           return
$cardinal.'nd';
       case
3:
           return
$cardinal.'rd';
       default:
           return
$cardinal.'th';
       }
   }

?>
g8z at yahoo dot com
20-Jul-2006 02:15
<?php
/**
generate_ratio outputs the properly reduced ratio of the two numbers specified. This function is divide by zero safe.

Courtesy of the $5 Script Archive: http://www.tufat.com
**/

function generate_ratio($number_1, $number_2)
{
  
// figure out which number is lowest
  
$lowest = $number_1;
   if (
$number_2 < $lowest)
      
$lowest = $number_2;

  
// find the greatest common factor
  
$gcf = 1;
   for (
$count=$lowest + 1; $count >= 1; $count--) {
       if ((
$number_1 % $count == 0 && $number_2 % $count == 0) && $count > $gcf)
          
$gcf = $count;
   }

  
// reduce
  
$number_1 /= $gcf;
  
$number_2 /= $gcf;

  
// return ratio string
  
$ratio = $number_1 . ':' . $number_2;
   return
$ratio;
}

// EXAMPLE:
print generate_ratio(44, 32);
?>
edward at edwardsun dot com
20-Jul-2006 09:24
well just a note.. maybe i'm a bit stupid.. but remember to use pow() rather than the "^" sign for exponents.. as it took me 5 minutes to figure out why it wasn't working.
jaakko dot mantila at sagas dot fi
12-Jul-2006 06:10
Here is another payment function with working future value($fv) option:

function payment($r,$np,$pv,$fv,$prec) {
   /* Calculates the monthly payment
   ** $apr = the annual percentage rate of the loan.
   ** $n  = number of monthly payments (360 for a 30year loan)
   ** $pv    = present value or principal of the loan
   ** $fv  = future value of the loan (after payments)
   ** $prec = the precision you wish rounded to
   */
   /****************************************\
   ** No Warranty is expressed or implied. **
   *****************************************/
if(!$fv) $fv = 0;
$mypmt=$r * (-$fv+pow((1+$r),$np)*$pv)/(-1+pow((1+$r),$np));
return round($mypmt, $prec);
}
twoscoopsofpig at NOSPAM dot gmail dot com
08-Jul-2006 12:07
@ Moikboy:

This may or may not be more simplified factorialization:

<?php
$f
=$fact=25;
while (
$fact>0)
{
$f=$f*$fact--;}
echo
$f;
?>
marasek.SPAMLESS at telton.de
08-Jun-2006 08:23
I could not resist to do a simpler version of the ordinal function:
<?php
function ordinal($num)
{
  
$num = (int)$num;
  
$digit = substr($num, -1, 1);
  
$ord = "th";
   switch(
$digit)
   {
       case
1: $ord = "st"; break;
       case
2: $ord = "nd"; break;
       case
3: $ord = "rd"; break;
   break;
   }
return
$num.$ord;
}
?>
One could replace the typecast with

<?php
if($num===NULL or $num==="")
{return
NULL;}
?>

to get an empty result instead of "0th" in case $num is empty too.
moikboy (nospam!) moikboy (nospam!) hu
10-May-2006 04:15
I think, this is the optimal code for calculating factorials:

<?php
function fact($int){
   if(
$int<2)return 1;
   for(
$f=2;$int-1>1;$f*=$int--);
   return
$f;
};
?>

And another one for calculating the $int-th Fibonacci-number:

<?php
function fib($int){
   static
$fibTable=array();
   return empty(
$fibTable[$int])?$fibTable[$int] = $int>1?fib($int-2)+fib($int-1):1:$fibTable[$int];
};
?>
nessthehero at comcast dot net
22-Mar-2006 06:48
Just a simple function to find the ordinal ending to any number if you're printing for example: "The nth result is..."

function ordinal($num) {
   $digit = substr($num,-1,1);
   $ord = array(
       0 => 'th',
       1 => 'st',
       2 => 'nd',
       3 => 'rd',
       4 => 'th',
       5 => 'th',
       6 => 'th',
       7 => 'th',
       8 => 'th',
       9 => 'th'
   );       
   $string = $num.$ord[$digit];
   return $string;
}
Florian
04-Mar-2006 05:36
A function that simulates the sum operator. (http://en.wikipedia.org/wiki/Sum). Be careful with the expression because it may cause a security hole; note the single quotes to don't parse the "$".
<?php
# @param    string    $expr    expression to evaluate (for example (2*$x)^2+1)
# @param    string    $var      dummy variable (for example "x")
# @param    integer    $start
# @param    integer    $end
# @param    integer    $step

function sum($expr,$var,$start,$end,$step = 1) {
  
$expr = str_replace(';','',$expr);
  
$var = str_replace('$','',$var);
  
$start = (int)$start;    $end = (int)$end;    $step = (int)$step;    $sum = 0;
  
   for (
$i = $start; $i <= $end; $i = $i + $step) {
      
$_expr = str_replace('$'.$var,$i,$expr);   
      
$_eval = '$_result = '.$_expr.'; return $_result;';
      
$_result = eval($_eval);
       if(
$result === FALSE) return "SYNTAX ERROR : $expr";
      
$sum += $_result;
   }
   return (int)
$sum;
}
?>
jos at gtacrime dot nl
17-Feb-2006 05:39
Thanks to Chronial "at" cyberpunkuniverse.de, I was able to create the binompdf(n, p, k) function.

<?php
function nCr($n, $k){
   if (
$k > $n)
     return
NaN;
   if ((
$n - $k) < $k)
     return
nCr($n, ($n - $k));
  
$return = 1;
   for (
$i=0; $i<$k; $i++){
    
$return *= ($n - $i) / ($i + 1);
   }
   return
$return;
}

function
binompdf($n, $p, $k){
  
$return = nCr($n, $k) * pow($p, $k) * pow((1 - $p), ($n - $k));
   return
$return;
}
?>
peter-stangl at t-online dot de
02-Feb-2006 07:16
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');
   }
}

?>
daniel at g-loc dot org
02-Dec-2005 02:01
If youre an aviator and needs to calculate windcorrection angles and groundspeed (e.g. during flightplanning) this can be very useful.

$windcorrection = rad2deg(asin((($windspeed * (sin(deg2rad($tt - ($winddirection-180))))/$tas))));
$groundspeed = $tas*cos(deg2rad($windcorrection)) + $windspeed*cos(deg2rad($tt-($winddirection-180)));

You can probably write these lines more beautiful, but they work!
nbraczek at bsds dot de
27-Oct-2005 05:27
Under some circumstances, it is appropriate to round floats to a given number of significant digits. This function will do it for you:

/**
 * Round to significant digits
 *
 * @param float  $f The number to be rounded
 * @param integer $n Number of significant digits
 */
function round_significant($f, $n)
{
   if ($f==0) return $f;
   return round($f, $n-floor(log10(abs($f)))-1);
}
monte at ohrt dot com
18-Oct-2005 09:37
This is an efficient method of calculating the binomial coefficient C(n,k). This code was derived from Owant: Mastering Algorithms with Perl.

<?php
  
// calculate binomial coefficient
  
function binomial_coeff($n, $k) {

    
$j = $res = 1;

     if(
$k < 0 || $k > $n)
         return
0;
     if((
$n - $k) < $k)
        
$k = $n - $k;

     while(
$j <= $k) {
        
$res *= $n--;
        
$res /= $j++;
     }

     return
$res;

   }
?>

If you compiled php with --enable-bcmath, you can get full integer values of extremely large numbers by replacing:

$res *= $n--;
$res /= $j++;

with:

$res = bcmul($res, $n--);
$res = bcdiv($res, $j++);
crescentfreshpot at yahoo dot com
27-Jul-2005 10:50
Median:

number median ( number arg1, number arg2 [, number ...] )

number median ( array numbers )

<?php

function median()
{
  
$args = func_get_args();

   switch(
func_num_args())
   {
       case
0:
          
trigger_error('median() requires at least one parameter',E_USER_WARNING);
           return
false;
           break;

       case
1:
          
$args = array_pop($args);
          
// fallthrough

      
default:
           if(!
is_array($args)) {
              
trigger_error('median() requires a list of numbers to operate on or an array of numbers',E_USER_NOTICE);
               return
false;
           }

          
sort($args);
          
          
$n = count($args);
          
$h = intval($n / 2);

           if(
$n % 2 == 0) {
              
$median = ($args[$h] + $args[$h-1]) / 2;
           } else {
              
$median = $args[$h];
           }

           break;
   }
  
   return
$median;
}

?>
bjcffnet at gmail dot com
27-Jul-2005 07:57
thearbitcouncil at gmail dot com, you could just use array_sum():
<?php
function average($arr)
{
   if (!
is_array($arr)) return false;

   return
array_sum($arr)/count($arr);
}

$array = array(5, 10, 15);
echo
average($array); // 10
?>
graywh at gmail DELETE dot com
02-Jun-2005 03:48
If you're really concerned about speed, you could compute the factorial of large numbers using the Gamma function of n-1.

Integral y^(t-1)*Exp(-y) for y from 0 to Infinity

For Fibonacci numbers, there's a better-than-recursive way.

((1+sqrt(5))/2)^(n/sqrt(5)) - ((1-sqrt(5))/2)^(n/sqrt(5))
eric at woolhiser dot com
11-Mar-2005 12:41
For all you guys writing mortgage calculators out there:

<?php

function payment($apr,$n,$pv,$fv=0.0,$prec=2){
  
/* Calculates the monthly payment rouned to the nearest penny
   ** $apr = the annual percentage rate of the loan.
   ** $n  = number of monthly payments (360 for a 30year loan)
   ** $pv    = present value or principal of the loan
   ** $fv  = future value of the loan
   ** $prec = the precision you wish rounded to
   */
   /****************************************\
   ** No Warranty is expressed or implied. **
   *****************************************/
  
  
if ($apr !=0) {
      
$alpha = 1/(1+$apr/12);
      
$retval round($pv * (1 - $alpha) / $alpha /
       (
1 - pow($alpha,$n)),$prec) ;
   } else {
      
$retval = round($pv / $n, $prec);
   }
   return(
$retval);

}
?>
tmpa at yahoo dot com
20-Feb-2005 04:42
while joogat's one line function is short, it is probably better to calculate factorial iteratively instead of recursively. keep in mind if you want large factorials, you'll need to use some sort of arbitrary precision integer or perhaps the BCMath functions. then again, unless you're trying to do large numbers (170! is the highest that you can do that does not return infinity) you probably won't notice any time difference.
<?php
function factorial($in) {
  
// 0! = 1! = 1
  
$out = 1;

  
// Only if $in is >= 2
  
for ($i = 2; $i <= $in; $i++) {
      
$out *= $i;
   }

   return
$out;
}
?>
thearbitcouncil at gmail dot com
07-Jan-2005 06:32
Two functions I didn't find elsewhere... one to compute mean of an array of numbers, and another to computer variance of a sample of numbers. Both take an array of numbers as arguments. Not much error checking, or optimization...

(note: variance function uses the average function...)

<?php

function average($arr)
{
   if (!
count($arr)) return 0;

  
$sum = 0;
   for (
$i = 0; $i < count($arr); $i++)
   {
      
$sum += $arr[$i];
   }

   return
$sum / count($arr);
}

function
variance($arr)
{
   if (!
count($arr)) return 0;

  
$mean = average($arr);

  
$sos = 0;    // Sum of squares
  
for ($i = 0; $i < count($arr); $i++)
   {
      
$sos += ($arr[$i] - $mean) * ($arr[$i] - $mean);
   }

   return
$sos / (count($arr)-1);  // denominator = n-1; i.e. estimating based on sample
                                   // n-1 is also what MS Excel takes by default in the
                                   // VAR function
}

echo
variance(array(4,6,23,15,18)); // echoes 64.7...correct value :)

?>
info at gavinvincent dot co dot uk
17-Nov-2004 08:34
If you need to deal with polar co-ordinates for somereason you will need to convert to and from x,y for input and output in most situations: here are some functions to convert cartesian to polar and polar to cartesian
<?
//returns array of r, theta in the range of 0-2*pi (in radians)
function rect2polar($x,$y)
{
     if(
is_numeric($x)&&is_numeric($y))
   {
      
$r=sqrt(pow($x,2)+pow($y,2));
       if(
$x==0)
       {
             if(
$y>0) $theta=pi()/2;
           else
$theta=3*pi()/2;
       }
       else if(
$x<0) $theta=atan($y/$x)+pi();
       else if(
$y<0) $theta=atan($y/$x)+2*pi();
       else
$theta=atan($y/$x);
      
$polar=array("r"=>$r,"theta"=>$theta);
       return
$polar;
   }
   else return
false;
}

//r must be in radians, returns array of x,y
function polar2rect($r,$theta)
{
 if(
is_numeric($r)&&is_numeric($theta))
 {
      
$x=$r*cos($theta);
  
$y=$r*sin($theta);
  
$rect=array("x"=>$x,"y"=>$y);
 }
 else
 {
   return
false;
 }
}
?>
help at gjbdesign dot com
26-Sep-2004 12:05
Occasionally a user must enter a number in a form. This function converts fractions to decimals and leaves decimals untouched. Of course, you may wish to round the final output, but that is not included here.

<?php
/*Some example values of $q
$q = "2.5";
$q = "2 1/2";
$q = "5/2";
*/
function Deci_Con($q){
//check for a space, signifying a whole number with a fraction
  
if(strstr($q, ' ')){
      
$wa = strrev($q);
      
$wb = strrev(strstr($wa, ' '));
      
$whole = true;//this is a whole number
  
}
//now check the fraction part
  
if(strstr($q, '/')){
       if(
$whole==true){//if whole number, then remove the whole number and space from the calculations
            
$q = strstr($q, ' ');
       }
$b = str_replace("/","",strstr($q, '/'));//this is the divisor
//isolate the numerator
$c = strrev($q);
$d = strstr($c, '/');
$e = strrev($d);
$a = str_replace("/","",$e);//the pre-final numerator
      
if($whole==true){//add the whole number to the calculations
          
$a = $a+($wb*$b);//new numerator is whole number multiplied by denominator plus original numerator   
      
}
$q = $a/$b;//this is now your decimal
return $q;
   }else{
       return
$q;//not a fraction, just return the decimal
  
}
}
?>
donnieb819 at hotmail dot NOSPAM dot com
16-Sep-2004 08:58
Method to convert an arbitrary decimal number to its most reduced fraction form (so a string is returned, this method would probably be used for output formatting purposes.)  There were other methods similar to this one on the page, but none did quite what I wanted.  It's maybe not the most elegant code, but it gets the job done.  Hope this helps someone.  An iterative form of Euclid's algorithm is used to find the GCD.

<?php
function dec2frac( $decimal )
{
 
$decimal = (string)$decimal;
 
$num = '';
 
$den = 1;
 
$dec = false;
 
 
// find least reduced fractional form of number
 
for( $i = 0, $ix = strlen( $decimal ); $i < $ix; $i++ )
  {
  
// build the denominator as we 'shift' the decimal to the right
  
if( $dec ) $den *= 10;
  
  
// find the decimal place/ build the numberator
  
if( $decimal{$i} == '.' ) $dec = true;
   else
$num .= $decimal{$i};
  }
 
$num = (int)$num;
  
 
// whole number, just return it
 
if( $den == 1 ) return $num;
  
 
$num2 = $num;
 
$den2 = $den;
 
$rem  = 1;
 
// Euclid's Algorithm (to find the gcd)
 
while( $num2 % $den2 ) {
  
$rem = $num2 % $den2;
  
$num2 = $den2;
  
$den2 = $rem;
  }
  if(
$den2 != $den ) $rem = $den2;
  
 
// now $rem holds the gcd of the numerator and denominator of our fraction
 
return ($num / $rem ) . "/" . ($den / $rem);
}
?>

Examples:
echo dec2frac( 10 );
echo dec2frac( .5 );
echo dec2frac( 5.25 );
echo dec2frac( .333333333 );

yields:
10
1/2
21/4
333333333/1000000000
pat.mat AT sympatico DOT com
09-Jun-2004 10:36
For people interest in Differential Equations, I've done a function that receive a string like: x^2+x^3 and put it in
2x+3x^2 witch is the differantial of the previous equation.

In the code there is one thing missing: the $string{$i} is often going outOfBound (Uninitialized string offset: 6 in...)
if your error setting is set a little too high... I just dont know how to fix this.

So there is the code for differential equation with (+ and -) only:

<?
function differentiel($equa)
{
  
$equa = strtolower($equa);
   echo
"Equation de depart: ".$equa."<br>";
  
$final = "";
  
   for(
$i = 0; $i < strlen($equa); $i++)
   {
      
//Make a new string from the receive $equa
      
if($equa{$i} == "x" && $equa{$i+1} == "^")
       {
          
$final .= $equa{$i+2};
          
$final .= "x^";
          
$final .= $equa{$i+2}-1;
       }
       elseif(
$equa{$i} == "+" || $equa{$i} == "-")
       {
          
$final .= $equa{$i};
       }
       elseif(
is_numeric($equa{$i}) && $i == 0)
       {
          
//gerer parenthese et autre terme generaux + gerer ^apres: 2^2
          
$final .= $equa{$i}."*";
       }
       elseif(
is_numeric($equa{$i}) && $i > 0 && $equa{$i-1} != "^")
       {
          
//gerer ^apres: 2^2
          
$final .= $equa{$i}."*";
       }
       elseif(
$equa{$i} == "^")
       {
           continue;
       }
       elseif(
is_numeric($equa{$i}) && $equa{$i-1} == "^")
       {
           continue;
       }
       else
       {
           if(
$equa{$i} == "x")
           {
              
$final .= 1;
           }
           else
           {
              
$final .= $equa{$i};
           }
       }
   }
  
//
   //Manage multiplication add in the previous string $final
   //
  
$finalMul = "";
   for(
$i = 0; $i < strlen($final); $i++)
   {
       if(
is_numeric($final{$i}) && $final{$i+1} == "*" && is_numeric($final{$i+2}))
       {
          
$finalMul .= $final{$i}*$final{$i+2};
       }
       elseif(
$final{$i} == "*")
       {
           continue;
       }
       elseif(
is_numeric($final{$i}) && $final{$i+1} != "*" && $final{$i-1} == "*")
       {
           continue;
       }
       else
       {
          
$finalMul .= $final{$i};   
       }
   }
   echo
"equa final: ".$finalMul;
}
?>

I know this is not optimal but i've done this quick :)
If you guys have any comment just email me.
I also want to do this fonction In C to add to phpCore maybe soon...
Patoff
moc.erawtfostym@evad
27-May-2004 10:04
The fastest O(1) factorial function has a lookup table of all the factorials that fit within the output range. With an array of the first 34 (float) or 170 (double) factorials, you get identical results in a fraction of the time.
churkl at hotmail dot com
26-May-2004 04:32
Here is my factorial function which i think is very simple and without any confusion. email me comments if you like if i had something wrong.

<?php
function factorial($number)
{
  
$temp = 1;
   while (
$number > 1){
      
$temp *= $number--;
   }
   return
$temp;
}
?>
ausvald at tut dot by
01-May-2004 08:48
I see there are some factorial functions below.

I'll provide the best one:

<?
function factorial($n){ $n=(int)$n;
 
$f=1;
  for(;
$n>0;--$n) $f*=$n;
  return
$f;
}
?>
florian at shellfire dot de
29-Apr-2004 05:48
Please note that shorter is not always better
(meaning that really short faculty implementation above).

In my opinion, a clearer way to code this is, including a check
for negative or non-integer values.

In order to calculate the faculty of a positive integer,
an iterative way (which might be harder to understand)
is usually a bit faster, but I am using it only for small
values so it is not really important to me:

<?php

  
// Calculate the Faculty of a positive int-value
  
function iFaculty($a_iFac)
   {
     if (
$a_iFac > 0)
     {
         return
$a_iFac * $this->iFaculty($a_iFac - 1);
     }
     elseif (
$a_iFac == 0)
     {
         return
1;
     }
     else
     {
         return
0// Wrong argument!
    
}
   }
?>

I've also written another function to calculate the
binomial coefficient of 2 values, I didn't find it anywhere yet so I hope it might help someone (works fine with the above stated faculty-function and ready to be used inside of your own classes!)

<?php

  
// calculates the binomial coefficient "n over k" of 2 positive int values
   // fr n >= k
  
function iBinCoeff($a_iN, $a_iK)
   {
      
// the binomial coefficient is defined as n! / [ (n-k)! * k! ]
      
return $this->iFaculty($a_iN) / ($this->iFaculty($a_iN - $a_iK) * $this->iFaculty($a_iK));   
   }

?>
Chronial "at" cyberpunkuniverse.de
14-Jan-2004 05:47
Here are are a nPr and a nPc function
(had to define NaN - don't know, how to this the "rigth" way)

<?php
define
(NaN,acos(1.01));

function
nCr($n,$r){
   if (
$r > $n)
     return
NaN;
   if ((
$n-$r) < $r)
     return
nCr($n,($n-$r));
  
$return = 1;
   for (
$i=0;$i < $r;$i++){
    
$return *= ($n-$i)/($i+1);
   }
   return
$return;
}

function
nPr($n,$r){
   if (
$r > $n)
     return
NaN;
   if (
$r)
     return
$n*(nPr($n-1,$r-1));
   else
     return
1;
}
?>
chris at free-source dot com
08-Oct-2003 12:37
to "convert" scientific notation to a float simply cast it:
<?php
$val
= '3.5e4';
$val = (float) $val;
echo
$val;
?>

output:
35000
jl85 at yahoo dot com
05-Oct-2003 07:00
Here's yet another greatest common denominator (gcd) function, a reeeeally small one.

function gcd($n,$m){
if(!$m)return$n;return gcd($m,$n%$m);
}

It works by recursion. Not really sure about it's speed, but it's really small! This won't work on floating point numbers accurately though. If you want a floating point one, you need to have at least PHP 4, and the code would be

function gcd($n,$m){
if(!$m)return$n;return gcd($m,fmod($n,$m));
}
fabien_mornand at yahoo dot fr
01-Oct-2003 06:46
here is an algorithm to calculate gcd of a number. This is Euclid algorithm i was studying in Maths. I've converted it in php for the fun.

<?php
 
if($a && $b)
  {
$ax=$a; $bx=$b;
  
$r=fmod($a,$b);
  if(!
$r){$rx=$r;}
   while(
$r){
  
$rx=$r;
  
$a=$b;
  
$b=$r;
  
$r=fmod($a,$b);
   }
   }
echo
'PGCD ('.$ax.' , '.$bx.' ) = '.$rx;
?>
jordanolsommer at imap dot cc
27-Aug-2003 11:07
The reason the bitwise AND ("&") operator works to determine whether a number is odd or even is because odd numbers expressed in binary always have the rightmost (2^0) bit = 1 and even numbers always have the 2^0 bit = 0.

So if you do a " 1 & $num", it will return zero if the number is even (since xxxxxxx0 [the even number in binary] and 00000001 [the 1]) don't share any bits, and will return 1 if the number is odd (xxxxxx1 and 000001).

a clever way of doing things, but $num % 2 would work as well i think :).
matthew_gaddis at yahoo dot com
25-Apr-2003 05:23
Here is a cleaner factorial function:

function factorial($s){
   if($s) $r = $s * factorial($s - 1);
   else $r = 1;
   return $r;
}
jerry dot wilborn at fast dot net
17-Apr-2003 02:10
Here is how to calculate standard deviation in PHP where $samples is an array of incrementing numeric keys and the values are your samples:

$sample_count = count($samples);

for ($current_sample = 0; $sample_count > $current_sample; ++$current_sample) $sample_square[$current_sample] = pow($samples[$current_sample], 2);

$standard_deviation = sqrt(array_sum($sample_square) / $sample_count - pow((array_sum($samples) / $sample_count), 2));
jl85 at yahoo dot com
23-Feb-2003 12:04
Theres another faster way of doing even/odd number checking by using bitwise operators. Don't ask me how it works, I just found this out by experimenting with it (could the editor possibly explain?)

if ((1&$num)) {
 echo "$num is odd";
}

if (!(1&$num)) {
 echo "$num is even";
}

How it works is (1&$num) returns a 1 for odd numbers and returns 0 when it's an even number.
php at casaforge dot com (Hal)
01-Feb-2003 09:25
This might be useful in generating fractional numbers for construction, if only because most carpenters would rather put a nail in your foot than hear about any number that ends with .8125".

Since I couldn't figure out the fraction code above, this is my simple-minded take on the problem. Also, align by "char" doesn't seem to work yet in html, so it seems necessary to use tables (egad!) to make numbers align properly. The following code illustrates a way to make a dynamically sized table with aligned fractions from an array of random numbers. Since I don't care about fractions less than 1/16, this rounds them into oblivion. Also, it sorts the list from long to short and collates multiples in the array. One bit of cleverness here (gleaned from these pages) that might not be obvious: I'm using 1 *bitwise and* (1 &) to determine odd numbers.

If you copy and paste the following code, try refreshing the page a few times to see how the table adjusts itself.

<?php

// get some numbers to play with

$x = rand(0,130000)/10;
$y = rand(0,1200);
$z = rand(0,4)/64;
$array = array($x, $x, $x, $y, $y, $z, 324.19, 425/7, sqrt(2), pi(), pi());

// functions

function mult($n) { return intval (round ($n*16)); }

function
frac($num) { $mod = fmod ($num,1)*16;
if (
1 & $mod) { return " - ".$mod."/16"; }
else
$mod = $mod/2;
if (
1 & $mod) { return " - ".$mod."/8"; }
else
$mod = $mod/2;
if (
1 & $mod) { return " - ".$mod."/4"; }
else
$mod = $mod/2;
if (
1 & $mod) {return " - ".$mod."/2";}
}

// make a table

echo '<table>';
$array = array_map("mult", $array);
$array = (array_filter($array, strval)); //get rid of zeros
$array = (array_count_values ($array));
krsort ($array);
while (list (
$key, $val) = each ($array)) {
$key = $key/16;
echo
"<tr><td>$val</td><td>&nbsp; @ &nbsp;</td><td align=\"right\">".intval($key)." </td><td> ".frac($key)." </td></tr>";
}
echo
'</table>';

?>
nazgul26 (at_sign) windfox dot net
09-Dec-2002 06:58
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>";
   }
}
?>
jbeardsl at gte dot net
10-Nov-2002 02:36
I needed a truncate function to operate on real numbers. I preferred not to use a string-manipulation method, so here's my solution. HTH...

function truncate ($num, $digits = 0) {

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

   $shift = pow(10 , $digits);
   return ((floor($num * $shift)) / $shift);

}
jbeardsl [found_at] gte [d0t] net
09-Nov-2002 05:15
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(10, $digits);
   return ((
floor($num * $shift)) / $shift);
}
?>
patience at worldonline dot nl
06-Aug-2002 12:08
The example for Factorials given above is wrong. Here a correct version, so that you do not have to reinvent the wheel again...

<?php
function mathFact( $s )
{
 
$r = (int) $s;

  if (
$r < 2 )
  
$r = 1;
  else {
   for (
$i = $r-1; $i > 1; $i-- )
    
$r = $r * $i;
  }

  return(
$r );
}
?>
shanx at shanx dot com
09-Jul-2002 04:13
<?

/**
 * Function to calculate base36 values from a number. Very
 * useful if you wish to generate IDs from numbers.
 *
 * @param $value The number
 * @param $base The base to be applied (16, 36 or 64)
 * @return The calculated string
 * @author Shashank Tripathi (shanx@shanx.com)
 * @version 0.1 - Let me know if something doesnt work
 *
 */
 
function base36($value, $base)
{
  
$baseChars = array('0', '1', '2', '3', '4', '5',
                      
'6', '7', '8', '9', 'a', 'b',
                      
'c', 'd', 'e', 'f', 'g', 'h',
                      
'i', 'j', 'k', 'l', 'm', 'n',
                      
'o', 'p', 'q', 'r', 's', 't',
                      
'u', 'v', 'w', 'x', 'y', 'z'
                    
);

  
$remainder = 0;
  
$newval = "";
  
   while (
$value > 0 )
   {
      
$remainder = $value % $base;
      
$value = ( ($value - $remainder)/ $base );
      
$newval .= $baseChars[$remainder];
   }
   return
strrev($newval);
  
}

echo
"The string for 46655, for instance, is " . base36(46655, 36);

?>
webkid%webkid.com
01-Jun-2002 06:54
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!
webkid%webkid.com
01-Jun-2002 06:49
I found it kind of irritating that PHP had no native functionality for a calculating Factorials. Since I really didn't feel like loading the GMP library, I figured I'd write my own function.

function fact($s){$r=(int)$s; for ($i=$r;$i--;$i>1){$r=$r*$i;} return $r;}

I think that's right... I havn't tested it extensively but it should work.
cornelius at skjoldhoej dot dk
13-Jun-2001 05:03
I found that when dealing with tables, a 'least common multiple' function is sometimes useful for abusing tablespan and the likes.

So here goes (you may choose to remove the first part of the gcd function if the function call is well-behaved):

<?php
function gcd(n, m) //greatest common divisor
{
  
n=abs(n); m=abs(m);
   if (
n==0 and m==0)
       return
1; //avoid infinite recursion
  
if (n==m and n>=1)
       return
n;
   return
m<n?gcd(n-m,n):gcd(n,m-n);
}

function
lcm(n, m) //least common multiple
{
   return
m*(n/gcd(n,m));
}
?>

This may or may not be something to consider adding to the mathematical function library.
ian at mp3 dot com
20-Feb-2001 07:43
for those looking for a credit card verification function i wrote a simple LUHN Formula algorithm:

<?php
$valid
= 1;

$numOfDigits = 0 - strlen($ccNumber);

$i = -1;
while (
$i>=$numOfDigits){
  if ((
$i % 2) == 0){
  
$double = 2*(substr($ccNumber, $i, 1));
  
$total += substr($double,0,1);
   if (
strlen($double > 1)){
    
$total += substr($double,1,1);
   }
  } else {
  
$total += substr($ccNumber, $i, 1);
  }
 
$i--;
}

if ((
$total % 10) != 0){
 
$valid = 0;
}
?>
cathody at mail dot ru
11-Aug-2000 10:55
Converting non-standard form:

you can use something like this:

<?php
$v
=0.3e-9;
$v=sprintf ( "%2.9f", $v);
?>