Author Options:

How much does float slow down arduino calculations, how do I avoid float values? Answered

I am writing a sketch with about 3 float numbers that go through some simple calculations such as divide and multiply. The calculations are very simple and only about 3 or four lines of code. I can't have this slowing down the reading of my interrupt pin.

Can I store the numbers by multiplying until I have a whole number and store them as a int. Then divide them back out in the calculation?

Thanks any help would be appreciated, David.



Best Answer 5 years ago

Use only integers.

If your input number is one decimal point enter it as ten times = integer

Do the same in your formula

Exit out with the understanding that the value has a decimal point
moved one digit left.



Depending on what the procedure is, you can often get away with some clever arithmetic tricks to avoid the floats.

Using integers and operations like bitshift to divide by two or multiply by two, with some acceptable loss of precision, you can plough through a lot of math with reasonable approximations.

Taking 500 instruction cycles at 16mhz is arguably nothing in time world unless you are doing that calculation thousands of times per second. Also, if using interrupts, using an ISR appropriately with only a flagging procedure (rather than executing code in the interrupt) you should be fine.

Excellent reference when a killer bot is working your last shield :)

Don't EVER use FP in an interrupt routine.
Iceng has the trick of it. Use FIXED points, and look after the DP in your head until the last moment. Iceng is old enough to have used Slide rules. All of the oldschool guys who could, effectively kept the DP in their memory during the calc.

Thank you for your most accurate comment Steveastrouk,
Although it  has the sound of an epithet ;-)

He he. I am "Just" old enough to have used a slide-rule in anger.