php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #50679 range fails on some float values
Submitted: 2010-01-06 16:07 UTC Modified: 2010-01-06 20:31 UTC
From: kauffj at gmail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.3.1 OS: Ubuntu 9.10
Private report: No CVE-ID: None
 [2010-01-06 16:07 UTC] kauffj at gmail dot com
Description:
------------
The range function works on some float values but not others.

Reproduce code:
---------------
WORKS: print_r(range(0.3, 0.4, 0.1));
FAILS: print_r(range(5.5, 5.6, 0.1));
WORKS: print_r(range(5.5, 5.7, 0.1));


Expected result:
----------------
Array
(
    [0] => 0.3
    [1] => 0.4
)
Array
(
    [0] => 5.5
    [1] => 5.6
)
Array
(
    [0] => 5.5
    [1] => 5.6
    [2] => 5.7
)

Actual result:
--------------
Array
(
    [0] => 0.3
    [1] => 0.4
)
PHP Warning:  range(): step exceeds the specified range
Array
(
    [0] => 5.5
    [1] => 5.6
    [2] => 5.7
)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-01-06 20:31 UTC] jani@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://docs.sun.com/source/806-3568/ncg_goldberg.html
 
Thank you for your interest in PHP.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 18 14:01:35 2024 UTC