Shop OBEX P1 Docs P2 Docs Learn Events
code review needed: StringToFloat — Parallax Forums

code review needed: StringToFloat

mparkmpark Posts: 1,309
edited 2009-03-06 22:47 in Propeller 1
The FloatString object in obex does not have a string-to-float conversion, so I wrote my own and added it to my private copy of FloatString. I'm wondering about updating the official FloatString but thought I should let you guys review the code first. So here it is. Let me know if it's useful, correct (within its limitations), etc.

PUB StringToFloat(strptr) : flt | significand, ssign, places, exp, esign 
{{
  Converts string to floating-point number
  entry:
      strptr = pointer to z-string

  exit:
      flt = floating-point number


  Assumes the following floating-point syntax: [noparse][[/noparse]-] [noparse][[/noparse]0-9]* [noparse][[/noparse] . [noparse][[/noparse]0-9]* ] [noparse][[/noparse] e|E [noparse][[/noparse]-|+] [noparse][[/noparse]0-9]* ]
                                               ┌── ┌───── ┌─────────── ┌───────────────────
                                               │   │      │            │     ┌──── ┌─────
    Optional negative sign ────────────────────┘   │      │            │     │     │
    Digits ────────────────────────────────────────┘      │            │     │     │
    Optional decimal point followed by digits ────────────┘            │     │     │
    Optional exponent ─────────────────────────────────────────────────┘     │     │
      optional exponent sign ────────────────────────────────────────────────┘     │
      exponent digits ─────────────────────────────────────────────────────────────┘

  Examples of recognized floating-point numbers:
  "123", "-123", "123.456", "123.456e+09"
  
  Conversion stops as soon as an invalid character is encountered. No error-checking.
    
  Based on Ariba's StrToFloat in http://forums.parallax.com/forums/default.aspx?f=25&m=280607
}}
  significand~
  ssign~
  exp~
  esign~
  places~
  repeat
    case byte[noparse][[/noparse]strptr]
      "-":
        ssign~~
      ".":
        places := 1
      "0".."9":
        significand := significand * 10 + byte[noparse][[/noparse]strptr] - "0"
        if places
          ++places                    'count decimal places
      "e", "E":
        ++strptr ' skip over the e or E
        repeat
          case byte[noparse][[/noparse]strptr]
            "+":
              ' ignore
            "-":
              esign~~
            "0".."9":
              exp := exp * 10 + byte[noparse][[/noparse]strptr] - "0"
            other:
              quit
          ++strptr
        quit              
      other:
        quit
    ++strptr
    
  if ssign
    -significand
  flt := f.FFloat(significand)

  ifnot esign  ' tenf table is in decreasing order, so the sign of exp is reversed
    -exp
    
  if places
    exp += places - 1
        
  flt := f.FMul(flt, tenf[noparse][[/noparse]exp])              'adjust flt's decimal point

Comments

  • cessnapilotcessnapilot Posts: 182
    edited 2009-03-06 10:20
    mpark: your code is correct and elegant. I have made a small application to check it (see attached file). If you allow me, I will upgrade a GPS_Float object in OBEX with your·StringToFloat ·procedure , since some NMEA sentences may contain wider range of floats than the original (very simple) conversion routine expects.
    Thanks
  • mparkmpark Posts: 1,309
    edited 2009-03-06 16:33
    Thanks for your comments. You're free to use it any way you like. I'll look into adding it to the official FloatString.

    I'm not familiar with GPS_Float, but if it uses FloatString wouldn't it be better just to use the updated FloatString?
  • mparkmpark Posts: 1,309
    edited 2009-03-06 22:47
    Tried to update FloatString on obex and got an error. Will Parallax system administrators investigate immediately, as the error message says? We shall see.

    Edit: I don't know if anyone investigated, but I tried again and this time it worked. FloatString 1.2 is good to go.

    Post Edited (mpark) : 3/7/2009 8:48:55 AM GMT
Sign In or Register to comment.