From fc86a8fd71e500bb931421c18f5f04cd92f43526 Mon Sep 17 00:00:00 2001 From: Timothy Moose Date: Sun, 26 Jun 2022 19:49:18 -0500 Subject: [PATCH] Correct numerical errors in roundedFrameSection calculation --- Sources/JTAppleCalendar/JTACMonthQueryFunctions.swift | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Sources/JTAppleCalendar/JTACMonthQueryFunctions.swift b/Sources/JTAppleCalendar/JTACMonthQueryFunctions.swift index c05bd5cf..a6449493 100644 --- a/Sources/JTAppleCalendar/JTACMonthQueryFunctions.swift +++ b/Sources/JTAppleCalendar/JTACMonthQueryFunctions.swift @@ -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 {