String to Float
Sniper King
Posts: 221
I may have posed this question before but I don't think I got an answer.·
I have a string (GPS) and I want to convert this to a floating value.· i can't find anything that converts a string pointer to a floating value.· I wrote a small program to do this but it stops at 4 decimal places.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·- Ouch, thats not suppose to be hot!··
Michael King
Application Engineer
R&D
Digital Technology Group
I have a string (GPS) and I want to convert this to a floating value.· i can't find anything that converts a string pointer to a floating value.· I wrote a small program to do this but it stops at 4 decimal places.
PUB Str2Float(ptr) :c | a ,i , B ,k a:=1 c:=0 a:=fp.ffloat(a) c:=fp.ffloat(c) repeat i from 0 to strsize(ptr)-1 if byte[noparse][[/noparse]ptr+i]=="." quit if byte[noparse][[/noparse]ptr+i]<>"-" a:=fp.fdiv(a,10.0) a:=fp.fmul(a,10.0) k:=0 repeat i from 0 to strsize(ptr) b:=0.0 if byte[noparse][[/noparse]ptr+i]=="-" k:=1 if byte[noparse][[/noparse]ptr+i]=="1" b:=fp.fdiv(1.0,a) if byte[noparse][[/noparse]ptr+i]=="2" b:=fp.fdiv(2.0,a) if byte[noparse][[/noparse]ptr+i]=="3" b:=fp.fdiv(3.0,a) if byte[noparse][[/noparse]ptr+i]=="4" b:=fp.fdiv(4.0,a) if byte[noparse][[/noparse]ptr+i]=="5" b:=fp.fdiv(5.0,a) if byte[noparse][[/noparse]ptr+i]=="6" b:=fp.fdiv(6.0,a) if byte[noparse][[/noparse]ptr+i]=="7" b:=fp.fdiv(7.0,a) if byte[noparse][[/noparse]ptr+i]=="8" b:=fp.fdiv(8.0,a) if byte[noparse][[/noparse]ptr+i]=="9" b:=fp.fdiv(9.0,a) if byte[noparse][[/noparse]ptr+i]<>"." and byte[noparse][[/noparse]ptr+i]<>"-" a:=fp.fmul(a,10.0) C:=fp.fadd(c,b) debug.str(fs.floattostring(a)) debug.str(string(" - ")) debug.str(fs.floattostring(c)) debug.str(string(13)) if k==1 c:=fp.fneg(c)
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·- Ouch, thats not suppose to be hot!··
Michael King
Application Engineer
R&D
Digital Technology Group
Comments
I have been working on this for the last couple of days, but it is low man on the totem pole as far as priorities. Here is where the code is so far, but it's not complete and hasn't been tested.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Paul Baker
Propeller Applications Engineer
Parallax, Inc.
Post Edited (Paul Baker (Parallax)) : 9/11/2008 6:09:15 PM GMT
I have seen this but I don't know how it works.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·- Ouch, thats not suppose to be hot!··
Michael King
Application Engineer
R&D
Digital Technology Group
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Paul Baker
Propeller Applications Engineer
Parallax, Inc.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·- Ouch, thats not suppose to be hot!··
Michael King
Application Engineer
R&D
Digital Technology Group
31.48293
-110.246
It seems to me i should be getting a few more places before it goes dumb
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·- Ouch, thats not suppose to be hot!··
Michael King
Application Engineer
R&D
Digital Technology Group
The IEEE floating point format allows 23 bits for a mantissa which only provides for slightly less than 7 digits of accuracy.
I don't know how you're getting the numbers you're showing in your message, but you should get a little more accuracy.
Here is my output
String we are converting to a floating point value= -110.246233
Floatstring setprecision=7
Multiplier···· ·· Value· multiplied······· New value from Floatstring
················shown from floatString····
100····················· 1··························100
10······················ ·1························ ·110
0······················· ·0························ ·110
.1······················ ·.2······················· ·110.2
.01······················ .04······················ ·110.24
.001···················· .005···················· ·110.245
.0001················ · .0009················· 110.2459
.00001·············· ·.00007················ 110.246
.000001············ ·.000006··············· 110.246
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·- Ouch, thats not suppose to be hot!··
Michael King
Application Engineer
R&D
Digital Technology Group
There are all sorts of assumptions and round-off errors inherent in that, particularly when you have just slightly
less than 7 significant decimal digits of accuracy. If you want to check the accuracy, you'll have to hand convert
the floating point value (or write your own conversion program) by keeping it in binary for as long as possible,
applying the binary exponent, then converting first the integer portion to decimal, then the fraction portion.
With a value on the order of 110, you will only get between 4 and 5 decimal places. That's all the floating point
routines are capable of providing. If you need more, you may have to create your own arithmetic package.
Post Edited (Mike Green) : 9/11/2008 7:01:18 PM GMT
You need a floating point object included as f.
See also the thread here: http://forums.parallax.com/showthread.php?p=738380
Andy
I disagree: That's all the float32 package is able to provide. There are other implementations around: the 12 digit BCD package and the double precision package (sqrt comes soon) that I described at the propeller.wikispaces.com/MATH
Conversion routines and spin wrappers are not provided, I use them from assembler , but can be easily created.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
--Steve
·
is resulting in this from the serial terminal:
* See attached image, looks like I cant cut text from the serial terminal I'm using.
Any ideas where that 10^38 is coming from?
Not remotely an expert, but should those lines be ...
.. or ..
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Cardinal Fang! Fetch the comfy chair.
That is _really_ strange. All floats are single precision IEEE floats. In theory..
40.0
Float(40)
and floatm.ffloat(40)
.. should all produce absolutely identical 32 bit constants. If they don't then I think we have a problem.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Cardinal Fang! Fetch the comfy chair.