Bug #81697 [NEW]: Since 8.0, php is not multiplying float values correctly
| From: | neil+php at neilmasters dot co dot uk | Date: | Fri, 03 Dec 2021 17:30:20 +0000 |
| Subject: | Bug #81697 [NEW]: Since 8.0, php is not multiplying float values correctly | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-238150@lists.php.net to get a copy of this message | ||
From: neil+php at neilmasters dot co dot uk
Operating system: linux, mac
PHP version: 8.0.13
Package: Math related
Bug Type: Bug
Bug description:Since 8.0, php is not multiplying float values correctly
Description:
------------
Hi folks,
I tried a searching prior to submitting because I can not see myself
being the only person to have come across this, apologies if it is a
duplicate. I also have to admit to not really knowing what it is I need
to look for.
I am seeing odd results when multiplying specific values since 8.0 on
linux and mac - I can not test on windows currently.
2114.20 * 100: 211420 right? no. According to PHP since 8.0 it is
211419.99999999997
1.15 * 100: 115 yes? no. It results in 114.99999999999999
1.08 * 100: 108? Yes actually :)
This does not seem to be an issue with ints, just floats.
https://3v4l.org/rnTis is a quick 3v4l with a few examples and
I have
attached a test script which you can use to highlight the issue across
any number range you want.
Test script:
---------------
<?php
/**
* Test script to demo the issue of multiplication of specific numbers
* results in the wrong value.
*
* Using start of 1 and stop of 2 multiply every 2dp value(i.e. 1.01 -
1.99) by
* 100 and var dump the value.
*
* Example results:
* 1.06: 106
* 1.07: 107
* 1.08: 108
* 1.09: 109.00000000000001
* 1.10: 110.00000000000001
* 1.11: 111.00000000000001
* 1.12: 112.00000000000001
* 1.13: 112.99999999999999
* 1.14: 113.99999999999999
* 1.15: 114.99999999999999
* 1.16: 115.99999999999999
* 1.17: 117
* 1.18: 118
* ...
*
* Now here is the barking mad weirdness... bump up that start and stop
by 1 and you get
* totally different results, showing a lack of consistency in its
wrongness.
*
* 2.00: 200
* 2.01: 200.99999999999997
* 2.02: 202
* 2.03: 202.99999999999997
* ...
*/
$start = 1;
$stop = 2;
for ($i = $start; $i < $stop; $i++) {
for ($j = 0; $j < 100; $j++) {
$number = (float)sprintf(
'%s.%s',
$i,
$j < 10
? sprintf('0%s', $j)
: $j
);
var_dump($number * 100);
echo PHP_EOL;
}
}
Expected result:
----------------
float(100)
float(101)
float(102)
float(103)
float(104)
float(105)
float(106)
float(107)
float(108)
float(109)
float(110)
float(111)
float(112)
float(113)
float(114)
float(115)
float(116)
float(117)
float(118)
float(119)
float(120)
float(121)
float(122)
float(123)
float(124)
float(125)
float(126)
float(127)
float(128)
float(129)
float(130)
float(131)
float(132)
float(133)
float(134)
float(135)
float(136)
float(137)
float(138)
float(139)
float(140)
float(141)
float(142)
float(143)
float(144)
float(145)
float(146)
float(147)
float(148)
float(149)
float(150)
float(151)
float(152)
float(153)
float(154)
float(155)
float(156)
float(157)
float(158)
float(159)
float(160)
float(161)
float(162)
float(163)
float(164)
float(165)
float(166)
float(167)
float(168)
float(169)
float(170)
float(171)
float(172)
float(173)
float(174)
float(175)
float(176)
float(177)
float(178)
float(179)
float(180)
float(181)
float(182)
float(183)
float(184)
float(185)
float(186)
float(187)
float(188)
float(189)
float(190)
float(191)
float(192)
float(193)
float(194)
float(195)
float(196)
float(197)
float(198)
float(199)
Actual result:
--------------
float(100)
float(101)
float(102)
float(103)
float(104)
float(105)
float(106)
float(107)
float(108)
float(109.00000000000001)
float(110.00000000000001)
float(111.00000000000001)
float(112.00000000000001)
float(112.99999999999999)
float(113.99999999999999)
float(114.99999999999999)
float(115.99999999999999)
float(117)
float(118)
float(119)
float(120)
float(121)
float(122)
float(123)
float(124)
float(125)
float(126)
float(127)
float(128)
float(129)
float(130)
float(131)
float(132)
float(133)
float(134)
float(135)
float(136)
float(137)
float(138)
float(139)
float(140)
float(141)
float(142)
float(143)
float(144)
float(145)
float(146)
float(147)
float(148)
float(149)
float(150)
float(151)
float(152)
float(153)
float(154)
float(155)
float(156)
float(157)
float(158)
float(159)
float(160)
float(161)
float(162)
float(163)
float(164)
float(165)
float(166)
float(167)
float(168)
float(169)
float(170)
float(171)
float(172)
float(173)
float(174)
float(175)
float(176)
float(177)
float(178)
float(179)
float(180)
float(181)
float(182)
float(183)
float(184)
float(185)
float(186)
float(187)
float(188)
float(189)
float(190)
float(191)
float(192)
float(193)
float(194)
float(195)
float(196)
float(197)
float(198)
float(199)
--
Edit bug report at https://bugs.php.net/bug.php?id=81697&edit=1
--
Fix committed: https://bugs.php.net/fix.php?id=81697&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=81697&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=81697&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=81697&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=81697&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=81697&r=support
Expected behavior: https://bugs.php.net/fix.php?id=81697&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=81697&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=81697&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=81697&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=81697&r=phptooold
Daylight Savings: https://bugs.php.net/fix.php?id=81697&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=81697&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=81697&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=81697&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=81697&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=81697&r=mysqlcfg