View Full Version : Beating a dead horse :) decimal math..

s2jesse

03-19-2009, 09: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

embarrassed

Jesse

grasshopper

03-19-2009, 10: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

Try that.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔

Visit my site -> www.rawcircuits.com (Http://www.rawcircuits.com)

propeller.wikispaces.org/MATH

It is all assembler, though http://forums.parallax.com/images/smilies/smile.gif

Ale

s2jesse

03-19-2009, 01:12 PM

dont have my prop with me but the gps.heading is a string dont i need to convert that first?

mpark

03-20-2009, 02:50 PM

The FloatString object in OBEX has a StringToFloat function.

s2jesse

03-21-2009, 02: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 :)

·

Ariba

03-21-2009, 09:59 AM

s2jesse

here are some routines to do Fixed point math. Its set up for 2 decimal places, but this can be changed with 2 constants:

CON

DECPLC = 2 'Number of decimal places

FACTOR = 100 '10^DECPLC

VAR

byte aux[16] 'Auxiliary string

PUB StrToFixed(strptr) : val | i,p,dp,ng

p := i := dp := ng := 0

repeat while i<DECPLC

case byte[strptr++]

"-": ng := True

".": dp := True

"0".."9": val := val*10 + byte[strptr-1]-"0"

if dp

i++

other: if dp

val*=10

strptr--

i++

else

quit

ifnot dp

val*=FACTOR

if ng

-val

PUB FMul(fxA,fxB)

return fxA * fxB / FACTOR

PUB FDiv(fxA,fxB)

return fxA*FACTOR / fxB

PUB FSqr(fxVal)

return ^^(fxVal*FACTOR)

' For Add and Sub you use the normal operators (+,-)

PUB FixedToString(fxVal) | i,p

p~

if fxVal < 0 'Integer to String

-FxVal

aux[p++] := "-"

i := 1_000_000_000

repeat 10

if FxVal => i

aux[p++] := FxVal / i + "0"

FxVal //= i

result~~

elseif result or i == 1

aux[p++] := "0"

i /= 10

aux[p] := 0

i := strsize(@aux)-DECPLC 'insert DecPoint at right place

bytemove(@aux+i+1,@aux+i,i+DECPLC+1)

aux[ i] := "."

return @aux 'return pointer to ResultString

And here an Example how to use it:

CON

_clkmode = xtal1 + pll16x

_xinfreq = 5_000_000

OBJ

tv : "TV_Text"

PUB main | a,b

tv.start(12)

a := StrToFixed(string("99.9"))

b := StrToFixed(string("-12.34"))

a += b

tv.str(FixedToString(a))

tv.out(13)

a := StrToFixed(string("10"))

b := StrToFixed(string("12.34"))

a := FDiv(a,b)

tv.str(FixedToString(a))

tv.out(13)

a := StrToFixed(string("2.0"))

a := FSqr(a)

tv.str(FixedToString(a))

repeat

Hope this help...

Andy

s2jesse

03-21-2009, 03:05 PM

Ariba!

yes thanks this is perfectly what i need.

Extremely helpful! Easy to see whats going on thanks I learned alot from this example

Jesse

·