Shop OBEX P1 Docs P2 Docs Learn Events
Float32Full tutorial — Parallax Forums

Float32Full tutorial

Sniper KingSniper King Posts: 221
edited 2008-11-08 17:05 in Propeller 1
I am going to write this darn thing if it kills me!· Brain fry is setting in but things are starting to make some sense here.
I·would appreciate any input into this.· Also, if you see me making an error, please fix me!· Thank you all for your help in advance.


Lesson #1· Variables
[color=#ff0000]A:=100[/color]



' We need to make·A into a Floating point number.

[color=red]Debug.Dec(A)[/color]
[color=red]100[/color]



Ok now we are going to make A into a Floating point value.

[color=red]A:=FP.ffloat(A)[/color]
[color=red]Debug.Dec(A)[/color]
[color=red].311204034[/color]
 


Whoa, what is that?
Ok, Spin doesn't understand Floating point numbers like we do.· So, Spin will convert this to a long number for us to work with.· Now, as long as we continue using the Float32Full object, we can work with this value.· So even though we have some weird decimal at first glance, it will be correct if we use the Float32Full object.

How can we display the proper number?
Using the FloatString Object!· We will call the object "FS" in our code so...
[color=red]Debug.str(FS.floattostring(A))[/color]
[color=red]100.0[/color]


This changed the floating point value of A to a string so we can display it.· 'A' is still a floating point value but has been displayed as a string because spin won't let you display it as a number.

So as you can see, 'A' is a floating point number now.· We can work with this to do some really advanced math.· As long as A remains a floating value we can do floating point math equasions with it.

Next lesson: A little Math

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


Michael King
Application Engineer
R&D
Digital Technology Group

Post Edited (Sniper King) : 8/22/2008 8:10:24 PM GMT

Comments

  • Sniper KingSniper King Posts: 221
    edited 2008-08-22 19:55
    Lesson 2: A little Math
    We know how to put a simple spin variable into a floating point variable.· One thing I forgot to mention, MAKE SURE THAT YOUR ASSIGNED VARIABLES FOR FLOATING POINT MATH ARE LONGS!
    Ok, lets do some math.· Nothing to extreme here.· Lets add to values together.
    Long A
    Long B
    Long C
     
    A:=100  ' A is now = 100 as an integer.
    B:=50   ' B is now = 50 as an integer.
     
    A:=FP.ffloat(A)  ' A is now a floating point value = 100
    B:=FP.ffloat(B)  ' B is now a floating point value = 50
     
    ' New function here!
     
    C:=FP.fadd(A,B)  OMG what is this?
     
     
    

    ··· The last line of code is our addition function.· While it looks unfamiliar, it is pretty straight forward here.

    ··· C:=FP.fadd(A,B)· is C=A+B on paper.···

    Long A
    Long B
    Long C
     
    A:=100  ' A is now = 100 as an integer.
    B:=50   ' B is now = 50 as an integer.
     
    A:=FP.ffloat(A)  ' A is now a floating point value = 100
    B:=FP.ffloat(B)  ' B is now a floating point value = 50
     
    ' New function here!
     
    C:=FP.fadd(A,B)  OMG what is this?
    [color=blue]Debug.str(FS.floattostring(C))[/color]
    150.0
    
    
    


    ·That wasn't too hard.· This is not as hard as it first seams but you have to really understand to get this.
    My biggest·programming problem with this was getting everything into the correct format before doing any math on it.


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


    Michael King
    Application Engineer
    R&D
    Digital Technology Group
  • TreeLabTreeLab Posts: 138
    edited 2008-08-22 20:00
    Hi;

    in the code snip

    FP.ffloat(A)
    Debug.Dec(A)
    .311204034

    I think that FFloat is a function, and you must store the result back in A before the call to dec(A).

    There is a reasonable document (Propeller Floating Point.pdf) 'floating' about, perhaps packaged with the routines in ObEx. My copy is dated May 17/2006, and is attached.

    Cheers!
  • Sniper KingSniper King Posts: 221
    edited 2008-08-22 20:07
    Some little things that will kill you to death if you don't understand.



    A:=FP.fadd(5,5)
    

    What is wrong with the above statement?
    The number '5' is not represented correctly. You absolutely have to format this number as a floating point number so...

    A:=FP.fadd(5.0,5.0)  'This is correct!
    


    The above example is correct.· you can do this during math operations.

    Nesting your Math

    you can probably see already that some of your massive equasions are going to be HUGE using Float32Full.· So what! Now you can do that crazy hard math on you propeller chip and not some co-processor.

    Some little things you can do

    If you have a integer value that you are doing simple math on alot in your subroutines, and you need to do some float math using that value,·you may not want to change it to a float value·and back again all the time.· So, you can just reference it in you math statement...

    C:=FP.fadd(fp.ffloat(B),5.0)
    

    as you can see, 'B' stays an integer but we were able to do a little math on it without·Changing 'B'



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


    Michael King
    Application Engineer
    R&D
    Digital Technology Group

    Post Edited (Sniper King) : 8/22/2008 8:34:25 PM GMT
  • Sniper KingSniper King Posts: 221
    edited 2008-08-22 20:20
    Lesson 3:· IF Statements

    'IF' statements are not normal in the Float32Full object so you really need to pay attention hear and learn this before you move on.

    A:=FP.ffloat(100.0)
    B:=FP.ffloat(200.0)
     
    if A>>B
      debug.str(FS.floattostring(A))
    else
      debug.str(FS.floattostring(B)) 
     
     
    

    ·Will the above example Work?
    ·NOPE.· We are not really comparing two floating point values in this example, we are comparing two LONG values.· This will not give you the correct answer.

    how can we do this?

    A:=FP.ffloat(200.0)
    B:=FP.ffloat(100.0)
     
    if FP.fcmp(A,B)>0
      debug.str(FS.floattostring(A))
    else
      debug.str(FS.floattostring(B)) 
     
    
    

    ·this does work.

    WHY?

    OK,· this is the breakdown.

    if A>B then FP.fcmp(A,B)= a positive number
    if A<B then FP.fcmp(A,B)= a·negative number
    If A=B then FP.fcmp(A,B)= 0

    It doesn't matter what the positive number is here.· if you are greater than 0 then A>B and so on.· So use this instead of your standard IF statements.




    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·- 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 20:26
    Re: 2:07PM note ...

    FP.ffloat(5) is correct and will produce the floating point equivalent of the integer value 5
  • Sniper KingSniper King Posts: 221
    edited 2008-08-22 20:32
    Thanks Mike!

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


    Michael King
    Application Engineer
    R&D
    Digital Technology Group
  • AribaAriba Posts: 2,687
    edited 2008-08-23 01:02
    Instead of writing:
    A:=100  ' A is now = 100 as an integer.
    B:=50   ' B is now = 50 as an integer.
     
    A:=FP.ffloat(A)  ' A is now a floating point value = 100
    B:=FP.ffloat(B)  ' B is now a floating point value = 50
    
    



    you also can write:
    A:=100.0  ' A is now = 100 as a floating point value.
    B:=50.0   ' B is now = 50 as a floating point value.
    
    


    The ffloat() function do you only need to cast integer variables.

    Andy
  • Sniper KingSniper King Posts: 221
    edited 2008-08-23 21:13
    Thank you Andy.· That helps!· Pretty soon as all these little jewels come together,· i will rewrite the entire thread and just put all the useful data in it for newbies to really jump in head first and be able to get the math side of things down quickly.·



    This also really helps me.· The more I use it, the easier it is and writing this has made a big difference in many of my applications already!

    ································· yeah.gif


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


    Michael King
    Application Engineer
    R&D
    Digital Technology Group

    Post Edited (Sniper King) : 8/23/2008 9:19:43 PM GMT
  • fingeraefingerae Posts: 15
    edited 2008-11-08 04:19
    Please help!· I just want to do some simple floating point math, but I can't seem to do division or multiplication.· The following is the simplest example I can think of.· My goal is to divide two numbers to get a signed percentage, multiply that signed percentage by 500, and add it to 1500 for servo control.· But, the following code does not seem to perform division.· What am I doing wrong?



    CON

    · _clkmode = xtal1 + pll16x·· ' The code works at any frequency, but the serial line requires the crystal
    · _xinfreq = 5_000_000
    · RX_PIN··················· = 31········ 'Serial receive pin
    · TX_PIN··················· = 30········ 'Serial transmit pin
    · BAUD_RATE················ = 19200····· 'Serial baud rate

    OBJ
    · SER········ : "FullDuplexSerial"··· ' Object from the standard Propeller Tool library
    · MATH······· : "Float32Full"········ ' Decimal math
    · PRINTFLOAT· : "FloatString"


    var

    · long temp1, temp2, temp3

    PUB Main

    · MATH.start
    · SER.start(RX_PIN, TX_PIN, 0, BAUD_RATE)·············· 'Connect to serial line to display data
    ·
    · temp1 := 2
    · temp2 := 15

    · temp3:=MATH.FDiv(MATH.FFloat(temp1),MATH.FFloat(temp2))

    · repeat
    ··· SER.str(string("Value = "))· 'Display string
    ··· SER.str(PRINTFLOAT.FloatToString(temp3))
    ··· SER.str(string($0D))
    ··· SER.str(string("Value = "))· 'Display string
    ··· SER.dec(temp3)
    ··· SER.str(string($0D))
    ··· SER.str(string("Value = "))· 'Display string
    ··· SER.str(temp3)
    ··· SER.str(string($0D))
  • Mike GreenMike Green Posts: 23,101
    edited 2008-11-08 05:03
    What does it do? The division statement looks correct.
  • fingeraefingerae Posts: 15
    edited 2008-11-08 16:43
    The output on the serial terminal looks like it overflows. Depending on what is used as the print to screen statement, I get 65535, or 0. FSdd and FSub seem to work, but FDiv and FMul do not.
  • fingeraefingerae Posts: 15
    edited 2008-11-08 17:05
    I started from scratch and rewrote the code in what seems the same to me, but now it works correctly.· The only difference I see is the lack of the compound statement.· Any thoughts?

    CON

    · _clkmode = xtal1 + pll16x·· ' The code works at any frequency, but the serial line requires the crystal
    · _xinfreq = 5_000_000
    · RX_PIN··················· = 31········ 'Serial receive pin
    · TX_PIN··················· = 30········ 'Serial transmit pin
    · BAUD_RATE················ = 19200····· 'Serial baud rate

    OBJ
    · SER········ : "FullDuplexSerial"··· ' Object from the standard Propeller Tool library
    · MATH······· : "Float32Full"········ ' Decimal math
    · PRINTFLOAT· : "FloatString"


    var

    · long temp1, temp2, temp3, temp4

    PUB Main

    · MATH.start
    · SER.start(RX_PIN, TX_PIN, 0, BAUD_RATE)····· 'Connect to serial line to display data
    ·
    · temp1 := MATH.FFloat(2)
    · temp2 := 8.1
    · temp3:=MATH.FDiv(temp1,temp2)
    · temp4:=MATH.FMul(temp1,temp3)

    · repeat
    ··· SER.str(string("Value = "))· 'Display string
    ··· SER.str(PRINTFLOAT.FloatToString(temp4))
    ··· SER.str(string($0D))
    ··· SER.str(string($0D))
Sign In or Register to comment.