php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #54459 Range function accuracy
Submitted: 2011-04-03 20:51 UTC Modified: 2011-04-06 12:23 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: mail at anthonysterling dot com Assigned: aharvey (profile)
Status: Closed Package: Unknown/Other Function
PHP Version: Irrelevant OS: Mixed
Private report: No CVE-ID: None
 [2011-04-03 20:51 UTC] mail at anthonysterling dot com
Description:
------------
The range function appears to lose accuracy when using a float (as permitted in 
the documentation) as the 3rd (increment) parameter.

Test script:
---------------
<?php
foreach(range(90, 100, .1) as $i => $v){
  echo $i, ' = ', $v, PHP_EOL;
}

Expected result:
----------------
  0 = 90
  1 = 90.1
  2 = 90.2
  3 = 90.3
  4 = 90.4
  5 = 90.5
  6 = 90.6
  7 = 90.7
  8 = 90.8
  9 = 90.9
  10 = 91
  11 = 91.1
  12 = 91.2
  13 = 91.3
  14 = 91.4
  15 = 91.5
  16 = 91.6
  17 = 91.7
  18 = 91.8
  19 = 91.9
  20 = 92
  21 = 92.1
  22 = 92.2
  23 = 92.3
  24 = 92.4
  25 = 92.5
  26 = 92.6
  27 = 92.7
  28 = 92.8
  29 = 92.9
  30 = 93
  31 = 93.1
  32 = 93.2
  33 = 93.3
  34 = 93.4
  35 = 93.5
  36 = 93.6
  37 = 93.7
  38 = 93.8
  39 = 93.9
  40 = 94
  41 = 94.1
  42 = 94.2
  43 = 94.3
  44 = 94.4
  45 = 94.5
  46 = 94.6
  47 = 94.7
  48 = 94.8
  49 = 94.9
  50 = 95
  51 = 95.1
  52 = 95.2
  53 = 95.3
  54 = 95.4
  55 = 95.5
  56 = 95.6
  57 = 95.7
  58 = 95.8
  59 = 95.9
  60 = 96
  61 = 96.1
  62 = 96.2
  63 = 96.3
  64 = 96.4
  65 = 96.5
  66 = 96.6
  67 = 96.7
  68 = 96.8
  69 = 96.9
  70 = 97
  71 = 97.1
  72 = 97.2
  73 = 97.3
  74 = 97.4
  75 = 97.5
  76 = 97.6
  77 = 97.7
  78 = 97.8
  79 = 97.9
  80 = 98
  81 = 98.1
  82 = 98.2
  83 = 98.3
  84 = 98.4
  85 = 98.5
  86 = 98.6
  87 = 98.7
  88 = 98.8
  89 = 98.9
  90 = 99
  91 = 99.1
  92 = 99.2
  93 = 99.3
  94 = 99.4
  95 = 99.5
  96 = 99.6
  97 = 99.7
  98 = 99.8
  99 = 99.9
  100 = 100

Actual result:
--------------
  0 = 90
  1 = 90.1
  2 = 90.2
  3 = 90.3
  4 = 90.4
  5 = 90.5
  6 = 90.6
  7 = 90.7
  8 = 90.8
  9 = 90.9
  10 = 91
  11 = 91.1
  12 = 91.2
  13 = 91.3
  14 = 91.4
  15 = 91.5
  16 = 91.6
  17 = 91.7
  18 = 91.8
  19 = 91.9
  20 = 92
  21 = 92.1
  22 = 92.2
  23 = 92.3
  24 = 92.4
  25 = 92.5
  26 = 92.6
  27 = 92.7
  28 = 92.8
  29 = 92.9
  30 = 93
  31 = 93.1
  32 = 93.2
  33 = 93.3
  34 = 93.4
  35 = 93.5
  36 = 93.6
  37 = 93.7
  38 = 93.8
  39 = 93.9
  40 = 94
  41 = 94.1
  42 = 94.2
  43 = 94.3
  44 = 94.4
  45 = 94.5
  46 = 94.6
  47 = 94.7
  48 = 94.8
  49 = 94.9
  50 = 95
  51 = 95.1
  52 = 95.2
  53 = 95.3
  54 = 95.4
  55 = 95.5
  56 = 95.6
  57 = 95.7
  58 = 95.8
  59 = 95.9
  60 = 96
  61 = 96.1
  62 = 96.2
  63 = 96.3
  64 = 96.4
  65 = 96.5
  66 = 96.6
  67 = 96.7
  68 = 96.8
  69 = 96.9
  70 = 97
  71 = 97.1
  72 = 97.2
  73 = 97.3
  74 = 97.4
  75 = 97.5
  76 = 97.6
  77 = 97.7
  78 = 97.8
  79 = 97.9
  80 = 98
  81 = 98.1
  82 = 98.2
  83 = 98.3
  84 = 98.4
  85 = 98.5
  86 = 98.6
  87 = 98.7
  88 = 98.799999999999
  89 = 98.899999999999
  90 = 98.999999999999
  91 = 99.099999999999
  92 = 99.199999999999
  93 = 99.299999999999
  94 = 99.399999999999
  95 = 99.499999999999
  96 = 99.599999999999
  97 = 99.699999999999
  98 = 99.799999999999
  99 = 99.899999999999
  100 = 99.999999999999

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-04-03 21:29 UTC] mail at anthonysterling dot com
Reducing the precision to a value equal to, or lower than 13 appears to address 
it.
 [2011-04-04 00:13 UTC] cataphract@php.net
-Status: Open +Status: Bogus
 [2011-04-04 00:13 UTC] cataphract@php.net
.1 is not exactly representable; the rounding error is propagated through the multiplication until it gets big enough to affect the displayed number for the desired precision.

Bogus.
 [2011-04-04 09:15 UTC] mail at anthonysterling dot com
Thanks.

I understand *why* this happens, I'm just don't think this is expected nor 
desirable.

Should this not be compensated/adjusted for before reaching user-land as the 
intent is pretty clear (increment in 10% steps).

Thanks again.

Anthony.
 [2011-04-04 11:04 UTC] cataphract@php.net
-Status: Bogus +Status: Open -Type: Bug +Type: Feature/Change Request
 [2011-04-04 11:04 UTC] cataphract@php.net
I'm changing it to a request then.

But it's clear the function currently works as documented. What you want is some sort of correction to the cumulating errors. I suppose this is possible, but do you have any algorithm to suggest here?..
 [2011-04-06 12:23 UTC] aharvey@php.net
Automatic comment from SVN on behalf of aharvey
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=309986
Log: Implement FR #54459 (Range function accuracy) by changing the way range()
calculates values when used with floating point bounds/step.
 [2011-04-06 12:23 UTC] aharvey@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: aharvey
 [2011-04-06 12:23 UTC] aharvey@php.net
This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Dec 03 17:01:29 2024 UTC