Why is there a difference between CGRectInfinite and a CGRect with all members set to INFINITY?

不问归期 提交于 2019-12-12 03:31:05

问题


When working with (contentless) SpriteKit nodes I bumped into an infinite rectangle

NSLog(@"%@", NSStringFromCGRect([node calculateAccumulatedFrame]));

which outputted

{{inf, inf}, {inf, inf}}

I thought to check for this with CGRectIsInfinite, but that test failed, which lead me to trying the following check

CGRect rect = [node calculateAccumulatedFrame];
if (rect.origin.x == INFINITY && rect.origin.y == INFINITY && 
    rect.size.width == INFINITY && rect.size.height == INFINITY) {
    if (!CGRectIsInfinite(rect)) {
        NSLog(@"I don't get it");
    }
}

Which outputs: I don't get it, which neatly summarises my position right now.

As pointed out by allestuetsmerweh in an answer the function does return true for CGRectInfinite, when I output this rectangle I get

{{-8.9884656743115785e+307, -8.9884656743115785e+307}, {1.7976931348623157e+308, 1.7976931348623157e+308}}

(the sizes are both DBL_MAX)

What's the reasoning behind CGRectInfinite being a rectangle with some values set to DBL_MAX i.s.o. a rectangle with all elements set to INFINITY? (while the API does return CGRect with all members set to INFINITY)
Or rather, why doesn't a rectangle with the elements set to INFINITY register as a CGRectIsInfinite?


回答1:


CGRect r = CGRectInfinite;
NSLog(@"%i", CGRectIsInfinite(r)); //prints "1"

From the docs:

An infinite rectangle is one that has no defined bounds. Infinite rectangles can be created as output from a tiling filter. For example, the Core Image framework perspective tile filter creates an image whose extent is described by an infinite rectangle.




回答2:


What's the reasoning behind CGRectInfinite being a rectangle with some values set to DBL_MAX i.s.o. a rectangle with all elements set to INFINITY?

Here are some guesses:

  1. IEEE 754 defines two infinities (+∞ and -∞). One has to be chosen (or there are ambiguous values for CGRectInfinite).
  2. Infinity is not really a number. This can lead to various issues when converting to a string for serialization.
  3. It's a bit awkward to work with infinite values in C.

And why doesn't a rectangle with the elements set to INFINITY register as a CGRectIsInfinite?

Because, as expected, CGRectIsInfinite compares its argument to CGRectInfinite (which has a special meaning in some APIs and so should be unambiguously identifiable).



来源:https://stackoverflow.com/questions/25135341/why-is-there-a-difference-between-cgrectinfinite-and-a-cgrect-with-all-members-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!