Author Options:

Can i use brackets in a arduino calculation? Answered

Hi, i am trying to write a sketch avoiding using float variables. My solution is to calculate everything in one step. Can i use brackets in my calculation so that everything is done in the right order?

Thanks, David.


The forums are retiring in 2021 and are now closed for new topics and comments.

Best Answer 7 years ago

In relation to your other question, you can do a huge calculation all in one line of code or in ten different steps on many lines of code - that will not change how the compiler converts it to machine instructions. Yes you can use brackets, no it won't change the outcome.

finalValue = sqrt((initialValue+3.2) * 3);

is the same as:
initialValue += 3.2;
initialValue = initialValue * 3;
finalValue = sqrt(initialValue);

...same result, the compiler will break down the complex equation into steps like this...there might be some performance hit one way or the other, but it will be miniscule, and will not affect whether a floating point operation takes place.

In order to eliminate the float math we need to know what the calculation is. What is the input, and what is the desired output?


7 years ago

Do you mean parentheses? Yes. Square brackets are not valid.