View Full Version : Beating a dead horse :) decimal math..
03-19-2009, 10:59 AM
This has been asked a million times ive searched it all but still am lost. First off is there a· beginners faq/ tut/ guide to·math with the prop? Ive·got all my navigation stuff working with float math but need to double buffer the video so i dont have enough memory for the float32 stuff...
Say im using the gps object.. first lets just say we want to work with heading.. (I know the decimal isnt that important but for use as an example)
x := gps.heading······ gets a string of say 352.5
First. I think im supposed to just treat that as just 3525 but i dont even know how to "treat" that as 3525 in spin.·Ie convert it from the string.
Then what about BIG numbers like a latitude
so easy with teh float objects:)
thanks for some tips
03-19-2009, 11:31 AM
Just take the 352.5 and multiply it by 10. Then you will have the number 3525.
x := gps.heading
Y := X * 10
Visit my site -> www.rawcircuits.com (Http://www.rawcircuits.com)
It is all assembler, though http://forums.parallax.com/images/smilies/smile.gif
03-19-2009, 02:12 PM
dont have my prop with me but the gps.heading is a string dont i need to convert that first?
03-20-2009, 03:50 PM
The FloatString object in OBEX has a StringToFloat function.
03-21-2009, 03:25 AM
thanks but mainly just trying to learn it all..
In fact ive got my whole gps/waypoint navigation and wayppoint indicators all fully working with stringtofloat, float32 stuff and all the math all working...
I'm tryign to rewrite it all without using any floats and truly learn what im doing rather than just using other peoples objects blindly :)
03-21-2009, 10:59 AM
here are some routines to do Fixed point math. Its set up for 2 decimal places, but this can be changed with 2 constants:
DECPLC = 2 'Number of decimal places
FACTOR = 100 '10^DECPLC
byte aux 'Auxiliary string
PUB StrToFixed(strptr) : val | i,p,dp,ng
p := i := dp := ng := 0
repeat while i<DECPLC
"-": ng := True
".": dp := True
"0".."9": val := val*10 + byte[strptr-1]-"0"
other: if dp
return fxA * fxB / FACTOR
return fxA*FACTOR / fxB
' For Add and Sub you use the normal operators (+,-)
PUB FixedToString(fxVal) | i,p
if fxVal < 0 'Integer to String
aux[p++] := "-"
i := 1_000_000_000
if FxVal => i
aux[p++] := FxVal / i + "0"
FxVal //= i
elseif result or i == 1
aux[p++] := "0"
i /= 10
aux[p] := 0
i := strsize(@aux)-DECPLC 'insert DecPoint at right place
aux[ i] := "."
return @aux 'return pointer to ResultString
And here an Example how to use it:
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
tv : "TV_Text"
PUB main | a,b
a := StrToFixed(string("99.9"))
b := StrToFixed(string("-12.34"))
a += b
a := StrToFixed(string("10"))
b := StrToFixed(string("12.34"))
a := FDiv(a,b)
a := StrToFixed(string("2.0"))
a := FSqr(a)
Hope this help...
03-21-2009, 04:05 PM
yes thanks this is perfectly what i need.
Extremely helpful! Easy to see whats going on thanks I learned alot from this example