Unclear INT functionality (documentation issue)

Bugs will be moved here once resolved.

Post » Fri Aug 29, 2014 9:22 am

Problem Description
The INT functionality when used to convert a string to a value is unclear and to the unwary user (ie me) can lead to unusual results. This is not a bug but a mismatch in the manual documentation vs the actuality. It's perfectly fine but perhaps unexpected. The INT function operates as follows

int("33") = 33
int("33xx") = 33
init("xx33") = 0

This is similar and probably identical to the javascript implementation of INT, but that implementation is not universal agreed nor is it universally used in C2 where the numeric value of a string containing non-numeric text characters often returns a zero.


Attach a Capx
-unneeded, it's a documentation issue.

Description of Capx


Steps to Reproduce Bug

Observed Result

Expected Result

Affected Browsers

Operating System and Service Pack

Construct 2 Version ID
178
B
7
S
3
G
1
Posts: 184
Reputation: 1,352

Post » Fri Aug 29, 2014 10:08 am

(imo) Just because "33xx" simply works isn't a reason to use such a cast with non numerical numbers... Use regular expressions to extract the number first:
Code: Select all
(\d+)
and then cast it to int
B
32
S
7
G
2
Posts: 317
Reputation: 2,851

Post » Sat Aug 30, 2014 9:34 am

@IndieKiwi

Actually having "33xx" return 33 is a problem, I'd prefer a null or zero response rather than that. However I know why javascript does it that way. So I won't disagree as you're likely right about best practice but as Construct is for non-coders, regex expressions are unlikely the best solution.

My primary comment was that the functionality was not clearly described and there is an inconsistency in C2 about how strings get cast as numbers.

R
B
7
S
3
G
1
Posts: 184
Reputation: 1,352

Post » Sat Aug 30, 2014 1:41 pm

Would this definition be better?
http://en.wikipedia.org/wiki/Integer
Image ImageImage
B
169
S
50
G
174
Posts: 8,328
Reputation: 110,800

Post » Wed Sep 03, 2014 10:17 am

@newt
long time no troll \o/

Otherwise int is the exact translation of the javascript parseInt function
in chrome console:
Code: Select all
>  parseInt("33",10);
<· 33
>  parseInt("33xx",10);
<· 33
>  parseInt("xx33",10);
<· NaN // which C2 doesn't allow and returns 0 instead


But yeah, it should probably be more thorougly documented.
in my opinion, javascript/c2 shouldn't allow for conversion of a non numeric string, java for instance throw an exception on "xx33" and "33xx".
But that might be too hard/expensive to check...
Code: Select all
var cast = parseInt(input,10);
input === ""+cast;
would be too weak a check, since it wouldn't take into acount the case where input = "0033" which would be a valid integer representation in my opinion.
And a regex check like might be too much for just that.
(Also, C2 doesn't have any error handling mecanisms...)
Last edited by Yann on Wed Sep 03, 2014 10:27 am, edited 2 times in total.
B
66
S
22
G
14
Posts: 1,484
Reputation: 16,511

Post » Wed Sep 03, 2014 10:21 am

I would think if anything, that the manual should state to not use int that way (aka with non number strings), since it is outside of what it is meant to do, and so cannot be guaranted to work consistently that way forever.
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 » Thu Sep 04, 2014 4:11 pm

I've updated the documentation. The way it works is consistent with every int conversion function I've seen so far (including in C): it parses digits until it doesn't understand something, then stops. If it doesn't understand the first character, it rejects it as probably not a number string. I think it's useful to do it this way around: parsing "33%" allows you to extract 33 even though there's an extra character on the end, and parsing "abcdef33" fails since it does not look like a number and most likely is not (and is an error).
Scirra Founder
B
398
S
236
G
88
Posts: 24,433
Reputation: 194,635


Return to Closed bugs

Who is online

Users browsing this forum: No registered users and 6 guests