This makes the calculation correct for the case where periodDurationMs does not divide exactly into durationMs.