Go Down

Topic: How to avoid floating point math (Read 2 times) previous topic - next topic



TimeAlarms doesn't use interrupts. So Repeats is not an ISR (Interrupt Service Routine).

Quote from http://arduino.cc/forum/index.php/topic,37693.0.html

Q: Are there any restrictions on the code in a task handler function?
A: No. The scheduler does not use interrupts so your task handling function is no different from other functions you create in your sketch.


Code: [Select]
Calc = ((float)Count* ((float)720 / (float)k_factor));

just a tip: division is much slower than multiply so this code can be written as

Code: [Select]
Calc = 720.0 * Count * inverse_k_factor;
As the first value is float the whole expression is float, no need to cast. Yes you need to calculate inverse_k_factor somewhere in advance.

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)


Your intuition is correct. Floating point math is typically implemented via calls. When floating math is used in the isr and elsewhere in the main loop, you can get into re-entrancy -> big issues.

Sometimes 32-bit multificaton is done via calls so same issue.

You can simplify the math or to use a flag.

Nick Gammon

I don't see the relevance of this. As shown, the only interrupt which is called at all frequently (100 times a second) doesn't do floating maths.


Nov 30, 2012, 03:05 pm Last Edit: Nov 30, 2012, 03:10 pm by PeterH Reason: 1
Isn't the floating point code reentrant, in any case? I would have assumed it was, and I can't think of any good reason for it NOT to be.

I suggest you post all of your code - or attach it, if it's too big to post. Various snippets have been posted, but the only complete copy I can see is in the first post, which is formatted horribly and probably out of date.
I only provide help via the forum - please do not contact me for private consultancy.

Go Up