Should I be writing CGFloat values with postfix f
or not?
CGFloat fValue = 1.2;
vs.
CGFloat fValue = 1.2f;
I know that this postfix define a float value. But is it necessary, does it make sense, are there any performance differences between using those two or is this just visual presentation so you can quickly define value type (e.g. float in this case)?
The Float and CGFloat data types sound so similar you might think they were identical, but they aren't: CGFloat is flexible in that its precision adapts to the type of device it's running on, whereas Float is always a fixed precision.
Suggested approach: It's a question of how many bits are used to store data: Float is always 32-bit, Double is always 64-bit, and CGFloat is either 32-bit or 64-bit depending on the device it runs on, but realistically it's just 64-bit all the time.
A CGFloat is a specialized form of Float that holds either 32-bits of data or 64-bits of data depending on the platform. The CG tells you it's part of Core Graphics, and it's found throughout UIKit, Core Graphics, Sprite Kit and many other iOS libraries.
f = float. In c a value of 1 is an integer and 1.0 is a double, you use f after a decimal number to indicate that the compiler should treat it as a single precision floating point number.
1.2 is a double
; i.e. 64-bit double-precision floating point number.
1.2f is a float
; i.e. 32-bit single-precision floating point number.
In terms of performance, it doesn't matter as the compiler will convert literals from float
to double
and double
to float
as necessary. When assigning floating-point numbers from functions, however, you will most likely need to cast to avoid a compiler warning.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With