Shop OBEX P1 Docs P2 Docs Learn Events
Floating Point Numbers in Spin Language — Parallax Forums

Floating Point Numbers in Spin Language

B.ASHISHB.ASHISH Posts: 28
edited 2012-09-16 07:03 in General Discussion
Please tell us how to work with floating point numbers in Spin language.We actually want to pass floating point numbers as arguments in Cos functions.
Thank You in advance.

Comments

  • Heater.Heater. Posts: 21,230
    edited 2012-09-16 02:30
    Look in the Parallax object exchange (OBEX) for the floating point math object or the somewhat smaller faster F32 object.
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2012-09-16 03:53
    The Propeller has a trig table for look up of trig functions. It also has log tables.

    But the simple realities that computers are base 2 machines and tend to be interger friendly is likely to guide you toward avoiding floating point calculations as much as possible and to try to do as much with base 2 and interger math. At the end of this, results can be converted to fixed point or floating point output.

    This is not just about the Propeller. It is about any computer is better used by limiting floating point. But often beginners think that floating point is the most direct approach to getting the results they want. I suppose it is because the common calculator relies heavily on floating point display.
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2012-09-16 03:59
    BTW, the new GCC has ANSI C with its floating point library if that is a more comforable approach to programing the Propeller.
  • Mike GreenMike Green Posts: 23,101
    edited 2012-09-16 07:03
    Floating point in Spin is supported to only a very limited extent. The compiler will accept floating point constants and do constant calculations on these, but it treats them as special 32-bit integer values and there are no floating point variables. The arithmetic operators (+, -, *, /, etc.) will not do floating point operations on these values, but will do integer operations on their internal representation (IEEE). If you want to do something like "A := (B * C) - 5.0", you have to do "A := Flt.FSub( Flt.FMul( B, C ) , 5.0 )" where Flt is the name of the floating point object. Look at the floating point library object in the ObEx (or F32) for more details.
Sign In or Register to comment.