PHP  
downloads | documentation | faq | getting help | | php.net sites | links 
search for in the  
previousmailparse_uudecode_allabsnext
Last updated: Tue, 28 May 2002
view this page in Printer friendly version | English | Brazilian Portuguese | Czech | Dutch | French | German | Hungarian | Italian | Japanese | Korean | Polish | Romanian | Russian | Spanish | Turkish

LII. Mathematical Functions

Introduction

These math functions will only handle values within the range of the integer and float types on your computer. (this corresponds currently to the C types long resp. double) If you need to handle bigger numbers, take a look at the arbitrary precision math functions.

Math constants

The following values are defined as constants in PHP by the math extension:

Taulu 1. Math constants

ConstantValueDescription
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]
Only M_PI is available in PHP versions up to and including PHP 4.0.0. All other constants are available starting with PHP 4.0.0. Constants labeled [4.0.2] were added in PHP 4.0.2.

Sis�llys
abs -- Absolute value
acos -- Arc cosine
acosh -- Inverse hyperbolic cosine
asin -- Arc sine
asinh -- Inverse hyperbolic sine
atan -- Arc tangent
atan2 -- arc tangent of two variables
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 -- e to the power of ...
expm1 --  Returns exp(number) - 1, computed in a way that accurate even when the value of number is close to zero
floor -- Round fractions down
getrandmax -- Show largest possible random value
hexdec -- Hexadecimal to decimal
hypot --  Returns sqrt( num1*num1 + num2*num2)
is_finite -- 
is_infinite -- 
is_nan -- 
lcg_value -- Combined linear congruential generator
log -- Natural logarithm
log10 -- Base-10 logarithm
log1p --  Returns log(1 + number), computed in a way that accurate even when the val ue of number is close to zero
max -- Find highest value
min -- Find lowest value
mt_getrandmax -- Show largest possible random value
mt_rand -- Generate a better random value
mt_srand -- Seed the better random number generator
number_format -- Format a number with grouped thousands
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 value
round -- Rounds a float
sin -- Sine
sinh -- Hyperbolic sine
sqrt -- Square root
srand -- Seed the random number generator
tan -- Tangent
tanh -- Hyperbolic tangent
User Contributed Notes
Mathematical Functions
add a note about notes

11-Aug-2000 01:55

Converting non-standard form:

you can use something like this:
$v=0.3e-9;
$v=sprintf ( "%2.9f", $v);


19-Feb-2001 09:43

for those looking for a credit card verification function i wrote a simple
LUHN Formula algorithm:

$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;
}


02-Mar-2001 05:02

Here is a pretty easy way to convert someone's weight in pounds to the
English stones and pounds:

$WeightStones = floor($weight/14);
$WeightPounds = ($weight%14);

echo $WeightStones."st ".$WeightPounds."lbs
(".$weight."lbs)";

If the person weighs 136lbs, "9st 10lbs" will be returned.

Hope this helps someone...


12-Jun-2001 08: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):

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.


webkid%webkid.com
31-May-2002 09: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.

webkid%webkid.com
31-May-2002 09: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!


30-Jun-2002 09:10

it's in swedish (scripts are short and easy to see). but i think that
thease functions should be implemented into PHP!



03-Jul-2002 09:40

Here are the functions that where listed in link above (they got lost).

# From Celsius

// To Fahrenheit
function celfar($c) {
return $c*1.8+32; }

// To Kelvin
function celkel($c) {
return $c+273.15; }

// To Rankine
function celran($c) {
return $c*1.8+491.67; }

// To R�aumur
function celrea($c) {
return $c*0.8; }

# From Fahrenheit

// To Celsius
function farcel($f) {
return ($f-32)*(5/9); }

// To Kelvin
function farkel($f) {
return ($f-32)*(5/9)+273.15; }

// To Rankine
function farran($f) {
return $f+459.67; }

// To R�aumur
function farrea($f) {
return ($f-32)*(4/9); }

# From Kelvin

// To Celsius
function kelcel($k) {
return $k-273.15; }

// To Fahrenheit
function kelfar($k) {
return ($k-273.15)*9/5+32; }

// To Rankine
function kelran($k) {
return $k*1.8; }

// To R�aumur
function kelrea($k) {
return ($k-273.15)*0.8; }

# From Rankine

// To Celsius
function rancel($r) {
return ($r*(5/9))-273.15; }

// To Fahrenheit
function ranfar($r) {
return $r-459.67; }

// To Kelvin
function rankel($r) {
return $r*(5/9); }

// To R�aumur
function ranrea($r) {
return ($r*(4/9))-218.52; }

# From R�aumur

// To Celsius
function reacel($re) {
return $re*1.25; }

// To Fahrenheit
function reafar($re) {
return $re*2.25+32; }

// To Kelvin
function reakel($re) {
return $re*1.25+273.15; }

// To Rankine
function rearan($re) {
return $re*2.25+491.67; }

add a note about notes
previousmailparse_uudecode_allabsnext
Last updated: Tue, 28 May 2002
show source | credits | stats | mirror sites:  
Copyright © 2001, 2002 The PHP Group
All rights reserved.
This mirror generously provided by:
Last updated: Sat Jul 6 00:05:55 2002 CEST