Welcome to the Parallax Discussion Forums, sign-up to participate.

# Floating Point Numbers in Spin Language

Posts: 28
edited 2012-09-16 - 07:03:03
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.

• Posts: 21,213
edited 2012-09-16 - 02:30:01
Look in the Parallax object exchange (OBEX) for the floating point math object or the somewhat smaller faster F32 object.
• Posts: 12,537
edited 2012-09-16 - 03:53:21
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.
Hwang Xian Shen, Puddleby-on-the-Marsh.
All things considered, I can live and thrive without Microsoft products. LINUX is just fine.
• Posts: 12,537
edited 2012-09-16 - 03:59:08
BTW, the new GCC has ANSI C with its floating point library if that is a more comforable approach to programing the Propeller.
Hwang Xian Shen, Puddleby-on-the-Marsh.
All things considered, I can live and thrive without Microsoft products. LINUX is just fine.
• Posts: 22,924
edited 2012-09-16 - 07:03: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.