php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #77674 Range function doesn't include
Submitted: 2019-02-27 10:28 UTC Modified: 2019-02-27 15:28 UTC
From: lifekent at gmail dot com Assigned: cmb (profile)
Status: Duplicate Package: Documentation problem
PHP Version: 7.2.15 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: lifekent at gmail dot com
New email:
PHP Version: OS:

 

 [2019-02-27 10:28 UTC] lifekent at gmail dot com
Description:
------------
---
From manual page: https://php.net/function.range
---
If this is an intended behavior than it must be in the http://php.net/manual/en/doc.changelog.php

Range function description:

The sequence is ended upon reaching the end value.

This works in PHP 7.0.2 but works different starting from the PHP 7.0.3 and doesn't include the end value.

Test script:
---------------
$min = 0.2;
$max = 5;

print_r(range($min, $max, 0.2));

Expected result:
----------------
(
    [0] => 0.2
    [1] => 0.4
    [2] => 0.6
    [3] => 0.8
    [4] => 1
    [5] => 1.2
    [6] => 1.4
    [7] => 1.6
    [8] => 1.8
    [9] => 2
    [10] => 2.2
    [11] => 2.4
    [12] => 2.6
    [13] => 2.8
    [14] => 3
    [15] => 3.2
    [16] => 3.4
    [17] => 3.6
    [18] => 3.8
    [19] => 4
    [20] => 4.2
    [21] => 4.4
    [22] => 4.6
    [23] => 4.8
    [24] => 5
)


Actual result:
--------------
(
    [0] => 0.2
    [1] => 0.4
    [2] => 0.6
    [3] => 0.8
    [4] => 1
    [5] => 1.2
    [6] => 1.4
    [7] => 1.6
    [8] => 1.8
    [9] => 2
    [10] => 2.2
    [11] => 2.4
    [12] => 2.6
    [13] => 2.8
    [14] => 3
    [15] => 3.2
    [16] => 3.4
    [17] => 3.6
    [18] => 3.8
    [19] => 4
    [20] => 4.2
    [21] => 4.4
    [22] => 4.6
    [23] => 4.8
)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-02-27 11:31 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2019-02-27 11:31 UTC] cmb@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.

See also <https://github.com/php/php-src/pull/2804>.
 [2019-02-27 15:09 UTC] danack@php.net
To add a bit more context, because this bug confused me also, and I can only remember things after writing them down.


0.2 isn't exactly representable in binary. Using 64 precision the two closest numbers are:

0x3FC9999999999999 in binary = 0.199999999999999983346654630623
0x3FC999999999999A in binary = 0.200000000000000011102230246252

When you enter 0.2 PHP actually uses the closest possible representation which is 0.200000000000000011102230246252 . Adding 0.200000000000000011102230246252 to itself over and over doesn't result in exactly 5.

Although your code example appeared to work on some versions of PHP, that was only working by coincidence, and there's no realistic way to make floating point math work 'reliably' when your expectations are different from how the maths is implemented by CPUs.

Range will only be safe to use with numbers that are exactly representable in the CPU, i.e. either integers or exactly representable floats like 2.5. But I'd recommend not using floats as having code break when changing a number from 0.25 to 0.2, would be 'a little surprising'.
 [2019-02-27 15:28 UTC] salathe@php.net
-Status: Not a bug +Status: Duplicate
 [2019-02-27 15:28 UTC] salathe@php.net
Also, a duplicate of #75310.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 19:01:30 2024 UTC