php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #53397 Modulo is not accurate with double
Submitted: 2010-11-24 15:12 UTC Modified: 2021-05-28 14:10 UTC
Votes:3
Avg. Score:2.3 ± 0.9
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: mryaggi at hotmail dot com Assigned: cmb (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: Irrelevant OS: All
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: mryaggi at hotmail dot com
New email:
PHP Version: OS:

 

 [2010-11-24 15:12 UTC] mryaggi at hotmail dot com
Description:
------------
In C++ :
1290594600000 % 86400000 = 37800000 //correct
But In PHP :
1290594600000 % 86400000 = 30811200 //wrong

However, in PHP : 
1290594600000 - (floor(1290594600000/86400000)*86400000) = 37800000 //correct!

Can modulo opertor be implemented this way when left value > PHP_INT_MAX ?

See Patch in attachement

Test script:
---------------
echo 1290594600000 % 86400000;
echo "\n";
echo 1290594600000 - (floor(1290594600000/86400000)*86400000);

Expected result:
----------------
37800000
37800000

Actual result:
--------------
30811200
37800000

Patches

ModuloForDouble (last revision 2010-11-24 14:13 UTC by mryaggi at hotmail dot com)

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-08-06 08:28 UTC] yohgaki@php.net
-Status: Open +Status: Verified -Package: Variables related +Package: Scripting Engine problem
 [2013-08-06 08:28 UTC] yohgaki@php.net
Test with 5.5.1 32bit. This issue exists still.
 [2013-09-18 21:50 UTC] cmbecker69 at gmx dot de
The operation a - (floor(a/b)*b) gives not the modulo, but the 
remainder.  Furthermore it seems the suggested patch does neither 
cater for large divisors nor large results.

It *might* be reasonable to stick with the current implementation,
and to state clearly in the documentation[1] that the modulus operator
gives correct results only for integers in the range
[-PHP_INT_MAX - 1, PHP_INT_MAX].  The current wording is somewhat
misleading, IMHO.

[1] <http://www.php.net/manual/en/language.operators.arithmetic.php>
 [2021-05-28 14:10 UTC] cmb@php.net
-Type: Bug +Type: Documentation Problem -Assigned To: +Assigned To: cmb
 [2021-05-28 14:10 UTC] cmb@php.net
The documentation had already been improved (mentioning fmod());
I'll clarify that the numbers are converted to int (it's not only
about stripping the decimal part).
 [2021-05-28 14:11 UTC] git@php.net
Automatic comment on behalf of cmb69
Revision: https://github.com/php/doc-en/commit/e4b889a3e8b9d87ab605aac9bbe85d2c16c69a1b
Log: Fix #53397: Modulo is not accurate with double
 [2021-05-28 14:11 UTC] git@php.net
-Status: Verified +Status: Closed
 [2021-05-28 22:06 UTC] git@php.net
Automatic comment on behalf of mumumu
Revision: https://github.com/php/doc-ja/commit/e3654c204e8b49233d52a57cbd999a77a71bdd78
Log: Fix #53397: Modulo is not accurate with double
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat May 03 01:01:29 2025 UTC