SwipeView icon indicating copy to clipboard operation
SwipeView copied to clipboard

Is there a reason for using roundf instead of floorf when calculating currentPage?

Open vicc opened this issue 10 years ago • 0 comments

@nicklockwood Any reason why you chose to use roundf instead of floorfwhen calculating the currentPage?

- (NSInteger)currentPage {

    if (_itemsPerPage > 1 && _truncateFinalPage && !_wrapEnabled &&
        _currentItemIndex > (_numberOfItems / _itemsPerPage - 1) * _itemsPerPage)
    {
        return self.numberOfPages - 1;
    }
    return roundf((float)_currentItemIndex / (float)_itemsPerPage);
}

After a bit of testing I noticed that roundf is not reliable enough when attempting to calculate the currentPage. The reason being that roundf rounds up any float with a decimal point of 0.5 or above. Rounding down is a better option though. This is where floorf is ideal.

visual

The page numbers in pink highlight the errors when using roundf.

BTW great job on this component!

vicc avatar Apr 17 '15 05:04 vicc