PHP : Function Reference : BCMath Arbitrary Precision Mathematics Functions : bcmod
Example 339. bcmod() example
<?php echo bcmod('4', '2'); // 0 echo bcmod('2', '4'); // 2 ?>
sebas
function bc_is_even($int_str) {
return (int)!($int_str & 1);
}
More resource efficient version of 'bc_is_even'.
cristiandotzuddas nospam gmaildotcom
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;
}
?>
jmullan
<?php
function bc_is_even($int_str) {
if (0 < strlen($int_str)) {
return !((substr($int_str, -1)) % 2);
} else {
// error
return 0;
}
}
?>
lauris
<?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 );
?>
|
|