Quote:
Originally Posted by Joe Ross
Since you don't really care what order the operations happen, the following will work for you.
time = (1000 * degrees) / 300;
time = (1000 * 30) / 300;
time = (30000) / 300;
time = 100;
The only problem with the above example is that if degrees gets above 32, you'd overflow the integer value. You could either declare time as a long, or change your constants, since multiplying by 1000 then dividing by 300 is the same as multiplying by 10 then dividing by 3.
|
If he doesn't want to declare time as a long and carry that through the rest of the calculations, the following will work with time left as an int.
time = (int) ((long) 1000 * degrees / 300);
or more simply if the compiler is really ANSI compliant
time = (int) (1000L * degrees / 300);