★ wanayoo — archive 1999 http://fr.php.net/manual/fr/ref.bc.phpNouvelle recherche | Portail wanayoo
PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links 
search for in the  
previousaspell_suggestbcaddnext
Last updated: Tue, 09 Jul 2002
view the printer friendly version or the printer friendly version with notes or change language to English | Brazilian Portuguese | Chinese | Czech | Dutch | Finnish | German | Hungarian | Italian | Japanese | Korean | Polish | Romanian | Russian | Spanish | Swedish | Turkish

IV. Nombres de grande taille

En PHP 4, ces fonctions ne sont disponibles que si l'option de configuration --enable-bcmath a été activée lors de la compilation. En PHP 3, ces fonctions ne sont disponibles que si l'option de configuration --disable-bcmath a été n' pas été activée lors de la compilation.

Note : Suite aux changement de licence, la librairie BCMATH est désormais distribuée séparemment. Vous pouvez télécharger l'archive à http://www.php.net/extra/number4.tar.gz. Lisez attentivement le fichier README.BCMATH de la distribution PHP.

Table des matières
bcadd -- Additionne deux nombres de grande taille.
bccomp -- Compare deux nombres de grande taille.
bcdiv -- Divise deux nombres de grande taille.
bcmod --  Retourne le reste d'une division entre nombre de grande taille.
bcmul -- Multiplie deux nombres de grande taille.
bcpow --  Elève un nombre à la puissance n-ième.
bcscale --  Détermine le nombre de décimales par défaut
bcsqrt --  Renvoie la racine carrée d'un nombre de grande taille.
bcsub --  Soustrait un nombre de grande taille à un autre.
User Contributed Notes
Nombres de grande taille
add a note about notes
raarts@netland.nl
06-Apr-2001 10:23

The README.BCMATH currently reads:

As of PHP 4.0.4, the BC math library routines are bundled in the
standard PHP distribution. There is no need to install any additional
files.

Thanks goes to Phil Nelson, for releasing the BC math library routines
under the LGPL.

benjcarson at digitaljunkies ca
07-Jul-2002 10:17

Note that bcmath doesn't seem to handle numbers in exponential notation (i.e. "1e4"), although PHP considers such a value a number.

example:

$exp1 = "1E5";
$exp2 = "2E4";
$ans1 = bcadd($exp1, $exp2, 3);
$ans2 = $exp1 + exp2;
echo("bcadd: $exp1 + $exp2 = $ans1");
echo("php: $exp1 + $exp2 = $ans2");

// Output:
bcadd: 1E5 + 2E4 = 0.000
php: 1E5 + 2E4 = 120000

Just a gotcha if you're using passing PHP numbers into bcmath functions...

benjcarson at digitaljunkies dot ca
07-Jul-2002 11:00

In addition to my last note, here are a quick pair of functions to convert exponential notation values into bcmath-style number strings:

// exp2int converts numbers in the
// form "1.5e4" into strings
function exp2int($exp) {
list($mantissa, $exponent) = spliti("e", $exp);
list($int, $dec) = split("\.", $mantissa);
bcscale ($dec);
return bcmul($mantissa, bcpow("10", $exponent));
}

// float2exp converts floats into exponential notation
function float2exp($num) {

if (0 == $num) { return "0E1";}
list($int, $dec) = split("\.", $num);

// Extract sign
if ($int[0] == "+" || $int[0] == "-") {
$sign = substr($int, 0,1);
$int = substr($int, 1);
}

if (strlen($int) <= 1) { // abs($num) is less than 1
$i=0;
for ($i=0; $dec[$i]=='0' && $i < strlen($dec); $i++);
$exp = -$i-1;
$mantissa = substr($dec,$i,1).".".substr($dec,$i+1);
} else { // abs($num) is greater than 1
$i=0;
for ($i=0; $int[$i]=='0' && $i < strlen($int); $i++);
$exp = strlen($int)-1 - $i;
$mantissa = substr($int,$i,1).".".substr($int,$i+1).$dec;
}

return ($sign . $mantissa . "E" . $exp);
}

add a note about notes
previousaspell_suggestbcaddnext
Last updated: Tue, 09 Jul 2002
show source | credits | stats | mirror sites
Copyright © 2001, 2002 The PHP Group
All rights reserved.
This mirror generously provided by: nexen.net
Last updated: Sat Aug 31 04:35:55 2002 CEST