PHP modulus not seeing that there is a remainder

C++, C#, Java, PHP, ect...
Post Reply
Joncom
Posts: 9
Joined: Fri Apr 20, 2012 5:00 am

PHP modulus not seeing that there is a remainder

Post by Joncom »

Why is it that when I run this script

Code: Select all

<?php
die("600851475143 mod 3 = " . 600851475143 % 3);
?>
it returns this output
600851475143 mod 3 = 0
but when I perform the math operator 600851475143 / 3 on my Windows calculator I'm given the answer
200283825047.66666666666666666667
Obviously there IS a remainder. So why does PHP not see it?

PS - Is it just me?
User avatar
Chris
Posts: 1580
Joined: Wed Sep 30, 2009 7:22 pm

Re: PHP modulus not seeing that there is a remainder

Post by Chris »

Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
MikeD
Posts: 294
Joined: Thu Sep 08, 2011 4:28 am

Re: PHP modulus not seeing that there is a remainder

Post by MikeD »

Joncom wrote:Why is it that when I run this script

Code: Select all

<?php
die("600851475143 mod 3 = " . 600851475143 % 3);
?>
it returns this output
600851475143 mod 3 = 0
but when I perform the math operator 600851475143 / 3 on my Windows calculator I'm given the answer
200283825047.66666666666666666667
Obviously there IS a remainder. So why does PHP not see it?

PS - Is it just me?
Unless i'm missing something, to divide you use / in PHP
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: PHP modulus not seeing that there is a remainder

Post by Xaleph »

The thing is, PHP doesn`t handle large numbers well. If the number is too big, an overlfow occurs and I think ( not sure!) that the overflow returns -1.
You could look into a bigint implementation in PHP that should solve the issue.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: PHP modulus not seeing that there is a remainder

Post by Jackolantern »

MikeD wrote:Unless i'm missing something, to divide you use / in PHP
Modulus is different than simple division. Division with / returns how many groups of x you can put y into, including the left-overs (remainder) as a decimal. Modulus or "mod" with % only returns the remainder as an integer. Kind of like elementary school division where instead of figuring out the decimal, you just put an "R" and some number with what was left after you divided out the whole numbers.

Modulus is useful for a host of calculations, one of which is figuring when to move to the next row in an animation sheet calculation in traditional game development 8-)
The indelible lord of tl;dr
Joncom
Posts: 9
Joined: Fri Apr 20, 2012 5:00 am

Re: PHP modulus not seeing that there is a remainder

Post by Joncom »

Xaleph wrote:The thing is, PHP doesn`t handle large numbers well.
Yes. So, apparently my number was too large:

Code: Select all

2^32        ->   4,294,967,296
My number   -> 600,851,475,143
The solution I ended up using was to use bcmod(), a modulus function which handles arbitrarily long integers.

Cheers.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: PHP modulus not seeing that there is a remainder

Post by Jackolantern »

Good to know! Thanks :)
The indelible lord of tl;dr
Post Reply

Return to “Coding”