When I do this:
float add=0;
int count[] = {3, 2, 1, 3, 1, 2, 3, 3, 1, 2, 1}
for (int i=0; i<11; i++)
add += 1 / ((float)count+1);
The output is:
4.00000000000000000000
But when I do this:
float add=0;
int count[] = {3, 2, 1, 3, 1, 2, 3, 3, 1, 2, 1}
for (int i=0; i<11; i++)
add += 1.0 / ((float)count+1);
The output is:
3.99999976158142089844
When I need to turn an int into a float, I either add (float)
in front or let it do arithmetic with a decimal such as a / 1.0
. Is there any difference?
Edit: Adding the desired behaviour.
The reason is that afterwards, I need a result that adds add
to another int variable for an int output. However, when I do it the second way, the int uses 3 instead of 4 so I would like to know what is the difference between the first and second code.