Writing graphics code in UIKit is a PITA. The "nominal" floating point type for Swift is Double
. But most of the UIKit
graphics code uses CGFloat
which seems to be either Double or Float based on the chip in my phone. I find myself having to constantly use CGFloat() and Double() transformers.
I have considered killing the problem by simply providing the operators it continually complains are lacking (I like to add lots of numeric type extensions anyway):
func * (lhs:CGFloat, rhs:Double) -> Double {
return Double(lhs) * rhs
}
func * (lhs:CGFloat, rhs:Double) -> CGFloat {
return lhs * CGFloat(rhs)
}
func * (lhs:Double, rhs:CGFloat) -> Double {
return lhs * Double(rhs)
}
func * (lhs:Double, rhs:CGFloat) -> CGFloat {
return CGFloat(lhs) * rhs
}
With this in place, I don't have to care anymore. I realize there will be lots of opinions as to whether this is a good thing or not. I get the cases where there can be subtle differences between a CGFloat that is a Float on a 32 bit platform and a Double, but I'm not sure that I'm likely to see them anyway. In other words, I can do this once and only once and get stung by those edge cases where fp math breaks down at the boundaries, or I can constantly convert things a million times over and still get stung by the same edge case. So my question is, is there ANY OTHER reason than those edge cases of fp math, not to do this?