You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As I noted in #3991, Julia's ranges include the end value, whereas other languages do not. At first I thought we could get away with a quick fix of subtracting 1 from the given end value, but then I remembered that negative step values exist, and if step is negative we need to add 1 instead.
Furthermore, I looked at other generated code with negative step values (test cases for this do not currently exist; we will need to add some to main) in other langauges and realized that three other languages does not handle negative step values correctly either. Basically it's a similar issue to what we had in #3809, where our current implementations do not account for negative step.
What needs to be done:
Julia: if step is positive, subtract 1 from end; if step is negative, add 1 to end.
Java, C#, C++: change the inequality checking i so it's < when step is positive and > when step is negative.
Python is fine, and Swift appears to be fine as well. I don't have Swift installed on my current machine, but I can install it to double-check at some point.
As with listSlice, we can make it so that if the value of step is known at generation-time, we generate more concise code. Otherwise we need to generate code to do that logic at runtime.
The text was updated successfully, but these errors were encountered:
It feels to me that we need to look more deeply into what forRange means (and ought to mean) to make the rendering in each language more transparent.
I definitely agree with the last paragraph: we need to be clearer about what information is known when (this is something that @Xinlu-Y was working on, but it got very complicated rather quickly). We need to 'teach' our representations more about staging.
I made a PR to fix Julia's forRange for positive step values. Beyond that, there seems to be something of a rabbit-hole, so it might be best to leave it for now.
As I noted in #3991, Julia's ranges include the
end
value, whereas other languages do not. At first I thought we could get away with a quick fix of subtracting1
from the givenend
value, but then I remembered that negativestep
values exist, and ifstep
is negative we need to add1
instead.Furthermore, I looked at other generated code with negative step values (test cases for this do not currently exist; we will need to add some to
main
) in other langauges and realized that three other languages does not handle negative step values correctly either. Basically it's a similar issue to what we had in #3809, where our current implementations do not account for negativestep
.What needs to be done:
step
is positive, subtract1
fromend
; ifstep
is negative, add1
toend
.i
so it's<
whenstep
is positive and>
whenstep
is negative.As with
listSlice
, we can make it so that if the value ofstep
is known at generation-time, we generate more concise code. Otherwise we need to generate code to do that logic at runtime.The text was updated successfully, but these errors were encountered: