Mathematic error

Bugs will be moved here once resolved.

Post » Sat Oct 08, 2016 11:09 pm

Problem Description
A simple but big issue. I found that when subtracting or adding to a global variable by 0.2, that the sum jumps wildly and then corrects itself a lot. whether dropping by a random 0.00000000005 or when hitting 0 jumping to huge amounts of decimals, and not 0.
I spotted this when transfering the variable to a spritefont, and checked it with the debug to confirm the problem. It also occurs when telling the game to add 0.2 every second, eliminating player involvement.

Attach a Capx
https://www.dropbox.com/s/xy04jybonj1jz ... .capx?dl=0

Description of Capx
The Capx is really simple. You can just use the keyboard commands at the bottom of the events to see the issue in the debug. I hope this is good enough.

Steps to Reproduce Bug
  • Create a global variable - number type
  • Create any method of trigger- keyboard trigger/set to timer
  • Run in debug and watch the variable when triggered over time.

Observed Result
A list of results from the most recent attempt to add by 0.2:
5.4... 5.6000000000000005... 5.800000000000001... 6.000000000000001... to...6.800000000000002... to... 8.799999999999999...9.099999999999998

Expected Result
0.2... 0.4... 0.6... 0.8... 1...

Affected Browsers
  • Chrome: (YES)

Operating System and Service Pack
Windows 10 64 bit

Construct 2 Version ID
beta r237
- Head of Tall Story Studios.

Creator of GOV, Project Abyss, Spiritlands & more!
B
27
S
12
G
1
Posts: 129
Reputation: 3,064

Post » Sat Oct 08, 2016 11:44 pm

If you want accuracy you will have to stay above the tens decimal position.
Image ImageImage
B
169
S
50
G
170
Posts: 8,291
Reputation: 108,726

Post » Sat Oct 08, 2016 11:57 pm

newt wrote:If you want accuracy you will have to stay above the tens decimal position.

It's insane though that a calculation that takes nothing to work out in my head, C2 is getting wrong. If C2 cannot do 0.2 + 0.2 all hope is lost.
- Head of Tall Story Studios.

Creator of GOV, Project Abyss, Spiritlands & more!
B
27
S
12
G
1
Posts: 129
Reputation: 3,064

Post » Sun Oct 09, 2016 12:00 am

Whats 2*0.1?
Image ImageImage
B
169
S
50
G
170
Posts: 8,291
Reputation: 108,726

Post » Sun Oct 09, 2016 12:06 am

newt wrote:Whats 2*0.1?

0.2. Not dismissing the horrendous bug.... but how could using 2*0.2 be used to do 0.6 + 0.2, or 0.8 + 0.2?
- Head of Tall Story Studios.

Creator of GOV, Project Abyss, Spiritlands & more!
B
27
S
12
G
1
Posts: 129
Reputation: 3,064

Post » Sun Oct 09, 2016 12:24 am

No your number *0.1
1*0.1
2*0.1
3*0.1
What Im saying is that if you need to have your value in the tens position multiply a whole number times 0.1
Or another method:
expression-needed-to-round-number_p1015301?#p1015301
Image ImageImage
B
169
S
50
G
170
Posts: 8,291
Reputation: 108,726

Post » Sun Oct 09, 2016 12:36 am

newt wrote:No your number *0.1
1*0.1
2*0.1
3*0.1
What Im saying is that if you need to have your value in the tens position multiply a whole number times 0.1
Or another method:
expression-needed-to-round-number_p1015301?#p1015301


Which number of mine?
Did you download the capx? If one is added to the population the food rate must drop by 0.2 once. If a farmer is added the food rate increases by 0.4. On a click by click basis.
- Head of Tall Story Studios.

Creator of GOV, Project Abyss, Spiritlands & more!
B
27
S
12
G
1
Posts: 129
Reputation: 3,064

Post » Sun Oct 09, 2016 12:50 am

Thank you for the link. That works. Doesnt solve C2's issue but its a work around for now.
- Head of Tall Story Studios.

Creator of GOV, Project Abyss, Spiritlands & more!
B
27
S
12
G
1
Posts: 129
Reputation: 3,064

Post » Sun Oct 09, 2016 2:10 am

It's not a bug, it's a limitation of how floating point numbers are stored on a computer and some rounding.
What happens is 0.2 can't be percisely represented in floating point so numbers are slightly off when doing math. It's much like how 1/3 takes an infinite amount of decimal places in base 10. It happens that 2/10 takes an infinite number of binary bits and floating point only has maybe 50? I forget exactly.

Look at the accuracy problems section here for more of a read.
https://en.m.wikipedia.org/wiki/Floating_point

You probably can find some more explanations by using the forum search on the bugs forum with the keywords floating point.

All other software that uses floating point numbers experience the same result. The difference sometimes is when putting the number into text they often round the result to say six decimal places instead of the 15 or so seen there.
B
92
S
32
G
107
Posts: 5,274
Reputation: 69,959

Post » Sun Oct 09, 2016 2:25 am

R0J0hound wrote:It's not a bug, it's a limitation of how floating point numbers are stored on a computer and some rounding.
What happens is 0.2 can't be percisely represented in floating point so numbers are slightly off when doing math. It's much like how 1/3 takes an infinite amount of decimal places in base 10. It happens that 2/10 takes an infinite number of binary bits and floating point only has maybe 50? I forget exactly.

Look at the accuracy problems section here for more of a read.
https://en.m.wikipedia.org/wiki/Floating_point

You probably can find some more explanations by using the forum search on the bugs forum with the keywords floating point.

All other software that uses floating point numbers experience the same result. The difference sometimes is when putting the number into text they often round the result to say six decimal places instead of the 15 or so seen there.


Thank you for the explanation :) certainly difficult for me to get my head around lol. All i can do then is really round to the right decimal value (0.1) after each calculation is made.
- Head of Tall Story Studios.

Creator of GOV, Project Abyss, Spiritlands & more!
B
27
S
12
G
1
Posts: 129
Reputation: 3,064

Next

Return to Closed bugs

Who is online

Users browsing this forum: No registered users and 0 guests