php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #62729 fmod() and "modulo" operator do not work are not consistent with big int.
Submitted: 2012-08-03 00:15 UTC Modified: 2016-08-31 21:27 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (100.0%)
From: arnaud at wixiweb dot fr Assigned: cmb (profile)
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.4.5 OS: Linux / Windows
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: arnaud at wixiweb dot fr
New email:
PHP Version: OS:

 

 [2012-08-03 00:15 UTC] arnaud at wixiweb dot fr
Description:
------------
fmod() and "modulo" operator do not work are not consistent with big int.
Moreover, the results change between windows(PHP5.4) to linux(PHP5.3)
See the test script for more informations

Test script:
---------------
// On Windows64 with PHP 5.4 
// Example 1
var_dump(fmod(12345678901234567, 62));   // float(52)
var_dump(12345678901234567 % 62);        // int(20) 

// Example 2
var_dump(fmod(9999999999999999111, 62)); // float(18) 
var_dump(9999999999999999111 % 62);      // int(0)

// On Linux64 with PHP 5.3 
// Example 3
var_dump(fmod(12345678901234567, 62));   // float(52)
var_dump(12345678901234567 % 62);        // int(51)
// Example 4
var_dump(fmod(9999999999999999111, 62)); // float(18)
var_dump(9999999999999999111 % 62);      // int(-60)




Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-08-03 02:06 UTC] rasmus@php.net
Note that longs on 64-bit Windows are still only 32 bits. A long on 64-bit Linux 
is 64 bits. So, to work with large values like that you will need to use 
something like bcmod()

fmod(x,y) returns x - n * y where n is the quotient of x / y floor'ed and 
converted to an integer which isn't always going to give you the same result as 
x % y. If you are working with integers use %. If you are working with doubles, 
use fmod(). And keep in mind that these are signed so you don't have the whole 
32/64 bit space. The upper bit is the sign.

Given this I am not sure I see a bug here. You are mostly comparing 32-bit to 
64-bit here.
 [2012-08-03 02:06 UTC] rasmus@php.net
-Status: Open +Status: Analyzed
 [2016-08-31 21:27 UTC] cmb@php.net
-Status: Analyzed +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2016-08-31 21:27 UTC] cmb@php.net
As of PHP 7.0.0 true 64bit support for Windows is officially
available, so this issue is supposed to be solved. The difference
in example 3 has been already explained by Rasmus. The difference
in example 4 is due to the numerator being not an int at all, but
rather got converted to float before the modulus operation, see
<https://3v4l.org/pK8C8>.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Jul 14 02:01:32 2025 UTC