When I run the following commands,
(0..20).step(0.1) do |n|
puts n
end
I get the following return:
0.0
0.1
0.2
0.30000000000000004
0.4
0.5
0.6000000000000001
0.7000000000000001
0.8
0.9
1.0
1.1
1.2000000000000002
1.3
1.4000000000000001
1.5
1.6
1.7000000000000002
...
What is the best way to avoid this roundoff error?
Update: My question about why this occurs has been previously answered here in another question, Is floating point math broken?, but I did not immediately find that.