This is my first post here and English is not my native language so please excuse some (hopefully minor) mistakes.
I ran across a problem when trying to iterate over some cubic space. I want to give a function two corners of a space and a resolution for each axis. Then I want to iterate over each axis in nested loops to do something at every point in that space. My code looks something like this:
corner1 = [0.0, 1.0, 5.0]
corner2 = [1.0, 2.0, 5.0]
res = [100, 100, 1]
steps = abs.((corner1 .- corner2))./res
for x=corner1[1]:steps[1]:corner2[1]
for y=corner1[2]:steps[2]:corner2[2]
for z=corner1[3]:steps[3]:corner2[3]
# do something with x, y and z
end
end
end
But, if you try to run that, julia will give me an error that I cannot create a range with 0 step. Under normal circumstances this would be reasonable. Here however the length of the range is zero anyway.
I came up with a very quick and hacky fix. The fucntion (:)(start::T, step::T, stop::T) where T<:Union{Float16,Float32,Float64}
in twiceprecision.jl raised the error.
My function now looks like:
function (:)(start::T, step::T, stop::T) where T<:Union{Float16,Float32,Float64}
step == 0 && start - stop != 0 && throw(ArgumentError("range step cannot be zero for non-zero length range"))
if step == 0
return 0.0:0.0
end
# rest of the function
end
My problem is solved by that. But I don’t think a proper fix is that easy.
Perhaps my approach on this problem was totally wrong and I have to tackle it different.
In any case I am happy about feedback and further advice.