Shop OBEX P1 Docs P2 Docs Learn Events
String to Floating point? — Parallax Forums

String to Floating point?

Sniper KingSniper King Posts: 221
edited 2008-06-11 18:53 in Propeller 1
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

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-06-10 18:53
    There's no ready-made generalized string to float conversion routine. If you have constants at compile time, you can write

    X := 31.2345

    and the compiler will do the conversion for you
  • Sniper KingSniper King Posts: 221
    edited 2008-06-10 18:59
    The is a constanly changing value. GPS

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·- 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
  • John AbshierJohn Abshier Posts: 1,116
    edited 2008-06-10 20:16
    Here is some pseudo code off the top of my head
    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
  • StefanL38StefanL38 Posts: 2,292
    edited 2008-06-10 21:32
    Hello Michael,

    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

    PUB IntOfString (stringptr): Value  | negFlag
    {{ Returns the integer or whole portion of a string with a decimal point.
       serial.RxString(@myStr))
       x := serial.IntOfString(@myStr)
       "-123.456" will return -123
    }}
        repeat strsize(stringptr)
            if  byte[noparse][[/noparse]stringptr] == "-"
                negFlag := true
            if (byte[noparse][[/noparse]stringptr] => ("0")) and (byte[noparse][[/noparse]stringptr] =< ("9"))
                value := value * 10 + (byte[noparse][[/noparse]stringptr]-"0")
            if byte[noparse][[/noparse]stringptr] == "."
               if NegFlag ==  true
                   value := value * -1
                   !negFlag
               quit
            stringptr++
        if NegFlag ==  true
           value := value * -1
           !negFlag 
    
    PUB FracOfString (stringptr): Value  | decFlag
    {{ Returns the fractional or decimal portion of a string with a decimal point.
       serial.RxString(@myStr))
       x := serial.FracOfString(@myStr)
       "-123.456" will return 456
    }}
        repeat strsize(stringptr)
            if  byte[noparse][[/noparse]stringptr] == "."
                decFlag := true
            if decFlag == True
               if (byte[noparse][[/noparse]stringptr] => ("0")) and (byte[noparse][[/noparse]stringptr] =< ("9"))
                  value := value * 10 + (byte[noparse][[/noparse]stringptr]-"0")
            stringptr++
    
    
    
    



    you can use this as a base for your needs

    best regards

    Stefan
  • John AbshierJohn Abshier Posts: 1,116
    edited 2008-06-11 01:29
    The Extended_FDSerial.spin that is distributed with Propeller Tool Ver 1.2 is 1.0. You need to go to the object exchange to get version 1.1 that thas the IntOfString and FracOfString. I wish I knew how to manage my objects and how to easily check to see if any objects have been updated.

    John Abshier
  • StefanL38StefanL38 Posts: 2,292
    edited 2008-06-11 18:41
    hello John,

    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
  • Sniper KingSniper King Posts: 221
    edited 2008-06-11 18:53
    I spent all night but I got the thing to work. Simple but it works...

    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
Sign In or Register to comment.