From e58a44cfbef27dae6ad3328177c51345bc165c0a Mon Sep 17 00:00:00 2001 From: Andrew Monshizadeh Date: Mon, 26 Jun 2017 17:56:22 -0700 Subject: [PATCH] Compare using FLT_EPSILON Summary: Floating point math loses precision and so comparing two floats that are likely to only differ in the mantissa, and even then by extremely small amounts, is tricky. Exact equality often fails in these cases and asserting on exact equality is probably not necessary. Closes https://github.com/Instagram/IGListKit/pull/828 Differential Revision: D5310575 Pulled By: rnystrom fbshipit-source-id: f942489fbe0b2bc5f215329caac8abbae577a830 --- Source/IGListCollectionViewLayout.mm | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Source/IGListCollectionViewLayout.mm b/Source/IGListCollectionViewLayout.mm index 57d71433..f8cf3e55 100644 --- a/Source/IGListCollectionViewLayout.mm +++ b/Source/IGListCollectionViewLayout.mm @@ -353,15 +353,16 @@ static void adjustZIndexForAttributes(UICollectionViewLayoutAttributes *attribut // add the left inset in case the section falls on the same row as the previous // if the section is newlined then the x is reset itemX += insets.left; - + // the farthest right the frame of an item in this section can go const CGFloat maxX = width - insets.right; - + for (NSInteger item = 0; item < itemCount; item++) { NSIndexPath *indexPath = [NSIndexPath indexPathForItem:item inSection:section]; const CGSize size = [delegate collectionView:collectionView layout:self sizeForItemAtIndexPath:indexPath]; - IGAssert(size.width <= paddedWidth, @"Width of item %zi in section %zi must be less than container %.0f accounting for section insets %@", + IGAssert(size.width <= paddedWidth || fabs(size.width - paddedWidth) < FLT_EPSILON, + @"Width of item %zi in section %zi must be less than container %.0f accounting for section insets %@", item, section, width, NSStringFromUIEdgeInsets(insets)); CGFloat itemWidth = MIN(size.width, paddedWidth); @@ -380,7 +381,7 @@ static void adjustZIndexForAttributes(UICollectionViewLayoutAttributes *attribut itemY += lineSpacing; } } - + const CGFloat distanceToRighEdge = paddedWidth - (itemX + itemWidth); if (self.stretchToEdge && distanceToRighEdge > 0 && distanceToRighEdge <= epsilon) { itemWidth = paddedWidth - itemX; @@ -446,7 +447,7 @@ static void adjustZIndexForAttributes(UICollectionViewLayoutAttributes *attribut } } } - + return result; }