bcmod

(PHP 3, PHP 4, PHP 5)

bcmod -- Get 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.

范例

例 1. bcmod() example

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

参见

bcdiv()


add a note add a note User Contributed Notes
sebas at stageprikbord dot nl
27-Mar-2007 02:53
function bc_is_even($int_str) {
  return (int)!($int_str & 1);
}

More resource efficient version of 'bc_is_even'.
jmullan at visi dot com
24-Jul-2006 10:12
<?php
function bc_is_even($int_str) {
   if (
0 < strlen($int_str)) {
       return !((
substr($int_str, -1)) % 2);
   } else {
      
// error                                                                                               
      
return 0;
   }
}
?>
cristianDOTzuddas]NOSPAM[gmailDOTcom
24-Jul-2005 10:45
BC Math is really cool, but has only 10 functions. So we MUST write other BC functions and share the code.

Here is a simple even/odd number check.

<?
function bc_is_even($int_str) {
   if (
strlen($int_str)>0) {
       if (
bcmod($int_str, 2)==='0')
           return
true;
       else
           return
false;
   }
   else       
// error
      
return 0;
}
?>
lauris at night dot lt
23-Dec-2003 04:04
<?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 );
?>