String to Floating point?
Sniper King
Posts: 221
Tried doing a search on this and I cant find it.· Any help here.
bytemove(@str,string("31.2345"), 7)
I want X [url=mailto:x=@str]= @str[/url]· where x is Long
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·- Was it pin 11 or 26?· Hmmm....··I think the smell of smoke tells the whole story.· Must be 26.
Michael King
Application Engineer
R&D
Digital Technology Group
bytemove(@str,string("31.2345"), 7)
I want X [url=mailto:x=@str]= @str[/url]· where x is Long
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·- Was it pin 11 or 26?· Hmmm....··I think the smell of smoke tells the whole story.· Must be 26.
Michael King
Application Engineer
R&D
Digital Technology Group
Comments
X := 31.2345
and the compiler will do the conversion for you
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·- Was it pin 11 or 26?· Hmmm....··I think the smell of smoke tells the whole story.· Must be 26.
Michael King
Application Engineer
R&D
Digital Technology Group
string :+ "31.2345\0" ' extracted from gps read
integer := 0
i := 0
repeat until string == "."
integer := 10 * integer + string - "0"
i ++
fraction := 0
i := 0
repeat until string == $0
fraction:= 10 * fraction + string - "0"
i ++
fpResult := FAdd(FFloat(integer), FDiv(FFloat(fraction), ###))
### is either a constant or calculated from i
I did notice that the number of decimals from my gps was different from the number of decimals in the example in the NMEA documentation I was using.
I wish the Spin complier would let me define variables as float and translate C := A + B to C := FAdd(A,B) for me.
John Abshier
in the Extended_FD_Serial-object
found by a search on the word fraction in the obex
obex.parallax.com/objects/search/?q=fraction
there are two PUB-Methods to extract the digits before and after the decimaldot
you can use this as a base for your needs
best regards
Stefan
John Abshier
well you can sort the obex-table on each columtitle by clicking on the colummtitle
I made a shortcut to the configuration: all objects sorted by published-date descending
obex.parallax.com/objects/?o=0&ot=dsc
I use this for my regularly check about new uploaded objects in the obex.
best regards
Stefan
I know, I know I should just pass a string pointer but when I did that it only passed the string up to the decimal point. So this worked and i stayed with it.
Oh yeah, I am finding some weird byte array issues that i fix by moving their declaration lines in front of other declaration. it doesn't affect the variables that I moved in front of.... Weird huh?
[noparse][[/noparse]code]
PUB Str2Float :c | a· ,i , B· ,k
a:=1
c:=0
·
a:=fp.ffloat(a)
···
· c:=fp.ffloat(c)
· repeat i from 0 to strsize(@buf)-1
····
···
····· if buf[noparse][[/noparse]i]=="."
········
······ quit
··· if buf[noparse][[/noparse]i]<>"-"··
···· a:=fp.fmul(a,10.0)···
··
·a:=fp.fdiv(a,10.0)
·· k:=0
· repeat i from 0 to strsize(@buf)
··· b:=0.0
·· if buf[noparse][[/noparse]i]=="-"
····· k:=1
···
·· if buf[noparse][[/noparse]i]=="1"
··· b:=fp.fmul(1.0,a)
·· if buf[noparse][[/noparse]i]=="2"
··· b:=fp.fmul(2.0,a)
·· if buf[noparse][[/noparse]i]=="3"
··· b:=fp.fmul(3.0,a)
·· if buf[noparse][[/noparse]i]=="4"
··· b:=fp.fmul(4.0,a)
·· if buf[noparse][[/noparse]i]=="5"
··· b:=fp.fmul(5.0,a)
·· if buf[noparse][[/noparse]i]=="6"
··· b:=fp.fmul(6.0,a)
·· if buf[noparse][[/noparse]i]=="7"
··· b:=fp.fmul(7.0,a)
·· if buf[noparse][[/noparse]i]=="8"
··· b:=fp.fmul(8.0,a)
·· if buf[noparse][[/noparse]i]=="9"
··· b:=fp.fmul(9.0,a)
····
·· if buf[noparse][[/noparse]i]<>"." and buf[noparse][[/noparse]i]<>"-"
······ a:=fp.fdiv(a,10.0)
·· C:=fp.fadd(c,b)
··
···
·if k==1
······ c:=fp.fneg(c)
·
[noparse][[/noparse]code/]
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·- Was it pin 11 or 26?· Hmmm....··I think the smell of smoke tells the whole story.· Must be 26.
Michael King
Application Engineer
R&D
Digital Technology Group