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

Pixel aligned CGFloat equality fix #128

Merged
merged 1 commit into from
Mar 30, 2024
Merged

Conversation

bryankeller
Copy link
Contributor

Details

This PR corrects how we compare pixel-aligned CGFloats. It's been wrong this whole time, and it's causing an off-by-one error on the Airbnb Messages surface.

The previous implementation considered 0.66 and 0.84 to be equal for a 3x screen, since 0.84-0.66=0.18, and 0.18 is smaller than 0.33, implying that these 2 values are within 1px of each other (0.33) on a 3x screen. This approach failed to take into account the actual rounding to valid pixel values. In reality, 0.84 and 0.66 should not be considered equal because they round to different pixels (0.66 stays at 0.66, 0.84 rounds up to 1, not down to 0.66).

Related Issue

N/A

Motivation and Context

Bug fix

How Has This Been Tested

Types of changes

  • Docs change / refactoring / dependency upgrade
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.

@bryankeller bryankeller requested a review from brynbodayle March 30, 2024 00:56
@bryankeller bryankeller added the bug Something isn't working label Mar 30, 2024
Comment on lines +27 to +30
func isEqual(to rhs: CGFloat, screenScale: CGFloat) -> Bool {
let lhs = alignedToPixel(forScreenWithScale: screenScale)
let rhs = rhs.alignedToPixel(forScreenWithScale: screenScale)
return lhs == rhs
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the actual fix - the rest of the PR is just some minor refactoring

XCTAssert(!CGFloat(1).isEqual(to: 10, screenScale: 9))
XCTAssert(!CGFloat(1).isEqual(to: 10, screenScale: 9))
XCTAssert(!CGFloat(1).isEqual(to: 9, screenScale: 9))
XCTAssert(!CGFloat(1.333).isEqual(to: 1.666, screenScale: 3))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what was I thinking - of course these aren't equal. They're literally 1px apart on a 3x screen! 🤦‍♂️

@bryankeller bryankeller merged commit f9b27b7 into master Mar 30, 2024
2 checks passed
@bryankeller bryankeller deleted the bk/CGFloat-equality-fix branch April 1, 2024 00:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants