Go Down

Topic: Floating Point Math? (Read 6 times) previous topic - next topic


..there are some Cortex M3 benchmarks in http://arduino.cc/forum/index.php/topic,121568.75.html
try to run it on an 8bitter, compare clock to clock ad you will see.. as a rule 64bit fp calcs are 2x slower than 32bit fp calcs in general..


Fixed point may be what you really need.
Floating point is designed to handle both very large numbers and very small numbers where precision is not that important.
If what you need is hundredths (or thousandths) of a degree for GPS readings, then use integers and write the code understanding that the values are hundredths of a degree (or volt or foot or temperature degree). This makes any math hugely faster. Perhaps it's only necessary to think of the value in full units when it is displayed for human viewing.


Fixed point is very handy for accumulating small bits to gain resolution.
An example using 16 bits, where the top 8 bits (H) are the integer component and the bottom 8 bits (L) are the fractional component.
Code: [Select]


It's quite easy if you define a new type using a union/struct...

Code: [Select]
typedef  union
      byte low;
      byte hi;
  };  unsigned  all;
} fixedPoint;

fixedPoint Accumulator;

Adding to Accumulator.all rolls from low to hi.
Access to the fraction component is by Accumulator.low
Access to the integer component is by Accumulator.hi



Looking through the data sheet I see the Due has instructions for:-
Single cycle 32 bit Multiply
Multiply and subtract
Multiply and add
Signed Divide
Signed Multiply (64 bit result)
Signed Multiply with accumulate (64 bit result)
Unsigned Divide
Unsigned Multiply with accumulate  (64 bit result)
Unsigned Multiply  (64 bit result)

So if the compiler uses those it will greatly speed things up.



Code: [Select]
long t = millis();
float a = 1.2;
float b = -7.8;
float c;
float d = 0;
for(long i = 0; i < 1000000; i++)
    c = a + b;
    c = c - a;
    c = a*c;
    c = c/a;
    d += c;
t = millis()-t;
Serial.print("Time: ");

runs in 10.1 seconds on a Melzi (RepRap Arduino controller with an ATmega1284P clocked at 16 Mhz), and 1.29 seconds on a Due.

Adrian Bowyer
RepRapPro Ltd

Go Up