1 | for i = 0 , 1 , 0.01 do print (i) end |
IT only prints up to .99 and idk why, I put this code in the command bar and it prints to .99 aswell
The issue is caused by floating point precision error. Due to the 100 addition operations you're doing, the binary value loses precision as it has a finite length and the bit-representation gets too long.
You can see the 0.99 you're getting is not exactly 0.99 with this piece of code:
1 | for i = 0 , 1 , 0.01 do |
2 | print (i = = 0.99 ) |
3 | end |
It is generally considered a bad practice to use decimals in for loops due to this very issue. A way better idea is to simply use integers and divide them back into the decimal form if necessary:
1 | for i = 0 , 100 do |
2 | i = i / 100 |
3 | print (i) |
4 | end |
It might be a simple fix. Either:
1 | for i = 0 , 1 , 0.01 do |
2 | print (i + 1 ) |
3 | end |
1 | for i = 0 , 1.01 , 0.01 do |
2 | print (i) |
3 | end |
Locked by User#19524
This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.
Why was this question closed?