In C and Objective-C, should we use 0.5f or 0.5?
c, floating-point, objective-c
Solution
`0.5` is a `double` constant, while `0.5f` is a `float` constant.
is there advantage of using 0.5f?
yes. you either want to specify the type (read: precision) for the calculation or avoid implicit narrowing (and associated compiler warnings).
the example you provide isn't compelling, beyond avoiding compiler warnings or just being very exacting regarding the parameter type.
however, less-than-obvious promotions to double precision can be costly.
To answer "Should we use 0.5f or 0.5": Because there are appropriate times for each, you should be using both in your programs. You should either be using the type appropriate for the calculation/precision you need, the type for the parameter you pass, or the type appropriate for other values in the expression. This is just like integers -- there are appropriate times to consider widths, and there appropriate times to suffix (e.g. `U` and `L`). There are times when the distinction is important, and (naturally) times when it is not important.
Problem
There are many places that I saw an author uses: ``` sprite.anchorPoint = CGPointMake(1, 0.5f); ``` that is, why not use `0.5` instead of `0.5f` -- is there advantage of using `0.5f`?