Shop OBEX P1 Docs P2 Docs Learn Events
Floating point tutorial... Maybe... Please.... — Parallax Forums

Floating point tutorial... Maybe... Please....

Sniper KingSniper King Posts: 221
edited 2008-08-22 18:23 in Propeller 1
Floating point math is such an integral part of accuracy in most of the applications we are all going to write.· Understandably, some brilliant programmer wrote the floating point object.· Being the developer, I am sure, he understands the ins and outs of using that object fully.·

Now speaking for myself, I would love to completely understand the use of this object and how to convert,assign and utilize floating point math as if I were writing in BASIC or some other high level language so it seems almost second nature.· For those who have spent alot of time in embedded systems, you as most of us·are subject to the awsome learning curve.·

I hope I am speaking for many here where a possible floating point sticky would make the transition to high level math a lot easier and make the propeller more powerful even to the beginner.

I hope this is reaching the right people.





▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·- Ouch, thats not suppose to be hot!··


Michael King
Application Engineer
R&D
Digital Technology Group

Comments

  • AleAle Posts: 2,363
    edited 2008-08-21 19:02
    I'm planning to add binary floating point to my Math article at wikispaces... so far only BCD based floating point. But I think you may not want to wait. Did you have a look at the sourcecode ? I thought it was sort of straightforward... but I already wrote fp for several processors...
  • Sniper KingSniper King Posts: 221
    edited 2008-08-21 19:41
    No I haven't looked at it. I imagine most of us on the forum have some math behind us as far as education goes. It seems to me that this is a very important aspect of the propeller that needs to be explained in detail for those of us who are new to the propeller. This is so important especially when you are dealing with great circle math. I realize that there are objects available that do this but it would be nice to look at it and totally understand why it works and even be able to check their work (No Offence meant). Math is everything to a computer! this is the coolest little computer I have ever played with! I want to know everything about it but doing leaning it tiny peice by tiny peice without ever grokking the whole concept leaves you knowing how to write code but not why it works.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·- Ouch, thats not suppose to be hot!··


    Michael King
    Application Engineer
    R&D
    Digital Technology Group
  • grasshoppergrasshopper Posts: 438
    edited 2008-08-21 20:12
    Well this seems to be a no-logical thread to me because the object currently used for floating point is real easy to understand. I am somewhat confused on the question or position that your are taking here. You want to talk about floating point? Shoot ask a more specific question and ill do my best to help.
  • ElectricAyeElectricAye Posts: 4,561
    edited 2008-08-21 20:24
    As though my opinion matters...

    I think a big contributing factor to confusion in general is the overall lack of comments on many of the programs (objects). I'm not dumb, and I can learn a lot by example, but I become completely imbecilic sometimes when I try to figure out how some of these objects do what they do. The brilliant minds who write the code for them breeze right through it, like one of those piano masters who can pick up a piece of new music and just start playing. But for people like me, I'm a mechanical type who needs little computers to do things for me so I want to drop in, figure this stuff out, get it up and running, and go on to the next thing. But without comments to show me the way, I'm quickly lost, bogged down, depressed, fed up. The Propeller has a lot of potential, but without really good documentation, it's gonna take a lot longer for people like me to grab onto it and hug it for all it's worth.

    At the very least the objects should have a list of what the limits on the variables are. Many times, when faced with what variables are going in or out of an object, it's mind-reading time.

    And when it comes to dealing with objects, I feel less and less like an engineer and more and more like a REVERSE engineer...

    It's newbie whining, maybe, and nobody here is expecting Noble-prize winning literature in their programs... but even a few comments can go a really long way.

    Mark

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    It might be the Information Age but the Eon of Ignorance has yet to end.
  • Sniper KingSniper King Posts: 221
    edited 2008-08-21 21:36
    My point exactly! This is a white-box/Black box argument here. When you come from embedded programming you have some background that will give you something to start with. If you have been doing float math on every chip you have ever used, adapting to this would of course not be too hard.

    "Well this seems to be a no-logical thread to me because the object currently used for floating point is real easy to understand"

    Easy for you to say. I look at the example and might as well be looking at a sentence in Russian! Why? Why? Why? There are so many questions. A simple Line by line explination as to why we use different things helps us newbies. I have been writing software for 20+ years and this is hard for me.

    While there are master programmers on this forum, I bet there are alot more beginners that are excited about the propeller and can think collectively of millions of applications but are intimidated by "simple" things like floating point math. These hurdles are going to prevent many from undertaking the challenge of creating a great application for the propeller.

    I reiterate! Some of us could really use a tutorial and I think I speak for a bunch of us!

    I know that I won't stop using the propeller. If I have to spend a lot of time learning how to do floating point math, i will. But, that is why we have forums, so we can help each other. I am only asking for help here. I am in the process of building devices that will save lives in Iraq and afganistan. The sooner I learn the sooner they get it.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·- Ouch, thats not suppose to be hot!··


    Michael King
    Application Engineer
    R&D
    Digital Technology Group
  • Erik FriesenErik Friesen Posts: 1,071
    edited 2008-08-21 23:01
    Just a couple of comments. I suspect that even the brilliant minds on this forum have to work to figure out code that they have not written. Chip didn't just sit down and throw together the video drivers I don't think.

    Are you sending this floating point to another processor? I find the floating point object so cumbersome that I have reverted to figuring all my math without floating point, and then printing the result with a point. For example, what do you suppose this formula for figuring altitude would look like with spin floating point?

    tal:=(inhgdif*50000)/(540-(inhgdif*1000/((11500+(inhgdif*10/7)))))/10
  • Sniper KingSniper King Posts: 221
    edited 2008-08-21 23:25
    unfortunately when you need 32bit math, you need 32bit math say for example Great circle math. If you multiply everything by 100000 you are going to lose something somewhere.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·- Ouch, thats not suppose to be hot!··


    Michael King
    Application Engineer
    R&D
    Digital Technology Group
  • Sniper KingSniper King Posts: 221
    edited 2008-08-21 23:32
    Ok my first to the point question.

    VAR

    Long Pterm
    Long Iterm
    Long Dterm


    OBJ

    FP : "Float32Full"

    PUB PID(KP, KI, KD , cErr, aErr , lErr,Time):Value |PIDs
    fp.start
    pterm:= FP.fmul(KP , cErr)

    iterm:= FP.fmul(KI , aErr)

    dterm:= FP.fmul(KD ,fp.fdiv(fp.fsub(cErr , lErr),Time))
    PIDs:= fp.fadd(fp.Fadd(pterm , dterm) , iterm)
    return PIDs
    fp.stop

    Here is my highspeed PID loop routine. It don't work!
    Can someone look at this and give me a heads up as to where I am dumb.

    Here is the routine that calls the above Object:

    PUB Main | count
    fp.start
    Serial.start(31, 30, %0000, 38400) 'Display Data in Basic Stamp Debug Terminal

    Serial.Str(string("***********************"))
    Serial.tx(13)
    Serial.Str(string("Prop PID Demo"))
    Serial.tx(13)
    Serial.Str(string("***********************"))
    repeat 2
    Serial.tx(13)

    waitcnt(80_000_000 + cnt)


    CP := 90.0
    SP := 50.0



    'NewPosition:=NewPosition + PID(PGAIN,IGAIN,DGAIN,CurrentError,AccumulatedError,LastError)


    repeat



    Serial.str(string("Current Point: "))
    E:=fp.fsub(fp.ffloat(SP),fp.ffloat(CP))
    x:=PID.pid(0.2,0.1,0.2,E,AE,LE,0.1)
    AE:=FP.fadd(E,AE)
    CP:=fp.fadd(CP,X )
    LE:=E
    Serial.str(FS.floattostring(E))
    Serial.tx(13)

    Serial.str(string("Set Point: "))
    Serial.str(FS.floattostring(SP))
    Serial.tx(13)



    waitcnt(20_000_000 + cnt)

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·- Ouch, thats not suppose to be hot!··


    Michael King
    Application Engineer
    R&D
    Digital Technology Group
  • Erik FriesenErik Friesen Posts: 1,071
    edited 2008-08-22 00:11
    What is your original formula?
  • DavidGregDavidGreg Posts: 38
    edited 2008-08-22 02:01
    I don't think you need FloatFull32, because you're only doing basic arithmetic.

    Here is the PID routine I mentioned in another thread. It works, and even if you don't like how its set up, it should be easy enough to change.
  • AribaAriba Posts: 2,687
    edited 2008-08-22 03:05
    SniperKing:

    You don't have to start and stop the FP object every time in the PID methode.
    It's enough to start it one time in the main method (which you have done anyway).

    Andy
  • grasshoppergrasshopper Posts: 438
    edited 2008-08-22 03:10
    Sniper King said...
    Ok my first to the point question.

    VAR

    Long Pterm
    Long Iterm
    Long Dterm

    OBJ

    FP : "Float32Full"

    PUB PID(KP, KI, KD , cErr, aErr , lErr,Time):Value |PIDs
    fp.start
    pterm:= FP.fmul(KP , cErr)


    In this statement the variables (KP and cErr) need to be in "floating point form" to begin with. If this is true then this statement would work correctly. Also note that if Kp is in "floating point form" but cErr is not; then it wont work correctly. So I think you would do this
     
    pterm := FP,Fmul(kp, (FP.Ffloat(cErr)) 
    
    
    



    Is this helping a bit?
  • Sniper KingSniper King Posts: 221
    edited 2008-08-22 17:04
    Ok, i am getting somewhere and it is starting to make some sense!!!!! YAY!

    New question:

    If you have two floating point numbers will this work:

    IF aFloat>BFloat

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·- Ouch, thats not suppose to be hot!··


    Michael King
    Application Engineer
    R&D
    Digital Technology Group
  • Mike GreenMike Green Posts: 23,101
    edited 2008-08-22 18:23
    No

    The comparison is done using integer arithmetic and two floating point values won't compare this way.
    Float32 has a FCmp routine which does comparisons and returns a value which is zero if the two input
    values are equal, a negative sign if the first input value is less than the second.

    You'd write: IF FP.FCmp(aFloat,bFloat) > 0
Sign In or Register to comment.