Reputation: 50
I need the result of this variable in a program, but I don't understand why I can't get the right result.
double r = pow((3/2), 2) * 0.0001;
printf("%f", r);
Upvotes: 1
Views: 67
Reputation: 23218
The problem is integer division, where the fractional part (remainder) is discarded
Try:
double r = pow((3.0/2.0), 2) * 0.0001;
The first argument of the pow() expects a double. Because the ratio: 3/2
uses integer values, the result passed to the argument is 1. By changing to float values, the result of the division can retain the fractional part, and the result becomes 1.5, the form expected by the function.
Upvotes: 5
Reputation: 182782
(3/2)
involves two integers, so it's integer division, with the result 1
. What you want is floating point (double) division, so coerce the division to use doubles by writing it as (3.0/2.0)
Upvotes: 5