php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #53397 Modulo is not accurate with double
Submitted: 2010-11-24 15:12 UTC Modified: 2013-08-06 08:28 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: mryaggi at hotmail dot com Assigned:
Status: Verified Package: Scripting Engine problem
PHP Version: Irrelevant OS: All
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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)

Add a Patch

Pull Requests

Add a Pull Request

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>
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC