Shop OBEX P1 Docs P2 Docs Learn Events
Beating a dead horse :) decimal math.. — Parallax Forums

Beating a dead horse :) decimal math..

s2jesses2jesse Posts: 62
edited 2009-03-21 08:05 in Propeller 1
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[noparse]:)[/noparse]

thanks for some tips

embarrassed
Jesse

Comments

  • grasshoppergrasshopper Posts: 438
    edited 2009-03-19 03:31
    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 -> [url=Http://www.rawcircuits.com]www.rawcircuits.com[/url]
  • AleAle Posts: 2,363
    edited 2009-03-19 05:57
    propeller.wikispaces.org/MATH

    It is all assembler, though smile.gif

    Ale
  • s2jesses2jesse Posts: 62
    edited 2009-03-19 06:12
    dont have my prop with me but the gps.heading is a string dont i need to convert that first?
  • mparkmpark Posts: 1,309
    edited 2009-03-20 07:50
    The FloatString object in OBEX has a StringToFloat function.
  • s2jesses2jesse Posts: 62
    edited 2009-03-20 19:25
    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 [noparse]:)[/noparse]






    ·
  • AribaAriba Posts: 2,690
    edited 2009-03-21 02:59
    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[noparse][[/noparse]16]           'Auxiliary string
                    
    PUB StrToFixed(strptr) : val | i,p,dp,ng
      p := i := dp := ng := 0
      repeat while i<DECPLC
        case byte[noparse][[/noparse]strptr++]
          "-":      ng := True
          ".":      dp := True
          "0".."9": val := val*10 + byte[noparse][[/noparse]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[noparse][[/noparse]p++] := "-"
    
      i := 1_000_000_000
    
      repeat 10
        if FxVal => i
          aux[noparse][[/noparse]p++] := FxVal / i + "0"
          FxVal //= i
          result~~
    
        elseif result or i == 1
          aux[noparse][[/noparse]p++] := "0"
        i /= 10
      aux[noparse][[/noparse]p] := 0
    
      i := strsize(@aux)-DECPLC                  'insert DecPoint at right place
      bytemove(@aux+i+1,@aux+i,i+DECPLC+1)
      aux[noparse][[/noparse] 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
  • s2jesses2jesse Posts: 62
    edited 2009-03-21 08:05
    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

    ·
Sign In or Register to comment.