I have the following calculation in Java:
rightMotor = (int) (((double)speed)/((double)100)*rightMotor));
Speed and rightMotor are both integers. The problem is, it keeps returning 0. I have tried to force to divide speed and 100 as doubles, which had no different results. Can anyone give me some insight in this?
Thanks in advance.
The reason the calculation returns zero is that the double
value computed by the expression is less than one.
It looks like you have placed the parentheses incorrectly - currently, you divide speed
by hundred times rightMotor
, which is bound to produce a value less than 1
.
You need to fix your formula to make it work. If you wanted to compute speed / 100 * rightMotor
as if it were a math formula, you could do the multiplication ahead of division, avoiding double
s altogether:
rightMotor = speed * rightMotor / 100;