Bug #75310 [ReO->Nab]: range(0.94, 0.49, -0.01) difference
| From: | pmmaga@php.net | Date: | Mon, 28 Jan 2019 23:10:31 +0000 |
| Subject: | Bug #75310 [ReO->Nab]: range(0.94, 0.49, -0.01) difference | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-219259@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=75310&edit=1
ID: 75310
Updated by: pmmaga@php.net
Reported by: shawn at mckenzies dot net
Summary: range(0.94, 0.49, -0.01) difference
-Status: Re-Opened
+Status: Not a bug
Type: Bug
Package: Arrays related
Operating System: Win,Linux
PHP Version: 7.0.3
Block user comment: N
Private report: N
New Comment:
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://www.floating-point-gui.de/
Thank you for your interest in PHP.
Although I had provided a possible fix, it would be incomplete (it would still fail for very small
numbers). Given that, I'm closing this.
Previous Comments:
------------------------------------------------------------------------
[2017-10-05 04:30:29] requinix@php.net
Fair enough.
------------------------------------------------------------------------
[2017-10-04 22:20:42] mail at pmmaga dot net
I would actually consider this a bug as it is currently inconsistent as you also showed. (Different
pairs act as expected)
I have linked a PR for a fix.
------------------------------------------------------------------------
[2017-10-04 19:55:07] requinix@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://www.floating-point-gui.de/
Thank you for your interest in PHP.
PHP can't really guarantee any particular behavior; pick different pairs of numbers and
you'll get different results. https://3v4l.org/JtEG9
------------------------------------------------------------------------
[2017-10-04 19:49:16] shawn at mckenzies dot net
Seems to have been introduced in 7.0.3.
------------------------------------------------------------------------
[2017-10-04 19:43:52] shawn at mckenzies dot net
Description:
------------
range(0.94, 0.49, -0.01) returns a 45 element array from .94 to .5 in PHP 7.0.3 - 7.2.0rc2 but
returns the expected 46 element array .94 to .49 in PHP 5.6.0 - 5.6.30.
Test script:
---------------
https://3v4l.org/gZ3cs#v703
$a = range(0.94, 0.49, -0.01);
echo count($a).PHP_EOL;
echo min($a).PHP_EOL;
print_r($a);
Expected result:
----------------
Same array should be created created across PHP versions.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=75310&edit=1