Floating point vs non-floating point
grasshopper
Posts: 438
I am writing a program that uses some mathematical calculations and figured I would ask as the subject line states.
I think that it is faster to use non floating point math to make my calculations. This would have to be accomplished like the example:
I would employ this type of "keeping my decimal place" throughout my code. Again is this faster or just a waste of time?
I think that it is faster to use non floating point math to make my calculations. This would have to be accomplished like the example:
say I need a number 10.99 ... ...I multiply 10.99 by 100 = 1099 This in effect makes 10.99 without the "."
I would employ this type of "keeping my decimal place" throughout my code. Again is this faster or just a waste of time?
Comments
Bob Sweeney
Jonathan
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
www.madlabs.info - Home of the Hydrogen Fuel Cell Robot
Leon
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Amateur radio callsign: G1HSM
Suzuki SV1000S motorcycle
In the prop it is even worse, because the FP is done in software (where your PC has a dedicated FP processor).
Yet, the huge range, the don't having to worry about the place of the decimal point and the availability of math functions like raising to the power, square roots etc. make me using floating point wherever I don't need the added speed or the extra free memory.
And in many cases, the speed is not so relevant anyway or you can easily optimize your code to make it irrelevant.
Just don't put FP math inside a loop needing tight timimg...
Another thing about it is the level of accuracy. You can lose track of your significant digits when doing it with integers. It helps to lay the whole calculation out in an excel spreadsheet (if it's a long formula), so you can simulate the range of your final results. For instance, if you only use 8 bits from an ADC, you only have 0-255. You can use this as your input and see how the output will look with each sample point after you have gone through your formula.
With integer, I would work with numbers less than 2^16 (65536?), which will handle (255*100=25500). This assumes you need to keep your values within a size to work with assembly multiplication and divide routines (2^16).
If you work with spin, you might be able to work with larger numbers.
-Parsko
How many clock cycles are added just to do Floating point? Better yet how much speed do you loose using Floating point.
I am currently using Float32 so in effect its running in a cog. Will this help the speed issue?
Floating point takes even more time since it involves splitting the numeric representation into 2 parts. For instance your number 10.99 would be stored as .1099 (mantissa) and 2 (exponent), that is .1099 x 10^2. (I am using normalized base 10 rather than binary for clarity). Any floating point operation on two numbers involves separate operations on both parts of the number.
See http://www.psc.edu/general/software/packages/ieee/ieee.php for a more detailed explanation or google "floating point" and have some fun.
Read the documentation. There's a lot of information there on floating point operation speed.
Float32 is not designed to function in parallel with other cogs. When your program "asks" the cog to do the floating point operations, it waits until the cog is done before continuing. The use of the extra cog is just to get the speed advantage of the native Prop instruction set.
Andy