Objective C snippet to understand floor and ceil C functions:
NSArray *array = @[@(5.0), @(5.3), @(5.5), @(5.7)];
CGFloat a, r;
for (NSNumber *n in array) {
a = [n floatValue];
r = ceil(a);
NSLog(@"Ceil (%.1f) = %.1f", a, r);
r = ceilf(a);
NSLog(@"Ceilf (%.1f) = %.1f", a, r);
}
for (NSNumber *n in array) {
a = [n floatValue];
r = floor(a);
NSLog(@"Floor (%.1f) = %.1f", a, r);
r = floorf(a);
NSLog(@"Floorf (%.1f) = %.1f", a, r);
}
It is interesting, that if you divide for instance ceil(9 / 2) you will get 4. But if you divide ceil(9 / 2.0) you will get 5 as expected. I struggled with this issue a little, while trying to calculate item height dynamically for iPhone 5 and iPhone 3-4 in my UICollectionView this way:
CGFloat viewHeight = IS_WIDESCREEN ? 568.0 : 480;
NSInteger numberOfRows = ceil([buttonData count] / 2.0);
CGFloat itemHeight = (viewHeight - 20.0 - 44.0) / numberOfRows;
where
#define IS_WIDESCREEN [[UIScreen mainScreen] bounds].size.height >= 568.0
I guess, that when you divide 9/2 you get 4 as a result (integer division). When you divide 9/2.0 you get 4.5 which is “ceiled” correctly.