CGRect intersection gives surprising result due to floating point arithmetic

(Frederick Kellison-Linn) #1

Due to FP rounding, we have the following result:

let rect = CGRect(x: 100.33333333333333333333333, y: 0, width: 100, height: 100) let infinite = CGRect.infinite 
let intersection = infinite.intersection(rect) 
print(infinite.contains(rect)) // true 
print(intersection == rect) // false

I've filed a bug here but wanted to solicit input from the community about whether this is actually a bug. It seems inherent to me that if rect1.contains(rect2) == true, then rect1.intersection(rect2) == rect2.

More generally, this appears to be the behavior on macOS and iOS as well, and I'm curious about what the view is on feature parity vs. bug fixes. If there's a buggy implementation, do we value consistency with Apple platforms over fully correct implementations?