I slapped together min() and max() functions using bccomp(). While min() and max() only take an arbitrary number of args (i.e. max(1, 5, 1235, 12934, 66)) bccomp only takes 2.
Note that this doesn't take into account $scale.
<?php
function bcmax() {
$max = null;
foreach(func_get_args() as $value) {
if ($max == null) {
$max = $value;
} else if (bccomp($max, $value) < 0) {
$max = $value;
}
}
return $max;
}
function bcmin() {
$min = null;
foreach(func_get_args() as $value) {
if ($min == null) {
$min = $value;
} else if (bccomp($min, $value) > 0) {
$min = $value;
}
}
return $min;
}
?>
bccomp
(PHP 4, PHP 5)
bccomp — Porovnať dve čísla s ľuboboľnou presnosťou
Popis
int bccomp
( string $left_operand
, string $right_operand
[, int $scale
] )
Porovnáva left_operand s right_operand om a vracia výsledok ako integer. Voliteľný parameter scale sa používa na nastavenie počtu číslic za desatinou čiarkou, čo sa využije pri porovnávaní. Vrátená hodnota je 0, ak sú oba operandy zhodné. Ak je left_operand väčší než right_operand , vráti sa hodnota +1 a ak je left_operand menší než right_operand , vráti sa hodnota -1.
Example#1 bccomp() príklad
<?php
echo bccomp('1', '2') . "\n";
echo bccomp('1.00001', '1', 3) . "\n";
echo bccomp('1.00001', '1', 5);
?>
Horeuvedený príklad vypíše:
-1 0 1
bccomp
frank at booksku dot com
04-Oct-2005 03:41
04-Oct-2005 03:41
11-Feb-2005 02:03
Note that the above function defeats the purpose of BCMath functions, for it uses the 'conventional' < operator.
Instead, it should be:
<?php
function my_bccomp_zero($amount, $scale)
{
if (@$amount{0}=="-")
{
return bccomp($amount, '-0.0', $scale);
}
else
{
return bccomp($amount, '0.0', $scale);
}
}
?>
