Why does maths fail on assigning values to a var

Hi guys, I have a problem here... Its about doing maths while assigning values to a variable.

An example here

case 1:

float a0Value=(analogRead(0));
Serial.println(a0Value);

outputs: 1023.00 (always)

case 2:
float a0Value=(analogRead(0)*(5/1023));
Serial.println(a0Value);

outputs: 0.00 (always) but it should return 5.00

???????

Another similar things happened when I use the + operator to join a String and a Value returned from a function.... it doesnt do the concatenation, fro example.

String connectedMessage="Connected: "+(client.connect(server,port)==1)
Serial.println(connectedMessage);

outputs: Connected: (without any information about the return value from the connect function)

5/1023 is 0

please read the test cases.

5/1023 is 0.004..... and 1023*5/1023 is 5 I am sure that the analogRead returns 1023....

nope. 5/1023 is 0

You must be careful to ensure that the Arduino is doing the maths they way you want. The compiler defaults to assuming all values are ints and in that context 5 / 1023 = 0

You can get the compiler to use floating point maths by giving it a floating point number - for example 5.0 / 1023, I don't know if it is sufficient to tell the compiler that the output value will be a float - I hardly ever use floating point maths because it is slow and not as precise.

HOWEVER it seems to me that the basic problem here is that your analogRead() is always returning 1023 even though you expect it to return different values. Have you checked that?

...R

Try:

float a0Value=((analogRead(0)*5)/1023);

See the difference? In integer maths, 1023*(5/1023) is not the same result as (1023*5)/1023

C is intended to be more efficient than many other languages like BASIC, Python of whatever you are used to. The penalty for this efficiency is that as a programmer, you need to be much more careful and aware of the detail of what you are writing.

A prime example is integer maths vs. floating maths. Integer maths is much more efficient, so C compilers use that unless you as a programmer force it to use floating.

Declaring a variable as float does not force the calculation to be done as floating maths. The C compiler will still do the maths as integer maths and convert to float at the end, because it's more efficient.

"5/1023" is done as integer maths because 5 and 1023 are integers. Hence the result is 0. However, in "5.0/1023", the 5.0 is a float, forcing the compiler to perform the maths as floating maths.

Thank you very much Robin2 and PaulRB. Understood.

But....

What about when adding this values to a String, the String doesnt have the function return value, I have achieved that by assigning the return value and then concatening this in another String variable and then showing it. 3 steps instead of one. I think its the arduino language limitations but I want to understand why and how should I code this kind of things to avoid losing tons of time with this kind of things..

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

...R

To answer your question specifically, you are adding, or perhaps concatenating, a String to a Boolean (the == operator yields a Boolean result). In C, Booleans are represented as 0=false and anything else=true. I think == yields true as 1. So you are adding/concatenating zero or 1 to a String. I suspect this will be interpreted as ASCII code 0 or 1. ASCII code 1 is not a printable character, and 0 terminates a string. So you see no difference. It sounds stupid compared to how other "high-level" languages would behave, I know. But C was designed to allow programmers to use clever tricks to achieve efficient code, assuming they are competent, rather than assuming they are a little nieve and trying to protect them from their own mistakes. Another example is array indexes. Many high level languages will check that an array index is within the allowed bounds, before reading or writing to that array index. But this is a step which is uneccessary and inefficient if the programmer has already ensured that the index will not ever be of of bounds. C assumes the programmer really knows their stuff.

You could argue that this makes C unsuitable for Arduino, because many or most Arduino users will be beginners at coding. In my opinion, you would not be wrong. But C's efficiency made it the best choice for less powerful 8-bit processors with limited speed, ram and flash memory. Now that more powerful 32-bit processors are falling to the same price levels, they can afford to support less efficient but more programmer-friendly languages like Python, and that is exactly what is happening.