Float32Full tutorial
Sniper King
Posts: 221
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
' We need to make·A into a Floating point number.
Ok now we are going to make A into a Floating point value.
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...
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
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
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.
··· 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.···
·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
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!
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...
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...
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
'IF' statements are not normal in the Float32Full object so you really need to pay attention hear and learn this before you move on.
·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?
·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
FP.ffloat(5) is correct and will produce the floating point equivalent of the integer value 5
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·- Ouch, thats not suppose to be hot!··
Michael King
Application Engineer
R&D
Digital Technology Group
you also can write:
The ffloat() function do you only need to cast integer variables.
Andy
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!
·································
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·- 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
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))
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))