I’m here making a game: random order of 1 - 4 show up in buttons, after a few seconds it disappears and players have to click them in right order. If it’s correct, the speed of disappearing become faster and points are added; oppositely, if it’s incorrect, scores will be deducted.
Now the problem is, after a few round, the speed variable just gone crazy. I suppose it went -0.1s for each win, but came out having the variable with lots of decimal places. What did I done wrong
Link to project: Thunkable
Screens of the error:
Thx for helping
Try adding a logic to roundoff for each of the points/scores, anytime during the logic if it detects decimals it will keeps it consistent.
Seems working after adding this block
But is there any reason for the variable showing that much of decimal places when it shouldn’t?
This has been discussed many times on the forums. I can’t find the best explanation but here’s one I did find: Round with x decimals behaviour.
Edit: And this response from ChatGPT:
- Binary Representation: Computers use binary (base-2) number systems, whereas humans typically use decimal (base-10). In binary, certain numbers that are simple in decimal cannot be represented precisely. For instance, in decimal, 1/10 is simply 0.1. However, in binary, 1/10 cannot be represented exactly, just as 1/3 cannot be precisely represented in decimal (it’s 0.3333… with an infinite number of 3s).
- Inexact Representation of Certain Decimals: The issue arises with decimals like 0.1 or 0.01. These numbers do not have an exact binary representation. The computer represents them with the closest possible binary number it can, but this representation is slightly off from the actual decimal value.
- Accumulation of Errors: When performing arithmetic operations on these inexact representations, the small errors can accumulate. For instance, if you add 0.1 ten times in a computer system, you might expect the result to be exactly 1.0. However, because each 0.1 is slightly off, the total might be slightly more or less than 1 (like 1.0000000004 or 0.9999999999).
- Implications in Programming: This characteristic of floating-point arithmetic can lead to unexpected results in programming. It’s particularly important in situations requiring high precision, like financial calculations. Programmers often have to use specific strategies to avoid or mitigate these errors, such as using integer-based arithmetic for certain calculations or applying rounding techniques.