Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a performance diff using CGFloat with or without postfix .f in Objective-C

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)?

like image 735
Borut Tomazin Avatar asked Mar 14 '13 09:03

Borut Tomazin


People also ask

What is the difference between CGFloat and Float?

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.

What is the difference between Float double and CGFloat?

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.

What does CGFloat mean?

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.

What does F after a number mean in C?

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 Answers

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.

like image 183
trojanfoe Avatar answered Sep 29 '22 08:09

trojanfoe