Hi so whenever I try to do division such as double x = 3 * (5/10); it will make x = 0 for some reason. Is there a reason this happens in c++ I'm learning and have no clue why this happens.
Asked
Active
Viewed 1,527 times
-3
-
1You are doing integer math, with does not have fractional values. 5/10 simply becomes 0 (only the whole part of 0.5 is kept). To have the floating point value, one of you literals has to be of floating point type. 3 * (5.0/10) should work. – Jan Henke May 07 '16 at 07:06
-
15/10 is an integer division so the result is 0 – Biruk Abebe May 07 '16 at 07:06
-
Try this: `double x = 3 * (5.0 / 10.0)` to get what you expect. – chain ro May 07 '16 at 07:11
1 Answers
0
think about this: what data type is 5? what data type is 10? INTEGER!!!
then (int)5 / (int)10 = (int)0.5 = 0
try this
double a = 3;
double b = 5;
double c = 10;
double x = a * (b/c);

danilonet
- 1,757
- 16
- 33