Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for infinite Ranges #4127

Merged
merged 7 commits into from
Jun 11, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .release-notes/4127.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## Fix for infinite Ranges

Even though all Range objects that contain NaN parameters are considered infinite, some did not iterate at all or produced an error after the first iteration. For example, the code from the Range documentation:
stefandd marked this conversation as resolved.
Show resolved Hide resolved

```pony
// this Range will produce 0 at first, then infinitely NaN
let nan: F64 = F64(0) / F64(0)
for what_am_i in Range[F64](0, 1000, nan) do
wild_guess(what_am_i)
end
```

did not run as expected, but rather produced an error on the first iteration. This is now fixed, and .next() calls on the above `Range[F64](0, 1000, nan)` now first yields 0 and subsequently indefinetely NaN values.
15 changes: 12 additions & 3 deletions packages/collections/_test.pony
Original file line number Diff line number Diff line change
Expand Up @@ -652,8 +652,6 @@ class \nodoc\ iso _TestRange is UnitTest
_assert_infinite[F64](h, 0, 10, p_inf)
_assert_infinite[F64](h, 100, 10, n_inf)



fun _assert_infinite[N: (Real[N] val & Number)](
h: TestHelper,
min: N,
Expand All @@ -663,7 +661,18 @@ class \nodoc\ iso _TestRange is UnitTest
let range: Range[N] = Range[N](min, max, step)
let range_str = "Range(" + min.string() + ", " + max.string() + ", " + step.string() + ")"

h.assert_true(range.is_infinite(), range_str + " should be infinite")
h.assert_true(range.is_infinite(), range_str + " should be infinite")
try
ergl marked this conversation as resolved.
Show resolved Hide resolved
let nextval = range.next()?
h.assert_eq[N](nextval, min)
else
h.fail(range_str + ".next(): first call should not fail but should produce " + min.string())
end
try
range.next()?
else
h.fail(range_str + ".next(): subsequent call should not fail")
end

fun _assert_range[N: (Real[N] val & Number)](
h: TestHelper,
Expand Down
3 changes: 3 additions & 0 deletions packages/collections/range.pony
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ class Range[A: (Real[A] val & Number) = USize] is Iterator[A]
or ((_min > _max) and (_inc > 0))

fun has_next(): Bool =>
if _infinite then
return true
end
if _forward then
_idx < _max
else
Expand Down