phpday 2027 - Call for Papers

bcmod

(PHP 4, PHP 5, PHP 7, PHP 8)

bcmod — Ottiene il modulo di un numero a precisione arbitraria

Descrizione

function bcmod(string $num1, string $num2, ?int $scale = null): string

Ottiene il resto della divisione di num1 per num2. Il risultato ha lo stesso segno di num1.

Elenco dei parametri

num1

L'operando di sinistra, come stringa.

num2

L'operando di destra, come stringa.

scale

Questo parametro viene utilizzato per impostare il numero di cifre dopo la virgola nel risultato. Se è null, verrà utilizzato il valore predefinito impostato con bcscale(), o in alternativa il valore della direttiva INI bcmath.scale.

Valori restituiti

Restituisce il modulo come stringa.

Errori/Eccezioni

Questa funzione genera un'eccezione ValueError nei seguenti casi:

  • num1 o num2 non è una stringa numerica BCMath ben formata.
  • scale è al di fuori dell'intervallo valido.

Questa funzione genera un'eccezione DivisionByZeroError se num2 è 0.

Log delle modifiche

Versione Descrizione
8.0.0 scale ora è nullable.
8.0.0 La divisione per 0 ora lancia un'eccezione DivisionByZeroError invece di restituire null.
7.2.0 num1 e num2 non vengono più troncati a intero, quindi ora il comportamento di bcmod() segue fmod() anziché l'operatore %.
7.2.0 È stato aggiunto il parametro scale.

Esempi

Example #1 Esempio di bcmod()

<?php
bcscale(0);
echo bcmod( '5',  '3'); //  2
echo bcmod( '5', '-3'); //  2
echo bcmod('-5',  '3'); // -2
echo bcmod('-5', '-3'); // -2
?>

Example #2 bcmod() con decimali

<?php
bcscale(1);
echo bcmod('5.7', '1.3'); // 0.5 a partire da PHP 7.2.0; 0 in precedenza
?>

Vedere anche:

  • bcdiv() - Divide due numeri a precisione arbitraria
  • bcdivmod() - Get the quotient and modulus of an arbitrary precision number
  • BcMath\Number::mod() - Gets the modulus of an arbitrary precision number

+add a note

User Contributed Notes 2 notes

up
4
lauris at night dot lt
22 years ago
<?php
/**
 * my_bcmod - get modulus (substitute for bcmod)
 * string my_bcmod ( string left_operand, int modulus )
 * left_operand can be really big, but be carefull with modulus :(
 * by Andrius Baranauskas and Laurynas Butkus :) Vilnius, Lithuania
 **/
function my_bcmod( $x, $y )
{
    // how many numbers to take at once? carefull not to exceed (int)
    $take = 5;     
    $mod = '';

    do
    {
        $a = (int)$mod.substr( $x, 0, $take );
        $x = substr( $x, $take );
        $mod = $a % $y;    
    } 
    while ( strlen($x) );

    return (int)$mod;
}

// example
echo my_bcmod( "7044060001970316212900", 150 );
?>
up
1
drahoszdenek at gmail dot com
13 years ago
The modulus can be only integer. For "floats" bcmod returns 0:

<?php
echo bcmod('10', '2.1'); // 0
?>

For real modulus you can use BN-PHP project (hosted at Bitbucket):

<?php
$eval = new \BN\Expression\ExpressionEvaluator();
$operators = new \BN\Expression\OperatorsFactory();
$eval->setOperators($operators->getOperators(array('%')));
        
echo $eval->evaluate('10 % 2.1'); // 1.6
echo $eval->evaluate('10 % -2.1'); // 1.6
echo $eval->evaluate('-10 % 2.1'); // -1.6
echo $eval->evaluate('-10 % -2.1'); // -1.6
?>
To Top