Page 1 of 1

Maths problem

Posted: Fri Jan 02, 2004 5:53 am
by lazersam
Hi all,
My php script needs to find the remainder value from two numbers.

For example, $a=6, $b=7 thus $remainder = 1

Does anyone know the formula to calculate this?

Regards

Lawrence.

Posted: Fri Jan 02, 2004 5:58 am
by JAM
Perhaps...

Code: Select all

<?php
    echo (7 % 5);
    // 2
?>

Posted: Fri Jan 02, 2004 6:40 am
by lazersam
JAM wrote:Perhaps...

Code: Select all

<?php
    echo (7 % 5);
    // 2
?>
Not really working. (7 % 2) needs to be 5 but results as 1

Posted: Fri Jan 02, 2004 6:53 am
by m3mn0n
The following is good if you always want the smaller number to be subtracted from the larger one.

Code: Select all

<?php
function cmp($a, $b) {
   if ($a == $b) {
       return 0;
   }
   return ($a < $b) ? -1 : 1;
}

$a = 6;
$b = 7;

$array = array($a, $b);

usort($array, "cmp");

$remainder = $array[1] - $array[0];

echo $remainder; // returns 1
?>
Reference link: [php_man]usort[/php_man]()

Posted: Fri Jan 02, 2004 6:56 am
by lazersam
Thanks sami.

Posted: Fri Jan 02, 2004 6:57 am
by itsmani1

Code: Select all

<?php
$a=11;
$b=4;
$c = $a%$b;
echo $c;
?>

Posted: Fri Jan 02, 2004 7:04 am
by m3mn0n
@lazersam: np =)

@itsmani1:
itsmani1 wrote:

Code: Select all

<?php
$a=11;
$b=4;
$c = $a%$b;
echo $c;
?>
That doesn't work because if $b is larger than $a, $c will always be equal to $a.

Posted: Fri Jan 02, 2004 7:07 am
by itsmani1
Sami wrote:@lazersam: np =)

@itsmani1:
itsmani1 wrote:

Code: Select all

<?php
$a=11;
$b=4;
$c = $a%$b;
echo $c;
?>
That doesn't work because if $b is larger than $a, $c will always be equal to $a.

that may be possible but i donot know i tmay happen
thankssssssssssssss

Posted: Fri Jan 02, 2004 7:10 am
by m3mn0n
It will always happen. Well for me anyway, and with the numbers I tested.

Code: Select all

<?php
$a=11; 
$b=12; 
$c = $a%$b; 
echo $c; // returns 11

$a=5; 
$b=38; 
$c = $a%$b; 
echo $c; // returns 5

$a=26; 
$b=353; 
$c = $a%$b; 
echo $c; // returns 26
?>

Posted: Fri Jan 02, 2004 8:25 am
by itsmani1
Sami wrote:It will always happen. Well for me anyway, and with the numbers I tested.

Code: Select all

<?php
$a=11; 
$b=12; 
$c = $a%$b; 
echo $c; // returns 11

$a=5; 
$b=38; 
$c = $a%$b; 
echo $c; // returns 5

$a=26; 
$b=353; 
$c = $a%$b; 
echo $c; // returns 26
?>



Okay Thanksssssss alot

Posted: Fri Jan 02, 2004 9:30 am
by JAM
lazersam wrote:
JAM wrote:Perhaps...

Code: Select all

<?php
    echo (7 % 5);
    // 2

?>
Not really working. (7 % 2) needs to be 5 but results as 1
True, I was thinking about reminder as "Remainder of $a divided by $b." and not subtraction as you wanted.

Code: Select all

$a = 7;
    $b = 2;
    echo abs($a - $b);
Sufficient, no matter what number is the bigger one...