Bug #79338 [Com]: fmod() not calculating properly
| From: | rico dot humme at gmail dot com | Date: | Fri, 23 Jul 2021 13:19:32 +0000 |
| Subject: | Bug #79338 [Com]: fmod() not calculating properly | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-235307@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=79338&edit=1
ID: 79338
Comment by: rico dot humme at gmail dot com
Reported by: ziggi at ziggi dot pl
Summary: fmod() not calculating properly
Status: Not a bug
Type: Bug
Package: *Math Functions
Operating System: Windows
PHP Version: 7.4.3
Block user comment: N
Private report: N
New Comment:
As of today this has not yet been resolved, sadly
for($i = 1; $i <= 10; $i++) {
$dividend = round($i * 3.2, 2);
echo "fmod($dividend, 3.2) > " . fmod($dividend, 3.2)."\n";
}
Output:
fmod(3.2, 3.2) > 0
fmod(6.4, 3.2) > 0
fmod(9.6, 3.2) > 3.2
fmod(12.8, 3.2) > 0
fmod(16, 3.2) > 3.2
fmod(19.2, 3.2) > 3.2
fmod(22.4, 3.2) > 3.2
fmod(25.6, 3.2) > 0
fmod(28.8, 3.2) > 3.2
fmod(32, 3.2) > 3.2
Previous Comments:
------------------------------------------------------------------------
[2020-03-03 17:43:23] derick@php.net
PERL:
$ perl -e 'use POSIX (); print POSIX::fmod(75.6, 25.2);'
25.2
------------------------------------------------------------------------
[2020-03-03 17:41:01] ziggi at ziggi dot pl
OK,
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:34:34] nikic@php.net
> import math
> print(math.fmod(75.6, 25.2))
25.199999999999996
Surprising absolute nobody, Python returns the same result.
Obviously.
Get off my lawn.
------------------------------------------------------------------------
[2020-03-03 17:09:45] ziggi at ziggi dot pl
Well,
My conclusion:
PHP:
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 11:38:55] cmb@php.net
Also note:
<?php
$num = 75.6;
$denom = 25.2;
$res = fmod($num, $denom);
var_dump($res === $denom); // bool(false)
?>
Further reading: <http://www.floating-point-gui.de/>
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=79338
--
Edit this bug report at https://bugs.php.net/bug.php?id=79338&edit=1