Shop OBEX P1 Docs P2 Docs Learn Events
float issue — Parallax Forums

float issue

lardomlardom Posts: 1,659
edited 2010-04-30 16:17 in Propeller 1
·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

  • Mike GreenMike Green Posts: 23,101
    edited 2010-04-29 04:05
    Read the documentation for the Floating Point Package in the Object Exchange (obex.parallax.com/objects/202/). The routines in FloatMath provide the same functions as the ones with the same names in the Floating Point Package. You want to use either FTrunc or FRound which both take a floating point number as the argument and return an integer.
  • ElectricAyeElectricAye Posts: 4,561
    edited 2010-04-29 04:12
    See attachment

    smile.gif
  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2010-04-29 15:51
    lardom said...
    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.
    If you are asking how to use float, does that mean you are using float in your program already (it is unclear)? If you aren't, the problem might be that math between two integers always gets rounded down. So 162/4 results in 40, not 41 as expected?
    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)!
  • lardomlardom Posts: 1,659
    edited 2010-04-30 05:14
    @Bobb Fwed. Thanks for the education. What is typecasting? I never used floatmath and I was looking for an example. You said that spin will accept integers...as a float value. That meant I had to recheck my assumptions. I printed out my code, marked it up with a pen and stepped though it with a calculator. Then I made what I thought was an equivalent variable substitution and it appears the bug has been eliminated. Life is good again. Thank you.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2010-04-30 06:37
    Typecasting is where a language (specifically the compiler) tracks a variable as a specific type of variable. For example, in C# there are (who knows) dozens upon dozens of types and you can't mix them. The compiler will prevent you from trying to use a double as a integer or a date as a string, etc. In SPIN the only type is LONG (or BYTEs, depending on how you look at it). Each LONG can be broken into a WORDs or BYTEs, but really all it cares about is LONGs. A LONG can contain any information you want (integer, random bit values, bytes that create a string, etc.). You (as the software engineer) must keep track of what is stored in each LONG.

    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)!
  • lardomlardom Posts: 1,659
    edited 2010-04-30 15:12
    @Bobb Fwed, just yesterday I read a post by Chip Gracey where he talked about the extreme limitations of Spin variables. He said without question he·prefers assembly. (I hope I got that right.) I am a mere mortal·so I had no idea what he meant. The limitations of Spin variables are over the horizon way out of my sight. Let me put things in perspective: I completed my first Pbasic project in '09'. That program has been successfully translated to Spin with the solution to this last bug. This is a thrill for me even though in relative terms I have only learned to spell c a t. Thanks again.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2010-04-30 16:17
    The limitations of SPIN variables are essentially the same in PASM. You have about the same options in PASM as SPIN, just it is much faster and you have more control over signed and unsigned variables.

    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
Sign In or Register to comment.