Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   float() (https://forums.graalonline.com/forums/showthread.php?t=134260228)

xXziroXx 08-17-2010 03:10 AM

float()
 
Can anyone tell me if this is supposed to happen and if so, why?

PHP Code:

float(123) = 123
float
("123") = 123
float
("str") = -1
float
("str123") = -1
float
("123str") = 123 

Note the very last one. If a string starts with numbers, the float() command thinks it's a number, and I find this very annoying and misleading. If it is indeed a bug with the engine, could it be fixed please?

WhiteDragon 08-17-2010 03:29 AM

This is known behavior, and I think intentional (would need to contact Stefan for reasoning). It shouldn't be hard to make your own float function which calls the internal float only in the case where all the characters of the string are integers (or decimal points). Not sure about scientific notation and how it handles that.

fowlplay4 08-17-2010 03:45 AM

Well this is just silly.

PHP Code:

echo("123str" "123str"); // echos 246 


xXziroXx 08-17-2010 03:47 AM

Quote:

Originally Posted by WhiteDragon (Post 1594759)
This is known behavior, and I think intentional (would need to contact Stefan for reasoning). It shouldn't be hard to make your own float function which calls the internal float only in the case where all the characters of the string are integers (or decimal points). Not sure about scientific notation and how it handles that.

I'd rather have the default float() function work like the way it, at least in my opinion, is supposed to work.

WhiteDragon 08-17-2010 03:48 AM

Quote:

Originally Posted by xXziroXx (Post 1594762)
I'd rather have the default float() function work like the way it, at least in my opinion, is supposed to work.

It'd involve reworking literally all the string->number casting in GS which could introduce bugs. (Not saying that I disagree with your motive, it's just unrealistic.)

xXziroXx 08-17-2010 04:31 AM

Quote:

Originally Posted by WhiteDragon (Post 1594764)
which could introduce bugs.

More like fix current bugs and have it work like supposed too after that. Maybe Stefan could even write something up that'll only affect how float() handles things.

fowlplay4 08-17-2010 04:34 AM

int("123str") should really return -1 too

-Ramirez- 08-17-2010 04:36 AM

Quote:

Originally Posted by WhiteDragon (Post 1594764)
It'd involve reworking literally all the string->number casting

This is data conversion, not casting.

xXziroXx 08-17-2010 04:41 AM

Quote:

Originally Posted by fowlplay4 (Post 1594778)
int("123str") should really return -1 too

Definitely. Just because a string STARTS with numbers doesn't mean it's a number.

Admins 08-17-2010 05:21 PM

This is the same behaviour as the various C functions for converting a string to integer or floating point value.

xXziroXx 08-17-2010 05:30 PM

Quote:

Originally Posted by Stefan (Post 1594875)
This is the same behaviour as the various C functions for converting a string to integer or floating point value.

Perhaps, but it doesn't make any sense, you gotta agree on that much. At the very least could you add another function that doesn't think 123string49foobar equals the number 123?

Loriel 08-17-2010 05:41 PM

Why the hell does Stefan have to do all the programming for you infants

stefan can you add a function to tie my shoelaces :'(

Fulg0reSama 08-17-2010 05:49 PM

Quote:

Originally Posted by Loriel (Post 1594882)
Why the hell does Stefan have to do all the programming for you infants

stefan can you add a function to tie my shoelaces :'(

Too advanced Loriel. Make them yourself. :mad:

Liberated 08-17-2010 05:55 PM

Quote:

Originally Posted by Loriel (Post 1594882)
Why the hell does Stefan have to do all the programming for you infants

because we pay him for it?

Loriel 08-17-2010 06:04 PM

Quote:

Originally Posted by Liberated (Post 1594885)
because we pay him for it?

okay so since I am paying him too i am paying him not to change float() or add a new function or whatever

Liberated 08-17-2010 06:29 PM

Quote:

Originally Posted by Loriel (Post 1594887)
okay so since I am paying him too i am paying him not to change float() or add a new function or whatever

Thats not what i ment,
Stefan is earning money with graal, we are paying him to play, and develop on it. Something is not working as expected,
they ask for a change, it is their right to ask stefan to change it for them, and not spend both their time own time on it.
You were asking why stefan had to do all the programming, not why he should or should not change float().

Fulg0reSama 08-17-2010 06:32 PM

We don't pay anyone. We rent their service to continue their work. We're more or so donaters, not tax payers so why should Stefan be expected to make anything more than he has to other than player satisfaction?

xXziroXx 08-17-2010 06:41 PM

Quote:

Originally Posted by Loriel (Post 1594882)
Why the hell does Stefan have to do all the programming for you infants

stefan can you add a function to tie my shoelaces :'(

QQ some more please, you can do it!

LoneAngelIbesu 08-17-2010 07:04 PM

For what it's worth, I've never used a float() function that didn't convert "123str" into 123. That's kind of the purpose, isn't it?

Skyld 08-17-2010 07:05 PM

Quote:

Originally Posted by xXziroXx (Post 1594790)
Definitely. Just because a string STARTS with numbers doesn't mean it's a number.

This is intentional since it allows extra notation to be correctly parsed:
PHP Code:

echo(float("1.0e4")); // 10000
echo(float("0xBC"));  // 188 

GScript is behaving in this case completely as expected, and this is the same as in JavaScript and a lot of other dynamically/variantly typed languages, so no, it can't be changed without breaking it's intended functionality.

If this is a problem then you should validate your input before processing.

MrOmega 08-17-2010 08:12 PM

Call me stupid but what are some actual purpose of using float( "123str");

Crow 08-17-2010 08:18 PM

Quote:

Originally Posted by MrOmega (Post 1594912)
Call me stupid but what are some actual purpose of using float( "123str");

Let's assume we have user input. We want a number. The user gives us a string instead of a number. Ideally, throwing it into the float()-bin will return -1 and tell us it's a string this way. When there are numbers at the beginning, it won't. Don't see an issue though. Of course, this is just an example. Might be a problem elsewhere. But that can be worked around.

LoneAngelIbesu 08-17-2010 08:36 PM

Quote:

Originally Posted by Crow (Post 1594913)
Ideally, throwing it into the float()-bin will return -1 and tell us it's a string this way.

Let's make sure to mention that, in this scenario, a custom sanitation function would be best, as Skyld said.

adam 08-17-2010 08:41 PM

Skyld is my hero.

WhiteDragon 08-17-2010 08:50 PM

Quote:

Originally Posted by Skyld (Post 1594899)
This is intentional since it allows extra notation to be correctly parsed:
PHP Code:

echo(float("1.0e4")); // 10000
echo(float("0xBC"));  // 188 


It's not like it'd be impossible to make a parser which only accepts those variants when a letter is encountered.

I also don't think it matters that this is "how it usually works" in dynamically-typed languages, since a lot of other languages do dumb things.


But the important fact is that this behavior already exists and bugs may be introduced if it is changed.

Riot 08-17-2010 10:09 PM

In my opinion, the fact that float returns -1 on error makes less sense then "123str" returning 123.

WhiteDragon 08-17-2010 10:13 PM

Quote:

Originally Posted by Riot (Post 1594939)
In my opinion, the fact that float returns -1 on error makes less sense then "123str" returning 123.

GS2 doesn't have any error handling or NaN/undefined. I suppose 0 would be a softer fail.

Edit: Post 666

LoneAngelIbesu 08-17-2010 10:59 PM

Quote:

Originally Posted by Riot (Post 1594939)
In my opinion, the fact that float returns -1 on error makes less sense then "123str" returning 123.

What doesn't make sense about float('123str') returning 123? Just look at it semantically: float() does not imply a function that checks if there are non-mathematical characters. It implies returning a float value.

Riot 08-18-2010 12:48 AM

Quote:

Originally Posted by LoneAngelIbesu (Post 1594944)
What doesn't make sense about float('123str') returning 123? Just look at it semantically: float() does not imply a function that checks if there are non-mathematical characters. It implies returning a float value.

I'm not saying it doesn't make sense, just that returning -1 (a perfectly valid value) on error makes less sense.

Loriel 08-18-2010 01:48 AM

Quote:

Originally Posted by WhiteDragon (Post 1594941)
GS2 doesn't have any error handling or NaN/undefined. I suppose 0 would be a softer fail.

Yeah that is the explanation but guy still has a point

Loriel 08-18-2010 01:49 AM

Quote:

Originally Posted by Liberated (Post 1594892)
Something is not working as expected

but it is working as expected

LoneAngelIbesu 08-18-2010 03:01 AM

Quote:

Originally Posted by Riot (Post 1594952)
I'm not saying it doesn't make sense, just that returning -1 (a perfectly valid value) on error makes less sense.

Sorry, then. The way you wrote it made it sound like you think both make no sense.

Liberated 08-18-2010 10:56 AM

Quote:

Originally Posted by Loriel (Post 1594959)
but it is working as expected

True, in the end it turns out not to be a bug, but was not the point of your comment or mine.


All times are GMT +2. The time now is 12:05 PM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.