Maths problem
Moderator: General Moderators
Maths problem
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.
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.
Perhaps...
Code: Select all
<?php
echo (7 % 5);
// 2
?>Not really working. (7 % 2) needs to be 5 but results as 1JAM wrote:Perhaps...Code: Select all
<?php echo (7 % 5); // 2 ?>
The following is good if you always want the smaller number to be subtracted from the larger one.
Reference link: [php_man]usort[/php_man]()
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
?>
Last edited by m3mn0n on Fri Jan 02, 2004 7:20 am, edited 1 time in total.
- itsmani1
- Forum Regular
- Posts: 791
- Joined: Mon Sep 29, 2003 2:26 am
- Location: Islamabad Pakistan
- Contact:
Code: Select all
<?php
$a=11;
$b=4;
$c = $a%$b;
echo $c;
?>@lazersam: np =)
@itsmani1:
@itsmani1:
That doesn't work because if $b is larger than $a, $c will always be equal to $a.itsmani1 wrote:Code: Select all
<?php $a=11; $b=4; $c = $a%$b; echo $c; ?>
- itsmani1
- Forum Regular
- Posts: 791
- Joined: Mon Sep 29, 2003 2:26 am
- Location: Islamabad Pakistan
- Contact:
Sami wrote:@lazersam: np =)
@itsmani1:
That doesn't work because if $b is larger than $a, $c will always be equal to $a.itsmani1 wrote:Code: Select all
<?php $a=11; $b=4; $c = $a%$b; echo $c; ?>
that may be possible but i donot know i tmay happen
thankssssssssssssss
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
?>- itsmani1
- Forum Regular
- Posts: 791
- Joined: Mon Sep 29, 2003 2:26 am
- Location: Islamabad Pakistan
- Contact:
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
True, I was thinking about reminder as "Remainder of $a divided by $b." and not subtraction as you wanted.lazersam wrote:Not really working. (7 % 2) needs to be 5 but results as 1JAM wrote:Perhaps...Code: Select all
<?php echo (7 % 5); // 2 ?>
Code: Select all
$a = 7;
$b = 2;
echo abs($a - $b);