Here is a small but common issue that I found on CPlusPlus forum .
Let's say we have a function like this
double f (int a, int b)
{
double result = (a*b) / 2 ;
return result ;
}
or something like this , the exact function isn't relevant . So the problem was that the function returned an int value and not a double .
The root of the problem is the following :
because all the variables are int the operator/ will take the argument (a*b) which is an integer so-to-say so it will store an integer in result and then convert it to double (e.g. from 2 to 2.0 , which is kind of useless) .
So in order to fix this you can simply cast the variables to double like this:
double f (int a, int b)
{
double result = ( (double) a* (double) b) / 2 ;
return result ;
}
No comments:
Post a Comment