Doc #77674 [Nab->Dup]: Range function doesn't include

From: Date: Wed, 27 Feb 2019 15:28:46 +0000
Subject: Doc #77674 [Nab->Dup]: Range function doesn't include
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-16435@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=77674&edit=1 ID: 77674 Updated by: salathe@php.net Reported by: lifekent at gmail dot com Summary: Range function doesn't include -Status: Not a bug +Status: Duplicate Type: Documentation Problem Package: Documentation problem Operating System: Linux PHP Version: 7.2.15 Assigned To: cmb Block user comment: N Private report: N New Comment: Also, a duplicate of #75310. Previous Comments: ------------------------------------------------------------------------ [2019-02-27 15:09:24] 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 11:31:47] 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 10:28:23] 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 ) ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=77674&edit=1

« previous php.doc.bugs (#16435) next »