I am all new to Arduino and C++ and trying to learn/understand it. It was 25+ years ago I did some programming in a non high level scripting language. Only playing with Python for the last 10 years.
To make the source code more readable and to follow what seems to be the standard way if writing code in this project a decided to go with #define on the user adjustable pre compiling "variables". But I havetrouble with the floats.
As I understand it, it must because the pre-compiler doesn't understand that R1 and R2 is not an integer. But by adding the decimal point it does.
Now I wonder is there any way to tell the compiler that R1 and R2 is a float without the ending decimal point?
I have tried static_cast((1.1/1023)*((R1+R2)/R2)) which make no difference.
The reason for searching for a different way to get this working is that I can see how easy it will be to forget to add the decimal point.
when you using #define keyword, you tell the compiler that your "thing" is a macro. What that means is that whenever the compiler see your "thing", it will replace it with the value. Thus your "thing" is not a variable, so it does not have a type.
arduino_new:
when you using #define keyword, you tell the compiler that your "thing" is a macro. What that means is that whenever the compiler see your "thing", it will replace it with the value. Thus your "thing" is not a variable, so it does not have a type.
Yes I do understand that.
Therefore I wonder if there is any way around to make the formula for batFact to calculate the correct answer without the decimal point in the two #define
you define batFact as a const but then attempt to assign a value to it - it would give a error *assignment of read-only variable 'batFact'*
I have updated the question. I am not trying to set the batFact variable. That line in the question was to show the result of the calculation. I see that it was confusing.
Therefore I wonder if there is any way around to make the formula for batFact to calculate the correct answer without the decimal point in the two #define
Yes you could. But you must explicitly cast them to a float like this:
Serial.print(batFact, 6); //prints: 0.003317 float is binary32 formatted data which gives accuracy to 6-7 digits.
}
void loop()
{
}
Thanks! As I wrote I am trying to learn/understand, not just do.
So I have to follow up with two questions:
Why is that working?
1.1/1023 is calculated correctly because there is a float in the operation, right?
Why is not ((1.1/1023)((R+R2)/R2))working, when ((1.1/1023)(((float)R1+R2)/R2)) is?
Is it because of the parentheses? And that it calculates the (R1+R2)/R2 part first/separate and there is no float in that calculation?
And have I understood it correctly when I think that in the compiled code there will be only one value stored, and that is the sum of my formula? Which is calculated when compiled, and not by the Arduino at run time.
strixx:
Thanks! As I wrote I am trying to learn/understand, not just do.
So I have to follow up with two questions:
Why is that working?
1.1/1023 is calculated correctly because there is a float in the operation, right?
Why is not ((1.1/1023)((R+R2)/R2))working, when ((1.1/1023)(((float)R1+R2)/R2)) is?
Is it because of the parentheses? And that it calculates the (R1+R2)/R2 part first/separate and there is no float in that calculation?
And have I understood it correctly when I think that in the compiled code there will be only one value stored, and that is the sum of my formula? Which is calculated when compiled, and not by the Arduino at run time.
strixx:
1.1/1023 is calculated correctly because there is a float in the operation, right?
Why is not ((1.1/1023)((R+R2)/R2))working, when ((1.1/1023)(((float)R1+R2)/R2)) is?
Is it because of the parentheses? And that it calculates the (R1+R2)/R2 part first/separate and there is no float in that calculation?
when you evaluate an expression you need to consider operator Precedence and Associativity, see
in the expression (R1+R2)/R2 the (R1+R2) is evaluated first (due to the ()) then the division
in the expression R1+R1/R2 the R1/R2 has the highest Precedence so it is evaluated first then the addition