1747Views2Replies

# Can i use brackets in a arduino calculation?

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.

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.

ex:

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?

Select as Best AnswerUndo Best Answer

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

Select as Best AnswerUndo Best Answer