Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zoomRelativePoint: Can Lead to NaN Values #37

Closed
ashfurrow opened this issue Jun 12, 2014 · 9 comments
Closed

zoomRelativePoint: Can Lead to NaN Values #37

ashfurrow opened this issue Jun 12, 2014 · 9 comments
Labels

Comments

@ashfurrow
Copy link
Contributor

Consider the following code:

- (CGPoint)zoomRelativePoint:(CGPoint)point
{
    CGFloat x = (self.contentSize.width / self.originalSize.width) * point.x;
    CGFloat y = (self.contentSize.height / self.originalSize.height) * point.y;
    return CGPointMake(round(x), round(y));
}

If originalSize hasn't been set from CGPointZero, then the resulting x and y values can be NaN, so if you rely on the return value for this for some view geometry, it can lead to an app crash.

@dblock dblock added the bug? label Jun 13, 2014
@ashfurrow
Copy link
Contributor Author

So I'm thinking that returning CGPointZero would work well as a sentinel value? Or should we do something else, like return {-1, -1}? Gosh I can't wait for Swift.

@orta
Copy link
Collaborator

orta commented Jun 17, 2014

CGPointZero, think its better to not throw everything out of whack.

@ashfurrow
Copy link
Contributor Author

👍

@dblock
Copy link
Collaborator

dblock commented Jun 17, 2014

I think CGPointZero would be incorrect, it's the geometric zero. If you cannot do the math because you don't have a size you should probably explicitly fail. Raise an error?

@ashfurrow
Copy link
Contributor Author

Yeah unfortunately there's no equivalent of CGRectNull here. Maybe this should be something that the calling code is responsible for checking against?

@dblock
Copy link
Collaborator

dblock commented Jun 17, 2014

Maybe just assert and return the original point otherwise?

@ashfurrow
Copy link
Contributor Author

Not sure if I like that, though. I mean, that'd bring down the whole app just for the originalSize being zero in either dimension, which doesn't really make any sense, is completely valid from a UIKit-perspective. What'd be awesome is if we had optional types (heh), but we don't. The "best" approach I can think of would be to wrap the point in a struct of our own with a "valid" flag included. But that seems like overkill.

@dblock
Copy link
Collaborator

dblock commented Jun 17, 2014

Agreed on overkill. Do you think adding an assert and not changing anything in the code itself would be sufficient? A store app will run through the assert and do what the current version of the library is doing (NaN).

@ashfurrow
Copy link
Contributor Author

Yeah I think you're right @dblock – probably the best compromise.

ashfurrow added a commit to ashfurrow/NAMapKit that referenced this issue Jun 17, 2014
ashfurrow added a commit to ashfurrow/NAMapKit that referenced this issue Jun 17, 2014
ashfurrow added a commit to ashfurrow/NAMapKit that referenced this issue Jun 17, 2014
ashfurrow added a commit to ashfurrow/NAMapKit that referenced this issue Jun 17, 2014
ashfurrow added a commit to ashfurrow/NAMapKit that referenced this issue Jun 17, 2014
ashfurrow added a commit to ashfurrow/NAMapKit that referenced this issue Jun 17, 2014
ashfurrow added a commit to ashfurrow/NAMapKit that referenced this issue Jun 17, 2014
ashfurrow added a commit to ashfurrow/NAMapKit that referenced this issue Jun 17, 2014
ashfurrow added a commit to ashfurrow/NAMapKit that referenced this issue Jun 17, 2014
ashfurrow added a commit to ashfurrow/NAMapKit that referenced this issue Jun 17, 2014
ashfurrow added a commit to ashfurrow/NAMapKit that referenced this issue Jun 17, 2014
ashfurrow added a commit to ashfurrow/NAMapKit that referenced this issue Jun 17, 2014
@dblock dblock closed this as completed in a21cb7d Jun 17, 2014
dblock added a commit that referenced this issue Jun 17, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants