Quote:
|
Originally Posted by Billfred
In essence, it forces the compiler to treat WHEEL_RAD like a long variable.
|
So right you are BillFred! (long) is a c "unary" operator called a cast. It forces or "coerces" a type conversion.
...if you can, take a peek at a c manual like K&Rs "The c programming Language" ... p 46-47
In your original equation,
unsigned int WheelCirc = (2000 * WHEEL_RAD * PI) / 1000000;
the type conversion takes place across assignments, that is, the value of the right side is converted to the type of the left side. An unsigned int in the pic is 16 bits, so you get to play with 0-65535. Unfortunately, your values take you way outside this range. But with Mikes eq:
unsigned int WheelCirc = ((long) WHEEL_RAD * 710) / 113;
the cast, (long) forces the right hand side calculations to be done in 32 bits.
Then, the assignment (the = sign) forces a type conversion to 16 bits.
The higher order bits get chopped when "shortening" - going from long to unsigned int, but the value of the right hand side, now fits 0-65535, so you get the answer you expect.
(I also like the 355/113 trick

)
Eric