Variable change problem

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.

image

Seems working after adding this block


Thanks

1 Like

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:

The issue you’re describing is a common one in many programming languages, including JavaScript, which is often used in platforms like Thunkable. It stems from how floating-point numbers are represented in computer systems, specifically using the IEEE-754 standard. Here’s a more detailed explanation:

  1. 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).
  2. IEEE-754 Floating-Point Standard: JavaScript uses the IEEE-754 standard for floating-point arithmetic. In this system, numbers are represented using a fixed number of binary digits. This representation can exactly represent numbers like 1/2 (0.5 in decimal) or 1/8 (0.125 in decimal), as they have exact binary equivalents (0.1 and 0.001 in binary, respectively).
  3. 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.
  4. 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).
  5. 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.

In summary, the issue with numbers like 1.0000000004 in Thunkable and other JavaScript environments is due to the inherent limitations of binary floating-point representation, specifically under the IEEE-754 standard. This representation cannot exactly express certain decimal numbers, leading to small but cumulative errors in calculations.

1 Like