At first I was confused. I did not know what I was doing wrong. Then I asked some people about what was wrong with my script and they said that that is an issue with for loop float points. The thing is I still need to use for loops. Is there any way to make this work? I mean of course I could do print(1)print(0.9) etc… but definitely not a good way.
I am aware of that but I want to know how people deal with these issues. I tried checking if i was larger than 1, and if it was it returned but sadly it did not work.
Floating point imprecision is a pain. But I honestly think @AMD_chan’s solution is best. Here is what his solution will look like in code (make sure to mark his as the solution, not mine):
for i = 10, 0, -1 do
print(i/10); -- You could also do i*0.1 if you wanted this loop to be really efficient (as multiplication is way cheaper than division)
end
See, it’s not that bad. Only one more calculation (the division) needed (from an efficiency view). But hey, I honestly think another calculation is way better than having those wrong floating points!
You’re treading into microoptimisation territory here. There’s nothing complex about dividing by 10 and decrementing by an integer increment. The operation is only a negligible amount of ms longer that you can reasonably ignore.
For curiosity’s sake, would multiplying by 0.1 be more efficient than dividing by 10? Would it have a good boost or just a negligible change? I’ve always learned multiplication is cheaper but I’m wondering how much cheaper it is in practical situations.
I personally don’t know because I haven’t bothered to check nor do I actually know (I’m unfamiliar with any technical workings of Lua). You can run a test like this in Studio by checking tick before and after the operation.
The difference it takes to complete the operation is negligible to me and hardly makes a difference. The difference is small enough not to notice it. Therefore, I ignore it.
The end result is the same, but multiplication is way “cheaper” (easier for computer to compute) than division. The only exception is division by 2 where a computer can just shift bits down (just like in our division by ten we just shift the numbers down a decimal place). I recommend Googling it if you want to learn more.
I was just wondering if in practical situations it had a difference. Thanks @colbert2677, I’ll test it and I’ll let you know after (if you want).
Well it appears you are right! Just ran a test as @colbert2677 said and it appears multiplying is actually slightly faster.
Well I guess I learned something new today. I guess I will try to use multiplicaiton instead of division when I can, even tho using multiplication instead of division won’t have a huge impact on performance.
With some situations (like dividing by a variable) you can’t avoid it but, starting from now, wherever I divide by a constant I think I’ll use multiplication instead (except when dividing by numbers like 3 where you deal with infinite decimals).
For anyone in the future watching down on us, to find that constant that can replace your division, you do this simple calculation:
1/(number you dividing by)
So I’ll use the 10 example:
1/10 = 0.1 <- This is the multiplication equivalent.
You would do this optimization while writing your code and optimizing it and not on runtime by the way (it would defeat the purpose of it)
Edit: After some further testing and time, I do not recommend doing this. In my testing, division is not always slower than multiplication; sometimes, it even beats it. So in this case, I prefer the readability of division instead of the super small (and sometimes non-existent) speed boost of multiplication.
This only works on the integer number format. In Lua we use doubles. Also, it is only plausible in a compiled language because you must know ahead of time whether to shr or div.