Strange Numbers with "&"

Bugs will be moved here once resolved.

Post » Sat Feb 01, 2014 2:34 pm

Link to .capx file (required! If link is blocked remove the http and www parts):
https://copy.com/k1YjZyP2VIO4wjMk

Steps to reproduce:
1. I have text set to "(Variable1/20)*100&"%"".
2. Set Variable1 to 11. This is not required if using the capx, as I have it counting down to show all numbers.

Observed result:
When it gets to 11, it should show 55%. Instead it shows 55.000000001%.
However, this only occurs when I have the &"%" added to the end. Not sure why, but without it, it just shows 55. I can get around this by amending "%" to the end... but it's just strange that it only doesn't work with "&" added.

Expected result:
55%

11/20 = 0.55 * 100 = 55. Not sure where the 000000000.1 comes from.

Tried adding "int" to the beginning, but that doesn't work at all, lol!

Browsers affected:
Chrome: yes
Firefox: yes
Internet Explorer: yes

Operating system & service pack:

Win 7 64bit SP1

Construct 2 version:

r159
    
@bearboxmedia
www.bearboxmedia.com

Nintendo Wii U Developer using Construct 2
B
80
S
12
G
7
Posts: 965
Reputation: 10,750

Post » Sat Feb 01, 2014 5:10 pm

Looks like typical rounding error at floating point math. Rounding the result:
set text to int((Variable1/20)*100)&"%"
seems to help.

Even though I have to admit that I have *no* idea why addition of the &"%" does in any way affect the display format of the float.

All in all.. Floats do give nasty surprises for unprepared every now and then.

Edit: yes.. i think this is a bug. Not the 55.000000...001 but the fact that string concatenation (ampresand &) apparently does some rounding. Joannak2014-02-01 17:12:31
Need help? Upload .capx file with your question.
B
16
S
4
G
2
Posts: 233
Reputation: 2,560

Post » Sat Feb 01, 2014 6:55 pm

@Joannak Strangely enough, when I tried to use int in my main project, I got the results 100%, 0% and -100% only. I just assumed it was doing some major rounding, and took it back out.

Glad someone's able to reproduce it though.
@bearboxmedia
www.bearboxmedia.com

Nintendo Wii U Developer using Construct 2
B
80
S
12
G
7
Posts: 965
Reputation: 10,750

Post » Sat Feb 01, 2014 7:13 pm

If you start a new empty project and set text object to display (11/20)*100&"%" it works just fine and displays 55%. I didn't take a look at your project. I tested in r159. Do you use any plugins in your project?
B
44
S
5
G
5
Posts: 412
Reputation: 5,771

Post » Sat Feb 01, 2014 10:56 pm

If you set text to a value like 55.000000001 then it gets rounded and displays "55".
If you convert the number to a string first (with the str() function or with concatenation) then it will show the number without rounding.
[code]Set variable1 to 11/20 * 100
55.00000000000001

Set text to variable1
"55"

Set text to str(variable1)
"55.00000000000001"

Set text to variable1 & ""
"55.00000000000001"
[/code]

[QUOTE=AnD4D] when I tried to use int in my main project, I got the results 100%, 0% and -100% only.[/QUOTE]
Try
[code]int((Variable1/20)*100) & "%"[/code]

B
55
S
29
G
19
Posts: 1,520
Reputation: 25,650

Post » Sun Feb 02, 2014 4:33 am

Try this for a bit of fun:

capx

I made it for a help request I put out for similar problems I was having with floating point variables. The bottom line, I suspect, is you can't be care-free when dealing with your variables...
A big fan of JavaScript.
B
74
S
20
G
71
Posts: 2,228
Reputation: 44,888

Post » Sun Feb 02, 2014 10:42 am

@nemo I used instance and global variables rather that just (11/20). The instance I use is 11, and the global I use is 20. For some reason, this is what generates the odd number.

@ramones Thanks, that works, although I still don't understand. I would have thought we wouldn't be having such a strange results appearing... especially after looking at @Colludium 's.
@bearboxmedia
www.bearboxmedia.com

Nintendo Wii U Developer using Construct 2
B
80
S
12
G
7
Posts: 965
Reputation: 10,750

Post » Sun Feb 02, 2014 12:27 pm

B
55
S
29
G
19
Posts: 1,520
Reputation: 25,650

Post » Sun Feb 02, 2014 4:53 pm

@ramones is right. This is not a bug; computers calculate floating point numbers with limited precision, and in some cases C2 will apply some rounding to try to hide that, but it changes if you use str() or &.
Scirra Founder
B
397
S
236
G
88
Posts: 24,402
Reputation: 194,474

Post » Sun Feb 02, 2014 7:49 pm

[QUOTE=Ashley] @ramones is right. This is not a bug; computers calculate floating point numbers with limited precision, and in some cases C2 will apply some rounding to try to hide that, but it changes if you use str() or &.[/QUOTE]

Is there a documentation of this automatic rounding? To me (as a long time coder) it seems something that should not be done haphazardly.

Ok, technically it may save some noobs on making mistakes (like comparing float equality to integer), but any deviations of the normal calculations should be well documented.


Need help? Upload .capx file with your question.
B
16
S
4
G
2
Posts: 233
Reputation: 2,560

Next

Return to Closed bugs

Who is online

Users browsing this forum: No registered users and 5 guests