3

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?

Travis Griggs
  • 21,522
  • 19
  • 91
  • 167
  • Not sure I see the connection with a post about pi? Ironic since I've done a first class modeling of unit'ed angles in Swift (see http://stackoverflow.com/questions/32827507/can-i-bind-different-associated-values-in-a-swift-enum-to-the-same-var) – Travis Griggs May 03 '16 at 23:20
  • 2
    Why are you converting millions of times? This question might be more interesting if you included some of the code where you're having to write lots of these casts. There may be a much simpler alternative that doesn't introduce an operator function into the module's namespace. – nhgrif May 03 '16 at 23:39
  • "Million" times is hyperbole of course. I'm not worried about the execution times. It's how many times I have to manually input the code to convert back and forth. I do a number of custom controls. An example is that code that has to compute where to draw/layout things based on time and calendars. NSInterval is a Double. So every computation you do has to be cast as a CGFloat when you interface with CGRect, CGSize, and any of the CGContext methods. – Travis Griggs May 11 '16 at 18:08
  • I should add that of course any code involving user input, which is in CGFloats then has to be cast backwards to time happy things (e.g. Doubles). – Travis Griggs May 11 '16 at 18:09
  • why not just create a typealias and an ifdef. get rid of CGFloat completely then you can use all of the Float64 functions directly without adding operators etc. this solution you still have to add cases for Float32, Int, blah blah blah {#if !os(32bit) typealias myFloat = Float32 #else typealias myFloat = Float64 #end} – μολὼν.λαβέ Dec 02 '16 at 01:47

0 Answers0