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

LII. Math�matiques

Introduction

Ces fonctions ne sont capables de manipuler que des entiers integer, ou nombres � virgule flottante (float). Si vous avez besoin de manipuler des nombres plus grands, reportez-vous aux fonctions math�matiques sur des nombres de grande taille.

Constantes math�matiques

Les valeurs suivantes sont d�finies comme des constantes en PHP :

Tableau 1. Constantes math�matiques

ConstanteValeurDescription
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]

Table des mati�res
Abs -- Valeur absolue
Acos -- arc cosinus
acosh -- Arc cosinus hyperbolique
Asin -- arc sinus
asinh -- Arc sinus hyperbolique
Atan -- arc tangent
Atan2 -- arc tangent de deux variables
atanh -- Arc tangeant hyperbolique
base_convert -- Convertit un nombre entre des bases arbitraires.
BinDec -- Convertit de binaire en d�cimal
Ceil -- Arrondit au nombre sup�rieur
Cos -- cosinus
cosh -- Cosinus hyperbolic
DecBin -- Convertit de d�cimal en binaire
DecHex -- Convertit de d�cimal en hexad�cimal
DecOct -- Convertit de d�cimal en octal
deg2rad --  Convertit un nombre de degr�s en radians
Exp -- exponentielle
expm1 --  Returns exp(number) - 1, computed in a way that accurate even when the value of number is close to zero
Floor -- Arrondi � l'entier inf�rieur
getrandmax -- Plus grande valeur al�atoire possible.
hexdec -- Convertit de hexad�cimal en d�cimal
hypot --  Returns sqrt( num1*num1 + num2*num2)
is_finite -- 
is_infinite -- 
is_nan -- 
lcg_value --  G�n�rateur de congruence combin�e lin�aire
Log -- Logarithme naturel (n�p�rien)
Log10 -- logarithme en base 10.
log1p --  Returns log(1 + number), computed in a way that accurate even when the val ue of number is close to zero
max -- La plus grande valeur.
min -- La plus petite valeur.
mt_getrandmax -- La plus grand valeur al�atoire possible.
mt_rand --  G�n�re une meilleure valeur al�atoire.
mt_srand -- Initialise une meilleure valeur al�atoire
number_format -- Formate un nombre pour l'affichage.
OctDec -- Convertit d'octal en d�cimal.
pi -- Retourne la valeur de pi
pow -- Puissance
rad2deg --  Convertit de radians en degr�s
rand -- G�n�re une valeur al�atoire.
round -- Arrondi.
Sin -- Sinus
sinh -- Sinyus hyperbolique
Sqrt -- Racine carr�e.
srand -- Initialise le g�n�rateur de nombres al�atoires
Tan -- Tangente
tanh -- Tangente hyperbolique
User Contributed Notes
Math�matiques
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, 11 Jun 2002
show source | credits | stats | mirror sites:  
Copyright © 2001, 2002 The PHP Group
All rights reserved.
This mirror generously provided by:
Last updated: Mon Jul 8 08:17:45 2002 CEST