This behaves as wanted:
double t = r[1][0] * .5;
But this doesn't:
double t = ((1/2)*r[1][0]);
r
is a 2-D Vector.
Just thought of a possibility. Is it because (1/2
) is considered an int
and (1/2) == 0
?
Is it because (1/2) is considered an int and (1/2) == 0?
Yes, both of those literals are of type int
, therefore the result will be of type int
, and that result is 0.
Instead, make one of those literals a float
or double
and you'll end up with the floating point result of 0.5
, ie:
double t = ((1.0/2)*r[1][0]);
Because 1.0
is of type double
, the int
2 will be promoted to a double
and the result will be a double
.
Write this instead:
double t = ((1/2.0)*r[1][0]);
1 / 2
is an integer division and the result is 0
.
1 / 2.0
is a floating point division (with double
values after the usual arithmetic conversions) and its result is 0.5
.
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