# range() [ext/standard/tests/array/range.phpt]

- Thread: https://externals.io/message/579
- Original archive: https://news-web.php.net/php.internals/579
- Started by Sara Golemon on 2003-04-02T02:14:20+00:00
- Messages: 1
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #579 — Sara Golemon — 2003-04-02T02:14:20+00:00

URL: https://externals.io/message/579#579

This test is failing because of the good old-well known floating point
drift issue.

4.9 + 0.1 > 5.0

The kludge attached below "solves" it, but... well... it's a kludge.  It
basicly adds (or subtracts) the smallest measurable piece of a double
during the comparison step (so as to not alter the actual value) and give
the step some room to breathe.

Isn't there a better way to do it?

Index: ext/standard/array.c
===================================================================
RCS file: /repository/php4/ext/standard/array.c,v
retrieving revision 1.224
diff -u -r1.224 array.c
--- ext/standard/array.c        1 Apr 2003 21:47:21 -0000       1.224
+++ ext/standard/array.c        2 Apr 2003 01:17:29 -0000
@@ -81,6 +81,8 @@
 #define INTERSECT_NORMAL               0
 #define INTERSECT_ASSOC                        1

+#define DOUBLE_DRIFT_FIX       0.000000000000001
+
 PHP_MINIT_FUNCTION(array)
 {
 #ifdef ZTS
@@ -1543,7 +1545,7 @@
                                err = 1;
                                goto err;
                        }
-                       for (; low >= high; low -= step) {
+                       for (; low >= (high - DOUBLE_DRIFT_FIX); low -=
step) {
                                add_next_index_double(return_value, low);
                        }
                } else if (high > low) {        /* Positive steps */
@@ -1551,7 +1553,7 @@
                                err = 1;
                                goto err;
                        }
-                       for (; low <= high; low += step) {
+                       for (; low <= (high + DOUBLE_DRIFT_FIX); low +=
step) {
                                add_next_index_double(return_value, low);
                        }
                } else {
