When I try to divide 1/60 or 1/(60*60) it gives 0. Even in debugger window. I am a bit confused what it could be, because 2/3 or 2.5/6 give results.
My code:
int main()
{
double k1 = 1/60;
cout << k1
<< endl;
double k2 = 1/(60*60);
cout << k2
<< endl;
return 0;
}
I appreciate your help.
Since both your operands are integers, the compiler performs an integer division (that doesn't calculate the decimal part). If at least one of the operands is a floating point type (as in your other examples) the other one gets promoted and a floating point division is performed.
The fix
Make at least one of the operands of a floating point type (double
or float
); you can do this e.g.:
double
literal2 (60
is integer, 60.0
or even 60.
is double
, 60.f
is float
)double(60)
, (double)60
).Personally I prefer using directly double
literals - not that the cast has any performance penalty on halfway decent compilers, but it feels "wrong" and verbose in respect to just using a literal of the correct type. (Obviously when both the operands are variables and not literals you have to use the cast)
Common objections
"but I'm assigning it to a double
!"
Many newbies are confused by this fact, since they think that assigning1 the result to a double
should be some kind of hint to the compiler. In fact, it's not.
The calculations/promotions done into the expression are completely independent from the type of the destination, which is just the last step. The subexpressions are evaluated for what they are, without regard to how the result will be used, so all the type promotions/operations depend only from the type of the operands.
why would one want integer division?
Several languages automatically perform floating point division even when the arguments are both integers (e.g. VB6, IIRC), since it feels more intuitive for novices. That's not like that in C/C++: the division is integral when the arguments are integer, since in many cases you just don't care about the decimals, and/or it's preferable for performance reasons not to use the FPU (the background philosophy in C and C++ is "you don't pay for what you don't use").
Obviously the problem could have been addressed using a separate operator for integral division (VB, again, uses \
), but IMHO we have enough operators in C++ as it is. :)
The line double k1 = 1/60
will be evaluated by the compiler as a compile-time constant. Since there is no '.0' on the end of the number, 1/60
will be evaluated via integer division, and hence be 0.
The denominator has to be a decimal number too.
double k1 = 1/60.0; //Should work
Otherwise your program will essentially truncate all the decimals.
LITTLE EXTRA: When your denominator is a variable, you have to cast it:
double k2 = 1/(double)myDenom;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With