I just found a bug in my code which can be simplified to:
float c;
int a,b;
a=5;b=6;
c=(a+b)/2;
In my debugging c has the value 5, i.e. integer division was executed.
This leads me to the question: What is a good practice when using numbers that are not stored in a variable, i.e. constant. In my case, the denominator 2.
When I'm implementing mathematical formulas that carry various constant like this, should I always put .0 at the end to make sure its internally used as a double? Does it speed up the operating time of my program? How much extra work has to be done if a constant has to be converted from int to double?
formulas like
z = 180 * 3.1415 * x + 5*y^2 - (10*x-y)/(y+x);
where x,y,z are doubles.
So my question is,
Is it "clean code" to write all factors with a .0 at the end ? It does decrease readability of the code quite alot, especially if the formulas are very long. But with this convention, I would prevent errors like the one discribed at the beginning.