Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
404 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...