Maths problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
lazersam
Forum Contributor
Posts: 105
Joined: Sat Nov 15, 2003 4:07 am
Location: Hertfordshire, UK

Maths problem

Post 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.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Perhaps...

Code: Select all

<?php
    echo (7 % 5);
    // 2
?>
User avatar
lazersam
Forum Contributor
Posts: 105
Joined: Sat Nov 15, 2003 4:07 am
Location: Hertfordshire, UK

Post 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
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post 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]()
Last edited by m3mn0n on Fri Jan 02, 2004 7:20 am, edited 1 time in total.
User avatar
lazersam
Forum Contributor
Posts: 105
Joined: Sat Nov 15, 2003 4:07 am
Location: Hertfordshire, UK

Post by lazersam »

Thanks sami.
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post by itsmani1 »

Code: Select all

<?php
$a=11;
$b=4;
$c = $a%$b;
echo $c;
?>
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post 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.
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post 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
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post 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
?>
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post 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
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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...
Post Reply