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 / reading and writing floats

Author
Message
netmon
18
Years of Service
User Offline
Joined: 14th Aug 2006
Location:
Posted: 10th Dec 2011 04:45
How do you go about reading and writing floats and get them back accurately?

I need to be able to read 0.5 from a file as a string and write 0.5 as a float, just doesn't seem right that it's being rounded down to 0.00000

Basically, I am stripping an xml file of its tags, and writing out the values from that. I test the line to see if it's a number or not. if its a number i write it to one file with writefloat(), otherwise, writeline()

There has to be some workaround for this.

I can see the placement editor is able to do it, but I suspect it was written in C
Dybing
13
Years of Service
User Offline
Joined: 12th Sep 2011
Location: Bergen, Norway
Posted: 10th Dec 2011 12:37
You need to manually convert the string into a float by using some string functions to decide what is before and after the decimal marker (. or , depending on where in the world you live).

Basic logic of it is:

-read the string as is
-into a substring (let's call it temp1) you load all the numbers before the period or comma.
-into another substring (let's call it temp2) you load the numbers after the period or comma
-next you load a float with the value of temp1 + the value of temp2 divided 10 per decimal

I have such a function that reads a string from text-input and do the convertion into float for an application I've made. It basically reads the weight of a LED panel with 0 to 3 decimals. Code is supplied, but
is probably not that easy to follow, as I've not altered it for readability... But it do work

netmon
18
Years of Service
User Offline
Joined: 14th Aug 2006
Location:
Posted: 10th Dec 2011 14:06
Thanks. After I woke up this morning and thought more about it more, I realized I was going to have to do just this.
Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 10th Dec 2011 18:14
Why not just write it as a float? Or read it in an store it like so:
Float# = Val( ReadString( File ) )

My signature is NOT a moderator plaything! Stop changing it!
Dybing
13
Years of Service
User Offline
Joined: 12th Sep 2011
Location: Bergen, Norway
Posted: 11th Dec 2011 22:13 Edited at: 11th Dec 2011 22:25
Quote: "Why not just write it as a float? Or read it in an store it like so:
Float# = Val( ReadString( File ) )"


The problem is, that val() will only return an integer, no matter what you do. Unless they've changed it in a patch, and I've not caught it in one of the patch-notes.

So AFAIK:

myString$ = "1.4"
myFloat# = val(myString$)
print myFloat#

will output 1, not 1.4

Thus the need to "wash" the string to extract the numbers before and after the decimal-point.

Perhaps not that crucial with file i/o if you know you'll be reading a float, you just do so as you suggest. But for user-input there is no ready option, apart from a piece of code similar to what I showed in my previous post (though I'm sure it could be made more elegant. I tend to brute-force my way through a problem step by step rather than spend time on efficient and pretty code )
netmon
18
Years of Service
User Offline
Joined: 14th Aug 2006
Location:
Posted: 12th Dec 2011 01:33
Here's my reval function, it looks to see if it's a float or not.

Login to post a reply

Server time is: 2024-11-22 19:52:23
Your offset time is: 2024-11-22 19:52:23