Quote:
Originally Posted by AustinSchuh
It might be better to simplify that further using some algebra to something like
Code:
time = (int)((long)10 * (long)degrees / (long)3)
That way, you won't run into interger or long overflow problems until degrees is 100 times as large as it would have had to be earlier.
|
True, he doesn't gain anything in using 1000 and 300 vs 10 and 3. And in that case he doesn't need to force the compiler to do the computation as long. 10 * 360 = 3600 which fits quite comfortably in an int.
BTW, you only need the first (long) cast. Once one element of that calcuation is long, the compiler will force the rest to long to complete it. Actually, casting either the 10 or degrees will do it. Casting the 3 to long will force the result to long but the compiler is free to do the calculation in the parentheses as int then promote it to long before the division.