Decimal point math in Spin
garylake
Posts: 41
Simple question and I hope a simple answer.
In Spin I would like to use decimal point numbers for division.
Like 1000/1.5
or 1000/0.3
In Spin I would like to use decimal point numbers for division.
Like 1000/1.5
or 1000/0.3
Comments
John Abshier
Here's some code I've been worrking on:
Notice that 1000/1.5 is the same as 10000/15
So you see that if you scale up your numbers by 10 or 100 you can easily handle one or two decimal places respectively.
This called fixed point arithmetic and is often sufficient.
Also be carefull of overflow when you do this.
@anybody
Does anyone have a copy of this:F32 - Concise floating point code for the Propeller.
Github gave me a 404 on this.
Thanks
https://github.com/parallaxinc/propeller/blob/master/libraries/community/p1/All/F32 - Concise floating point code for the Propeller.
*
and**
easily allow 32bit fixed point math. That's good enough even for some fun some wouldn't expect.And remember, as Heater's boss used to say "if you think you need floats, you're not doing it right." (Or something like that.)
Heater used to bring this up when someone asked about floating point math and I often remind myself of this advice. I actively avoid using floats in my Propeller projects.
Here's one example of why.
What's next highest number after one billion?
With integer math the answer is 1,000,000,001.
With 32-bit floating point math, the answer is 1,000,000,064.
There may be times when floats are a better option than using integers, but most (almost all) the times in the past when I initially thought floats were the better option, it turned out I was wrong and Heater's boss was right. You're almost always better off using integer math rather than floating point math. It turns out you don't even need floats for trig equations.
I haven't been able to eliminate floats completely from my programs but I always feel like I've failed a bit as a programmer when I need to use them.
Floats have two major advantages:
- Simplifies the writing and readability of source code.
- Has a natural advantage in retaining full utilisation of the mantissa across multiplies and divides.
Floats have two major disadvantages:
- Sacrifices mantissa size to have the exponent.
- Slower and has overheads when not in hardware.
I like to use both Integers and Floats - Even with a fast FPU at hand. Integers serve as precise adders that can be mapped to real world I/O without any fuzziness, so often that's exactly how I use them. Floats are better when doing the intermediate transforming maths.
Except in Spin, here all floating-point computations have to be done via function calls, rather than infix expressions.
-Phil
@""Duane Degn"
"If you can't solve the problem with integers, you don't understand the problem"
I hate floats
Craig