code review needed: StringToFloat
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
Thanks
I'm not familiar with GPS_Float, but if it uses FloatString wouldn't it be better just to use the updated FloatString?
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