Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String parsing behavior is inconsistent between float() and int() in gdscript #28551

Open
UserAB1236872 opened this issue Apr 30, 2019 · 4 comments · Fixed by #56828
Open

String parsing behavior is inconsistent between float() and int() in gdscript #28551

UserAB1236872 opened this issue Apr 30, 2019 · 4 comments · Fixed by #56828

Comments

@UserAB1236872
Copy link

UserAB1236872 commented Apr 30, 2019

This probably can't be fixed due to a breakage of incompatibility but I've noticed that the functions int and float behave subtly differently in strange ways:

int("456") # yields 456
int("asdfjaldsgja;lsdgj456") # yields 456
int("dfkj4dkfjj5dkjf6dfkjdk") # yields 456
int("4.56") # yields 4
int("4kgjsdjkfhglskd.b56") # yields 4
int("4.5.6") # yields 4
int("1e3") # yields 13

Essentially, int seems to do a left-right parse ignoring any character except [0-9] and ".". After it hits the first "." it will terminate.

Compare this with float

float("456") # yields 456
float("asdfjaldsgja;lsdgj456") # yields 0
float("dfkj4dkfjj5dkjf6dfkjdk") # yields 0
float("4.56") # yields 4.56
float("4kgjsdjkfhglskd.b56") # yields 4
float("4.5.6") # yields 4.5
float("1e3") # yields 1000

The float parser appears to abort the second it sees a non [0-9] or "." character except e. I expect this is due to the handling of exponentiation. It'll also terminate upon hitting a second decimal point, e, or a "." occurring anywhere after an "e".

You probably shouldn't have arbitrary text in the strings you're trying to convert to numbers anyway, but I think this should at least be documented in case someone hits it. To me, the most dangerous one is the difference between int("1e3") and float("1e3") which is a difference that could theoretically occur even in seemingly well-formed parse strings.

@UserAB1236872 UserAB1236872 changed the title String parsing behavior is inconsistent between float() and int() String parsing behavior is inconsistent between float() and int() in gdscript Apr 30, 2019
@Avantir-Chaosfire
Copy link
Contributor

The int functions in general lead to really unexpected results - "dfkj4dkfjj5dkjf6dfkjdk" is not a number, so why does it result in 456? It should fail to convert. Now everywhere I need to convert a string to an int, I'm going to have to remember to check if every character is a numeric character before doing the conversion, instead of just doing the conversion and checking the result. Furthermore, this requires me to implement string parsing logic that should already be in the library - I can't even use string.is_valid_integer, because that checks if the string contains an integer, not is a valid integer, making its name just flat out misleading.

@Avantir-Chaosfire
Copy link
Contributor

I think this issue should be re-opened. Clarifying how this works in the documentation is great, but it doesn't solve the issue. The methods should actually be changed to give sensible and consistent results.

@Calinou Calinou reopened this Feb 2, 2022
@Calinou Calinou modified the milestones: 3.5, 4.0 Feb 2, 2022
@akien-mga
Copy link
Member

I think this might have been fixed in 4.0. Can anyone confirm?

@akien-mga akien-mga modified the milestones: 4.0, 4.x Feb 23, 2023
@vnen
Copy link
Member

vnen commented Feb 24, 2023

The behavior is still the same in 4.0. A bit difficult to change now since it breaks compatibility.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants