|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79338 fmod() not calculating properly
Submitted: 2020-03-03 11:20 UTC Modified: 2020-03-03 17:43 UTC
From: ziggi at ziggi dot pl Assigned:
Status: Not a bug Package: *Math Functions
PHP Version: 7.4.3 OS: Windows
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 this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: ziggi at ziggi dot pl
New email:
PHP Version: OS:


 [2020-03-03 11:20 UTC] ziggi at ziggi dot pl
From manual page:

I would say - simple answer provided by PHP team:

"The value that you expect (0) is obtained using float precision. (In C)
PHP uses double, and with this type fmod() returns 1.3."

I completely pointless. This is not math! This is plain bullshit!!!

Perl, MySQL, Python... you name it, all have no problems!!!

Test script:
echo fmod(75.6, 25.2);

Expected result:

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2020-03-03 11:35 UTC]
-Status: Open +Status: Not a bug
 [2020-03-03 11:35 UTC]
> cat test.c
#include <math.h>
#include <stdio.h>
int main() {
    printf("%f\n", fmod(75.6, 25.2));
    return 0;

> gcc test.c -lm
> ./a.out 
 [2020-03-03 11:38 UTC]
Also note:

$num = 75.6;
$denom = 25.2;
$res = fmod($num, $denom);
var_dump($res === $denom); // bool(false)

Further reading: <>
 [2020-03-03 17:09 UTC] ziggi at ziggi dot pl

My conclusion:

echo 2.0 * 2.0;
outcome: 5.0

OMG! Why 2 * 2 is 5 in PHP???

Answer from PHP team:
Because we use shitty C library with twisted precision so 2 * 2 is often 5. Enjoy or use Python.
 [2020-03-03 17:34 UTC]
> import math
> print(math.fmod(75.6, 25.2))

Surprising absolute nobody, Python returns the same result.


Get off my lawn.
 [2020-03-03 17:41 UTC] ziggi at ziggi dot pl

Not really good at all but obviously "obvious"...

>> Get off my lawn.

I do. Do not get upset so easy, man!
Thank you for your time, anyway.
 [2020-03-03 17:43 UTC]

$ perl -e 'use POSIX (); print POSIX::fmod(75.6, 25.2);'
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sun Feb 28 19:01:24 2021 UTC