float issue
lardom
Posts: 1,659
·162/4 = 40.5 Any number with a decimal point causes my app to hang. Please show me how to use floatmath to truncate or round off. The syntax isn't clear to me.
Comments
Key things to keep in mind is that SPIN will accept integers (or any value -- due to lack of typecasting) as a float value; but the value will not be the value you expect if and integer is treated like a float. Be sure than when you are working with floats, all values are floats, this is usually done by adding a ".0" after your constant values. The float libraries give you methods to convert integers to floats and the other way around.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
April, 2008: when I discovered the answers to all my micro-computational-botherations!
Some of my objects:
MCP3X0X ADC Driver - Programmable Schmitt inputs, frequency reading, and more!
Simple Propeller-based Database - Making life easier and more readable for all your EEPROM storage needs.
String Manipulation Library - Don't allow strings to be the bane of the Propeller, bend them to your will!
Fast Inter-Propeller Comm - Fast communication between two propellers (1.37MB/s @100MHz)!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
If you look up the IEEE standard for float numbers you will find that it stores the value in a very different way than a binary integer. So while the compiler will let you supply an integer in float math, the bits are not in the right configuration for the float objects to translate them as the intended value. But you can convert the values before hand.
In integer math, there are no decimals, the values have a very strict limit of −2,147,483,648 to +2,147,483,647 or 0 to +4,294,967,295. With each integer in the range being representable precisely. In float math the range of values is greatly expanded, precision in the math is also increased: decimal values are now allowed. But precision in the values stored is decreased. Instead of 32-bits of precision there is only 24 (the remaining bits are used to track the widened range of values). It is also massive amounts slower to deal in float.
Either way there are trade offs. The rule of thumb I use is to cling to integer math as long as possible. At some point it will be insufficient for representing math equations you are using. At that point only should use use float math.
I know I have simplified how float works, but not grossly. Do some reading so you understand why the bit values of integer math and float math are different.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
April, 2008: when I discovered the answers to all my micro-computational-botherations!
Some of my objects:
MCP3X0X ADC Driver - Programmable Schmitt inputs, frequency reading, and more!
Simple Propeller-based Database - Making life easier and more readable for all your EEPROM storage needs.
String Manipulation Library - Don't allow strings to be the bane of the Propeller, bend them to your will!
Fast Inter-Propeller Comm - Fast communication between two propellers (1.37MB/s @100MHz)!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Well good luck with the learning. It seems like not too long ago I was asking all these same questions. I have a fair amount of background in other languages, but new to programming or new to a language there is always a great deal of pain and tedium.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
April, 2008: when I discovered the answers to all my micro-computational-botherations!
Some of my objects:
MCP3X0X ADC Driver - Programmable Schmitt inputs, frequency reading, and more!
Simple Propeller-based Database - Making life easier and more readable for all your EEPROM storage needs.
String Manipulation Library - Don't allow strings to be the bane of the Propeller, bend them to your will!
Fast Inter-Propeller Comm - Fast communication between two propellers (1.37MB/s @100MHz)!
Post Edited (Bobb Fwed) : 4/30/2010 4:33:58 PM GMT