Swift: Compilation errors when using math functions on CGGeometry types

Originator:sshrpe
Number:rdar://17180625 Date Originated:05-Jun-2014 07:04 PM
Status:Open Resolved:
Product:Developer Tools Product Version:
Classification: Reproducible:Always
 
Summary:
Using math functions such as ceil() causes compilation errors when used within extensions of CGGeometry types (CGRect, CGSize etc) and built against iOS simulators or in fat binaries.

Example:

An extension to CGSize to convert a size to a specified aspect ratio and ensure it falls on an exact multiple value of that ratio would be very useful. The code below provides such a conversion, returning a new CGSize struct with the desired aspect ratio.

extension CGSize {

    func normalisedSizeWithAspectRatio(ratio:CGSize) -> CGSize {
        let base = ceil( self.width / ratio.width );
        return CGSizeMake(base * ratio.width, base * ratio.height)
    }

}

This code will always cause a compilation error in one way or another if used in an iOS project. When compiling for the 64-bit simulators (iPhone 5s, iPad Air) you need to use the ceil() function, as CGFloat is defined as a Double. When compiling for 32-bit simulators you need to use the ceilf() function as CGFloat is defined as a Float. Compiling for devices always fails one way or the other.

I remain convinced that if I throw enough casts at the problem I will be able to make it work eventually, but I have not found the solution and the code this will cause me to end up writing would be highly obfuscated.

Steps to Reproduce:
Add the attached file to an iOS project and compile for device, 32-bit simulator and 64-bit simulator

Expected Results:
The same code should be able to compile on all three target devices

Actual Results:
Code using ceil() will compile for 64-bit, code using ceilf() will compile on 32-bit. 

Version:
Xcode 6 beta 1

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!