php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75310 range(0.94, 0.49, -0.01) difference
Submitted: 2017-10-04 19:43 UTC Modified: 2019-01-28 23:10 UTC
Votes:2
Avg. Score:3.5 ± 0.5
Reproduced:0 of 0 (0.0%)
From: shawn at mckenzies dot net Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 7.0.3 OS: Win,Linux
Private report: No CVE-ID: None
 [2017-10-04 19:43 UTC] 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.


Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-10-04 19:49 UTC] shawn at mckenzies dot net
-Summary: range(0.94, 0.49, -0.01) difference +Summary: range(0.94, 0.49, -0.01) difference -Operating System: Win7,Linux +Operating System: Win,Linux -PHP Version: 7.2.0RC3 +PHP Version: 7.0.3
 [2017-10-04 19:49 UTC] shawn at mckenzies dot net
Seems to have been introduced in 7.0.3.
 [2017-10-04 19:55 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2017-10-04 19:55 UTC] 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 22:20 UTC] 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-05 04:30 UTC] requinix@php.net
-Status: Not a bug +Status: Re-Opened
 [2017-10-05 04:30 UTC] requinix@php.net
Fair enough.
 [2019-01-28 23:10 UTC] pmmaga@php.net
-Status: Re-Opened +Status: Not a bug
 [2019-01-28 23:10 UTC] pmmaga@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.

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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 16:01:29 2024 UTC