Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CGFloat-based math functions?

In both OS X and iOS, Apple is using the CGFloat typedef to automatically get a float on 32-bit systems and a double on 64-bit systems. But when using the normal Unix math functions you need to make that decision on a case by case basis.

For example the floor() function is defined as:

double floor(double x);

and the floorf() function is defines as:

float floorf(float x);

I know all iOS devices are 32-bit today, but the reason to use CGFloat is to automatically get improved precision when the first 64-bit iOS devices are introduced (iPad 5?) and not having to change any code. But to make that possible we need CGFloat-based math functions:

CGFloat Floor(CGFloat x);

Are there any functions like that in iOS or any third-party libraries? Or how do other developers handle this? I suspect most are either using CGFloat together with the float-versions on iOS but then you would "need" to change every line with a math function if compiling for a 64-bit iOS device (and handle two different versions of of your code base).

Or you could just use float instead of CGFloat everywhere and not worrying about 64-bit iOS. But then you would get inconsistency with Apple's libraries and IMHO uglier code.

Or you could maybe use CGFloat together with the double-versions and just take the space and performance hit of letting the compiler convert between double and float all the time. And not caring about possible "Implicit conversion to 32 Bit Type" warnings.

Or maybe the best strategy, bet on no 64-bit version of iOS will ever arrive (or at least in a near enough future) and use CGFloat together with float-versions of the Unix math functions and not worrying about the future.

What strategy are you using?

like image 362
Påhl Melin Avatar asked Mar 18 '11 13:03

Påhl Melin


People also ask

What is CGFloat?

When you build for a 64-bit CPU, the CGFloat type is a 64-bit, IEEE double-precision floating point type, equivalent to the Double type. When you build for a 32-bit CPU, the CGFloat type is a 32-bit, IEEE single-precision floating point type, equivalent to the Float type.

Is CGFloat a double?

As @weichsel stated, CGFloat is just a typedef for either float or double . You can see for yourself by Command-double-clicking on "CGFloat" in Xcode — it will jump to the CGBase.

What is the difference between the Float double and CGFloat data types?

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.


1 Answers

Edit: So this question is no longer theoretical now that we have 64-bit iOS!

I think the easiest way to deal with this is to use tgmath.h, instead of math.h (math.h gets imported by Cocoa automatically, tgmath doesn't so you'll need to declare it somewhere).

There are a number of potential solutions discussed at the Cocoa mailing list thread: http://www.cocoabuilder.com/archive/cocoa/291802-math-functions-with-cgfloat.html

I would agree with the majority of people on that list that tgmath is probably the way to go. I believe it was originally intended to allow easy porting of Fortran code, but it also works in this scenario. TGMath will let you do:

double floor(double x); float floor(float x); long double floor(long double x); 
like image 199
lxt Avatar answered Oct 07 '22 04:10

lxt