BC 数学 函数
在线手册:中文  英文

bcmod

(PHP 4, PHP 5)

bcmodGet modulus of an arbitrary precision number

说明

string bcmod ( string $left_operand , string $modulus )

Get the modulus of the left_operand using modulus.

参数

left_operand

The left operand, as a string.

modulus

The modulus, as a string.

返回值

Returns the modulus as a string, or NULL if modulus is 0.

范例

Example #1 bcmod() example

<?php
echo bcmod('4''2'); // 0
echo bcmod('2''4'); // 2
?>

参见


BC 数学 函数
在线手册:中文  英文

用户评论:

drahoszdenek at gmail dot com (2013-02-01 16:44:23)

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
?>

admin at deosnet dot com (2012-12-20 14:59:54)

This function is also equivalent to :

<?php
    
echo 4%2// 0
    
echo 2%4// 2
?>

ArleCamille (okw1003 at gmail dot com) (2009-11-16 04:47:55)

GCD made in Euclidian algorithm:
<?php
function bcgcd($a$b)
{
    if(
$a $b)
    {
        
$t $b;
        
$b $a;
        
$a $t;
        unset(
$t);
    }
    if(
bcmod($a$b) == "0") return $b;
    else return 
bcgcd($bbcmod($a$b));
}
?>
hope this helps.

mcuelenaere at gmail dot com (2007-12-21 13:00:54)

Heres an useful IBAN generate function:

<?php
function controleer_iban($iban) {
    
$iban str_replace(array(" ""  ""   ""\t"), ""$iban);
    
$iban strtoupper(str_replace(" """$iban));
    if(
strlen($iban)>34)
        return 
false;
    
$acceptabel "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 1 2 3 4 5 6 7 8 9 0";
    
$acceptabel explode(" "$acceptabel);
    for(
$i 0$i<strlen($iban); $i++) {
        if(
in_array(substr($iban$i1), $acceptabel) === false)
            return 
false;
    }
    
$alfa "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z";
    
$alfa explode(" "$alfa);
    for(
$i 1$i<27$i++) {
        
$alfa_replace[] = $i+9;
    }
    
$controlegetal str_replace($alfa$alfa_replacesubstr($iban4strlen($iban)-4).substr($iban02)."00");
    
$controlegetal 98 - (int)bcmod($controlegetal,97);
    if((int)
$controlegetal === (int)substr($iban22))
        return 
true;
    else
        return 
false;    
}
?>

sebas at stageprikbord dot nl (2007-03-27 06:53:09)

function bc_is_even($int_str) {
return (int)!($int_str & 1);
}
More resource efficient version of 'bc_is_even'.

lauris at night dot lt (2003-12-23 08:04:35)

<?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$x0$take );
        
$x substr$x$take );
        
$mod $a $y;    
    } 
    while ( 
strlen($x) );

    return (int)
$mod;
}

// example
echo my_bcmod"7044060001970316212900"150 );
?>

易百教程