For the computation involving multiple types of variables, such as integer and double, is the following approach the correct way in terms of not causing any hidden error or information lost?
int a = 2;
double b = 3.0;
double c;
c = (double)(a+b);
c = (double)(a/b);
Another scenario is that:
int x =2;
int y = 3;
double z;
z=x/y will not give me correct value like 0.666666666, how to handle this kind of scenario?
When you perform an operation between a float and a integer, the result is a float. In general, the result will be the larger data type. What you are doing in the first place is correct if you want decimal precision. You don’t need the cast, even.
In the second case, you can do several things
or