Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / How to convert a String into a Float value

Author
Message
Rudders
12
Years of Service
User Offline
Joined: 20th Jan 2012
Location: Oxfordshire UK
Posted: 28th Feb 2012 13:23
Hi All,

does anyone know how to convert a string ("15.95") into a Float value (15.95)

I cannot fins a command for this? The 'Val' command will create an Integer from a string... but what about a float?

Please! Rudders
kamac
13
Years of Service
User Offline
Joined: 30th Nov 2010
Location: Poland
Posted: 28th Feb 2012 13:40 Edited at: 28th Feb 2012 13:41
I think it's currently impossible for Tier 1, but when using Tier 2 (C++) you can do that with ease.

Like that:



Or either:



If you don't want a very long output to the float.

You'd need to include stdio.h for that.

Rudders
12
Years of Service
User Offline
Joined: 20th Jan 2012
Location: Oxfordshire UK
Posted: 28th Feb 2012 13:48
Thanks but unfortunately it is Tier 1 I am using..
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 28th Feb 2012 14:01 Edited at: 28th Feb 2012 14:02
EDIT Put this in the wrong thread... doh!

Attachments

Login to view attachments
kamac
13
Years of Service
User Offline
Joined: 30th Nov 2010
Location: Poland
Posted: 28th Feb 2012 14:13 Edited at: 28th Feb 2012 14:14
Quote: "Thanks but unfortunately it is Tier 1 I am using.."


Just wait. TGC will surely add that command later on.

There should be an update at the end of this month, maybe it will have that command?

baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 28th Feb 2012 14:25
"ValFloat( str )" is in the next update.

baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 28th Feb 2012 14:38 Edited at: 28th Feb 2012 16:36
Here's a temporary fix I whipped up for you:


EDIT: fixed small bug (read below)

Rudders
12
Years of Service
User Offline
Joined: 20th Jan 2012
Location: Oxfordshire UK
Posted: 28th Feb 2012 16:02
Thanks Steve,

Will really help with 'All text' external files.

Cheers Rudders
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 28th Feb 2012 16:17
No problem

That level editor has not been forgotten BTW, just trying desperately to finish my playbook game in time...

baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 28th Feb 2012 16:19 Edited at: 28th Feb 2012 16:35
It's bugged by the way. I'll post a new version. Hold on.

EDIT: It was loosing accuracy after 6 decimal places so I've fudged a fix...


Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 28th Feb 2012 17:59 Edited at: 28th Feb 2012 18:00
I'm so glad you did that code! Thanks!


I can't believe AppGameKit cannot do VAL to float but it explains many problems I've been having!!! :o

My signature is NOT a moderator plaything! Stop changing it!
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 28th Feb 2012 18:02
My pleasure

The new valFloat command is in the next build so you won't need my version for long...

Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 28th Feb 2012 18:23
VAL should be smart enough to know the difference....

But thanks anyway!

My signature is NOT a moderator plaything! Stop changing it!
Hodgey
15
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 29th Feb 2012 05:57
Also, remember to check the UCF Project. There is a valfloat command in there I believe.

baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 29th Feb 2012 10:01
Couldn't find one there... and I did look

I was going to add my version but there's not much point as the new one will be out soon. Unless you think it's worth it?

Hodgey
15
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 29th Feb 2012 11:50
Quote: "Couldn't find one there... and I did look"

You are correct, this is the thread I was thinking of and for some reason, I thought I posted my function in the UCF project but didn't. That's the second mistake I've made today so I'm going to log off before I hurt somebody. See you all tomorrow.

baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 29th Feb 2012 11:56
LOL, didn't see that version but it seems we have a similar way around the problem. I like your solution too, it's a lot neater than mine although I didn't test accuracy. Mine had a problem past 6 decimal places which I added the little fix for.

Rich Dersheimer
AGK Developer
15
Years of Service
User Offline
Joined: 1st Jul 2009
Location: Inside the box
Posted: 29th Feb 2012 19:22
I posted my version here...

http://forum.thegamecreators.com/?m=forum_view&t=188719&b=41

because I was trying to get a float from a user text input.
Hodgey
15
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 29th Feb 2012 21:38 Edited at: 29th Feb 2012 21:40
Quote: "I like your solution too, it's a lot neater than mine although I didn't test accuracy."

Thanks baxslash, mine's based on division and so is limited to the accuracy of AGK's division. A small example of its loss of accuracy:



As yours is string based, it shouldn't suffer from the same problem.

And Rich beat the lot of us.

baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 29th Feb 2012 21:57
Looks like we all have our own way to do the same operation

Mine appears to be the only one that can calculate this value though:


Hodgey
15
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 1st Mar 2012 07:38
Quote: "As yours is string based, it shouldn't suffer from the same problem. "

Quote: "Mine appears to be the only one that can calculate this value though:"

QED: Thus it is proved. It'll be interesting to test the speed of yours against the 107 build's native one.

baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 1st Mar 2012 10:37 Edited at: 1st Mar 2012 10:37
Quote: "It'll be interesting to test the speed of yours against the 107 build's native one."

I am using my newly acquired crystal ball and it tells me AppGameKit is pretty likely to be quite a bit faster

EDIT: Hodgey, you need to update the link in your sig...

Hodgey
15
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 1st Mar 2012 11:53 Edited at: 1st Mar 2012 11:54
Quote: " Hodgey, you need to update the link in your sig..."

How embarrassing! Thanks for that.

Testing it now.

Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 13th Mar 2012 17:54
I really like the richFloat function best. I think it has the potential for fastest execution. It has only one loop and, at most, two calls to val and two multiplication operations (division is just a strange case of multiplication).

Here is a suggested update to it, to increase speed and fix one bug (which is probably why it didn't output what was expected):


Cheers,
Ancient Lady
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 13th Mar 2012 18:01
I'm afraid it returns 183.000000 for this string "183.287956341278564".

This was the main bug. It might be that by simply truncating the decimal part to 6 spaces would fix this (that's what I had to do in my own version).

Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 13th Mar 2012 18:45
Strange.

Here is the version I'm now using in my code:


I like short variable names, and I had an old 'FloatVal' function, so I stuck with that name.

I looked at what I posted and can't see where I goofed making the names longer.

I just ran some tests to see how long fractional parts gets handled (the Str for the results specifies 12 decimal places):


It looks like we get somewhat strange behavior. I even tried calculating the two parts seperately and still got the same results.

The initial calculation for the fractional part shows it's ok out to the first 7 or 8 places and then messes up. And it get further messed up when combined with the whole part.

I think we are seeing a basic problem with handling large fractions. It may be due to how the basic is doing multiplications or the range it allows in storage (as hinted by the second mess up). If only 32 bit math/values are being used, that might be part of it.

I tried all three of the float conversion versions and they all returned the same result. I adjust the original FloatVal to allow up to 10 decimal places.

If it were Tier2 or Fortran (yup, I've done serious graphics and science in that language, I've even programmed in COBOL), I'd make sure that the variables were all the long version of floats (64 bits).

Cheers,
Ancient Lady
Rich Dersheimer
AGK Developer
15
Years of Service
User Offline
Joined: 1st Jul 2009
Location: Inside the box
Posted: 13th Mar 2012 21:38 Edited at: 13th Mar 2012 21:46
Oh, I forgot, my version suffers from the change in how right() is calculated by AGK. If you take out the -1 in the right() it works okay, but still won't get past 5 decimal places.
JimHawkins
15
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 14th Mar 2012 01:24
I'm afraid that the test numbers are massively overflowing the precision of the 32-bit floating point numbers used by AGK. I've just made a 5 minute Delphi program so that you can see the problem.

The float can only support about 7 digits of precision.

I've uploaded a zip with the executable - what you will see is the results correspond well with what you've done, but float type really only has 23 bits of precision inside the 32-bit value, which has to take sign and mantissa into account.

This is the Delphi code for the Do It button:



Single = 32-bit
Double = 64-bit
Extended = 128-bit

Using the example numbers, here is the output:



The maximum precision of the AppGameKit float type is approximately 7 significant digits. Double is about 15 significant digits, and the extended about 19. The maxima are variant because floating point is strange beast that can have a value for infinity!

Zip with the exe attached. Try pushing the numbers across the 7 digit limit and you'll see what happens.

-- Jim

Attachments

Login to view attachments
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 14th Mar 2012 16:29
After digging about a bit to find the correct page....
(http://www.appgamekit.com/documentation/principles/1_datatypes.htm)

The AppGameKit documentation specifically states that the real range only goes to 7 digits.

Quote: "
INTEGER Range : -2,147,483,648 to 2,147,483,647
REAL Range : 3.4E +/- 38 (7 digits)
"


I suspect that is probably sufficient for most applications being done with AGK.

Cheers,
Ancient Lady
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 14th Mar 2012 18:48
Yeah, I doubt I'll need to worry about being any more accurate than that

Login to post a reply

Server time is: 2024-11-23 04:51:46
Your offset time is: 2024-11-23 04:51:46