php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #43572 fmod($a,$b) wrong when $b is a multiple of $a
Submitted: 2007-12-12 00:55 UTC Modified: 2007-12-18 11:51 UTC
From: csaba at alum dot mit dot edu Assigned:
Status: Not a bug Package: Math related
PHP Version: 5.2.5 OS: Win XP
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.
Password:
Status:
Package:
Bug Type:
Summary:
From: csaba at alum dot mit dot edu
New email:
PHP Version: OS:

 

 [2007-12-12 00:55 UTC] csaba at alum dot mit dot edu
Description:
------------
if $div evenly divides $num, fmod($num, $div) still returns a value not equal to 0 for almost all decimal $div, where the decimal portion is not n/2^k for some n and k.

e.g.
fmod(9.75,1.625) => 0 because .125 is 5/2^3


Expected result:
----------------
I expect to see fmod return 0 when the 1st argument is a multiple of the 2nd.

Csaba Gabor from Vienna

Actual result:
--------------
fmod(6.5, 1.3) => 1.3
which is incorrect

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-12-12 10:51 UTC] csaba at alum dot mit dot edu
Just to be clear:
fmod(9.75,1.625) => 0
Expected, and works correctly for me.

php -n -r "echo fmod(6.5, 1.3);"
=> 1.3
which is not correct (0 is correct)

Csaba

Note that on windows systems, double quotes are needed (because of the space) instead of the single quotes.
 [2007-12-18 11:51 UTC] felipe@php.net
Floating point values have a limited precision. Hence a value might 
not have the same string representation after any processing. That also
includes writing a floating point value in your script and directly 
printing it without any mathematical operations.

If you would like to know more about "floats" and what IEEE
754 is read this:
http://docs.sun.com/source/806-3568/ncg_goldberg.html
 
Thank you for your interest in PHP.

The value that you expect (0) is obtained using float precision. (In C)
PHP uses double, and with this type fmod() returns 1.3.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 19 18:01:33 2024 UTC