Problem when adding "x" to Text displaying a Var

Discussion and feedback on Construct 2

Post » Tue Dec 10, 2013 12:40 pm

Hi. I have encountered a strange problem when adding "0.1" to a global var. The frist time it adds 0.1 to 1 it shows "1.1" as expected. But the second time the value becomes "1.2000000000000002" and then "1.3000000000000003" and so on...

The Number should be shown in the UI as "Multiplier: Yx" where Y is the variable which gets incremented.

After experimenting a bit I found this: When I don't add "& "x"" in the text object it works and adds only 0.1 everytime. But when I add a "x" in the textobject behind the number I get this very long numbers. But why?
Adding an "x" letter to the text object should not change or affect the variable?

Bl4ckSh33p2013-12-10 12:44:05
Bl4ckSh33p-Soft - Custom Software and Indie Games
B
56
S
17
G
6
Posts: 748
Reputation: 9,420

Post » Tue Dec 10, 2013 12:58 pm

It doesn't change the value, it just doesn't display it in the text when the value isn't converted to a string for the text, maybe because some rounding is in place exactly for those cases. Check out the debugger to see that the value of the variable is indeed this bandworm of a number, no matter how it is displayed.

I know there are some very small computational inaccuracies in Construct, but I didn't think it would happen for additions :/ sucks a bit.
Visual Novel 'Engine' in 100 Events
if you ever have to choose between buying Construct 2 on scirra.com or on Steam, read this: Review
B
22
S
9
G
1
Posts: 788
Reputation: 3,788

Post » Tue Dec 10, 2013 1:05 pm

I created an example. When I add "x" as string behind the number the number gets wrong, when I just display the variable (a number) it works fine. It's a bug?

https://dl.dropboxusercontent.com/u/26714455/IncrementExample.capx

But the lower part without the "x" shows the short number 1.2 even if the debugger says the var contains "1.2000000000000002". Bl4ckSh33p2013-12-10 13:07:52
Bl4ckSh33p-Soft - Custom Software and Indie Games
B
56
S
17
G
6
Posts: 748
Reputation: 9,420

Post » Tue Dec 10, 2013 1:11 pm

[QUOTE=mindfaQ] It doesn't change the value, it just doesn't display it in the text when the value isn't converted to a string for the text, maybe because some rounding is in place exactly for those cases. Check out the debugger to see that the value of the variable is indeed this bandworm of a number, no matter how it is displayed.

I know there are some very small computational inaccuracies in Construct, but I didn't think it would happen for additions :/ sucks a bit.[/QUOTE]

@mindfaq
It is not C2's fault, it is how computers work, because a decimal number, when converted to binary, has not always an ended number of decimals (Maybe there are also other problems in top of this), and so, they are rounded in the process, either way, try to keep that in mind when using a softtware that uses floats
Game design is all about decomposing the core of your game so it becomes simple instructions.
B
53
S
22
G
18
Posts: 2,122
Reputation: 17,123

Post » Tue Dec 10, 2013 1:19 pm

The correct answer is probably "1.2000000000000002" - computers have a limited precision so rounding errors are common. However in some circumstances C2 will slightly round values to try and hide these rounding errors. (In retrospect, I'm not sure if that's such a good idea...)

Imagine doing one third times three with 8 digits of precision. That means you round one third to 0.33333333. Times three is 0.99999999. Lo and behold, one third times three is not 1. The same thing happens in binary with numbers like 0.1, which are actually have a recurring representation in binary.
Scirra Founder
B
399
S
236
G
89
Posts: 24,523
Reputation: 195,378

Post » Tue Dec 10, 2013 1:25 pm

@Aphrodite: yes I guess so, I just didn't expect it to happen for +0.1.

@Bl4ckSh33p: I have already explained it. Conversion to string leads to the different display. There is probably a display rule in place that prevents those little differences to show when the number is still handled as float. If you use str(muliplier) in your text, you will see all decimals and thats also what happens when you append the &"x".
Visual Novel 'Engine' in 100 Events
if you ever have to choose between buying Construct 2 on scirra.com or on Steam, read this: Review
B
22
S
9
G
1
Posts: 788
Reputation: 3,788

Post » Tue Dec 10, 2013 1:33 pm

I see, thanks. But is it possible to use the "short" number as string somehow? I tried "floor" and "int" but this only displays whole numbers (1, 2, 3) and no decimals.
Bl4ckSh33p-Soft - Custom Software and Indie Games
B
56
S
17
G
6
Posts: 748
Reputation: 9,420

Post » Tue Dec 10, 2013 2:46 pm

@Bl4ckSh33p,

Here's a potential workaround for you. This will give you an answer to one decimal place (adjust as you require):

string = 0.1 * round(YourNumber * 10)
A big fan of JavaScript.
B
76
S
20
G
74
Posts: 2,245
Reputation: 46,464

Post » Tue Dec 10, 2013 3:03 pm

Thank you very much.
I added your example but it is still not working everytime when I add "& "x"" behind it. 1.1 is shown, 1.2 is not, 1.3 is shown, 1.4 not and so on.
It seems C2 does not like to add a string behind it.

When I don't add the string "x" it seems to work every time.
Bl4ckSh33p-Soft - Custom Software and Indie Games
B
56
S
17
G
6
Posts: 748
Reputation: 9,420

Post » Tue Dec 10, 2013 3:18 pm

That's weird! I have just duplicated this - it doesn't make sense although it seems to be something to do with having the global variable at the start of the display text string. If you make your text string look like this then it'll display correctly (ie have some text to display first, even if it's a nil entry):

"" & Multiplier & "x"
A big fan of JavaScript.
B
76
S
20
G
74
Posts: 2,245
Reputation: 46,464

Next

Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 9 guests