|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2019-05-27 09:51 UTC] e dot grinec at gmail dot com
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 Patchespatch.txt (last revision 2019-05-27 09:58 UTC by e dot grinec at gmail dot com)Pull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 19:00:01 2025 UTC |
function fmod($x, $y) { if (!$y) { return NAN; } $q = $x / $y; $f = ($q < 0 ? 'ceil' : 'floor'); return $x - $f($q) * $y; }