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
This commit is contained in:
Andrew Monshizadeh 2017-06-26 17:56:22 -07:00 committed by Facebook Github Bot
parent d0253b705a
commit e58a44cfbe

View file

@ -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;
}