Regarding evaluating the equation "x = 60 / 180;" where 'x' is a 'float' variable. This is the behavior of the standard C language. Here's what is happening:
- Arithmetic expressions are evaluated using the type of the parameters('integer' or 'float'). so [int] / [int] is evaluated with integer arithmetic and results in an [int] result. If one of the operands is a [float] then floating point arithmetic will be used in the expression calculation.
- 60 /180, using integer arithmetic results in 0.
- The result of the expression is converted to a floating point value (0.0) before it is stored in variable 'x'.
So when you changed '180' to '180.0', the compiler then did the calculations using floating point arithmetic and you get the desired results of 0.3333.
You'd get the same original results if the code was.
int i = 60;
int j = 180;
x = i/j;
There is a way to force the compiler to use floating point arithmetic in the above. You use a "cast" statement which converts one 'type' to another. The last line above would be replaced with
x = i / (float) j;
The "(float)" is a 'cast' modifier which says convert the expression to the right to a 'float' value. There's a lot more powerful things that you can do with 'cast' but can't be covered here.