I need to loop on a floating point but I don't find the right syntax. What I tried:
(-1.0).upto(1.0).step(0.1) do |t|
...
end
I get the following error:
Error: #NoMethodError: undefined method `upto' for -1.0:Float>
EDIT: Is there a "good" or better way to do this loop?
One option is to use step on a Range
:
(-1.0..1.0).step(0.1) { |i|
puts i
}
Note, while you increment with 0.1 you will run into floating point precision issue. So take that into consideration.
-1.0
-0.9
-0.8
-0.7
-0.6
-0.5
-0.39999999999999997
-0.29999999999999993
-0.19999999999999996
-0.09999999999999995
5.551115123125783e-17
0.10000000000000006
0.20000000000000007
0.30000000000000004
0.4000000000000001
0.5000000000000001
0.6000000000000001
0.7000000000000001
0.8
0.9000000000000001
1.0