Skip to content

Correct numerical errors in roundedFrameSection calculation #1355

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Sources/JTAppleCalendar/JTACMonthQueryFunctions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ extension JTACMonthView {
case .stopAtEachCalendarFrame, .stopAtEach, .nonStopTo:
assert(fixedScrollSize != 0, "You should not try to divide by zero.")
let frameSection = theTargetContentOffset / fixedScrollSize
let roundedFrameSection = floor(frameSection)
// Reduce the precision of `frameSection` before calling `floor()` to eliminate numerical
// errors that would produce the wrong result. For example, if `frameSection == 49.9999999999999`,
// the double precision calculation will incorrectly procude 49 while the float precision
// calculation will correctly produce 50.
let roundedFrameSection = CGFloat(floor(Float(frameSection)))
if scrollDirection == .horizontal {
x = roundedFrameSection * fixedScrollSize
} else {
Expand Down