Modulus operator is supposed to show the remainder. Like for echo(34%100)
outputs 34
. But why do i get a "Division by zero
" error for this code echo(34%4294967296)

- 7,761
- 16
- 33
- 49

- 2,802
- 7
- 33
- 59
-
1use `echo fmod(34, 4294967296);` prints `34` – Aug 06 '13 at 00:20
-
use `echo bcmod('34', '4294967296');` [prints `34`](http://3v4l.org/5lYTP) – Dave Chen Aug 06 '13 at 00:22
-
both fmod and bcmod works, now i'm confused which one to use. I'm handling huge number, which function do you suggest, bcmod or fmod? – Nok Imchen Aug 06 '13 at 00:27
4 Answers
4294967296
is 2^32
and cannot be represented as 32 bit number - it wraps back to 0. If you use 64-bit version of PHP, it may work.
You might be able to use floating point modulus fmod to get what you want without overflowing.

- 111,019
- 13
- 122
- 148
-
yes, i dound that explanation in http://computer-programming-forum.com/53-perl/f2805415f84c54e1.htm, but how to overcome the bug? is there any fix or another way round? – Nok Imchen Aug 06 '13 at 00:15
-
264-bit PHP doesn't yet support 64-bit integers on Windows either, so even that wouldn't help. – garrettmurray Aug 06 '13 at 00:17
-
-
ye, fmod work wonders :) I'm unable to accept your solution as answer, geting this error "You can accept an answer in 3 mins" – Nok Imchen Aug 06 '13 at 00:21
I found this question searching for "division by zero error when using modulus", but the reason why was different.
Modulus (the %
operator) will not work when the denomenator is is less than 1. using fmod() solves the problem.
Example:
$num = 5.1;
$den = .25;
echo ($num % $den);
// Outputs Warning: Division by zero
echo fmod($num, $den);
// Outputs 0.1
$num = 5.1;
$den = 1;
echo ($num % $den);
// Outputs 0, which is incorrect
echo fmod($num, $den);
// Outputs 0.1, which is correct

- 25,694
- 7
- 76
- 115
https://bugs.php.net/bug.php?id=51731
2^31 is the largest integer you can get on Windows.
If you still want to mod large numbers, use bcmod.

- 10,887
- 8
- 39
- 67
-
-
Fmod casts both sides to float, bcmod uses an "arbitrary precision number" (it's own numeric types). – Neil Neyman Aug 06 '13 at 00:28
-
@NeilNeyman ah, so fmod shows exact value while bcmod rounds the value? – Nok Imchen Aug 06 '13 at 00:29
-
3@Nok: I think this is a good question: What is the best function for computing modules in PHP? or difference between `fmod()` , `bcmod()` and `gmp_mod()` – Aug 06 '13 at 00:30
-
Not necessarily. Floating point math can be another source of problems, although I'm not sure this applies to mod division. But no -- arbitrary precision in this context just means it will work with numbers that go beyond max_int or min_int or that have more decimal places than the built-in php types allow. – Neil Neyman Aug 06 '13 at 00:32
-
2@nok: see the warning on this page also has a link to what arbitrary length types are. http://www.php.net/manual/en/language.types.float.php – Neil Neyman Aug 06 '13 at 00:35
There's a lot of reports of mod getting wonky with large integers in php. Might be an overflow in the calculation or even in that number itself which is going to give you bugs. Best to use a large number library for that. Check out gmp or bcmath.

- 2,116
- 16
- 21