Bug #78073 [NEW]: echo fmod(25, 0.2); // 0.2, must be 0
| From: | e dot grinec at gmail dot com | Date: | Mon, 27 May 2019 09:51:43 +0000 |
| Subject: | Bug #78073 [NEW]: echo fmod(25, 0.2); // 0.2, must be 0 | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-221000@lists.php.net to get a copy of this message | ||
From: e dot grinec at gmail dot com
Operating system: Windows 10 x64
PHP version: 7.3.5
Package: *Math Functions
Bug Type: Bug
Bug description:echo fmod(25, 0.2); // 0.2, must be 0
Description:
------------
---
From manual page: https://php.net/function.fmod
---
PHP 5.2.x through PHP 7.3.2 still the same problem.
Code:
<?php
echo fmod(-25, 0.2); // -0.2, should be 0
echo -25 - floor(-25/0.2) * 0.2; // 0, as it should be
?>
Default installation of OSPanel (OpenServer 5.2.2) with default
configuration of php.ini and etc in it.
Documentation says:
"Returns the floating point remainder of dividing the dividend (x) by
the divisor (y). The remainder (r) is defined as: x = i * y + r, for
some integer i. If y is non-zero, r has the same sign as x and a
magnitude LESS than the magnitude of y."
But r magnitude EQUAL to y.
Solution:
function fmod($x, $y){return echo $x - floor($x/$y) * $y;}
Test script:
---------------
<?php
echo var_dump(fmod(21, 0.2) == 0); // bool(false)
echo var_dump(fmod(1, 0.1) == 0); // bool(false)
echo fmod(1, 0.1); // 0.1
echo fmod(1, 0.2); // 0.2
Expected result:
----------------
<?php
echo var_dump(fmod(21, 0.2) == 0); // bool(true)
echo var_dump(fmod(1, 0.1) == 0); // bool(true)
echo fmod(1, 0.1); // 0
echo fmod(1, 0.2); // 0
Actual result:
--------------
bool(false)
bool(false)
0.1
0.2
--
Edit bug report at https://bugs.php.net/bug.php?id=78073&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=78073&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=78073&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=78073&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=78073&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=78073&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=78073&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=78073&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=78073&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=78073&r=support
Expected behavior: https://bugs.php.net/fix.php?id=78073&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=78073&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=78073&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=78073&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=78073&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=78073&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=78073&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=78073&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=78073&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=78073&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=78073&r=mysqlcfg