Skip to content

Commit 3f0219f

Browse files
committed
Factor calendar denominators before multiplying
Reduce intermediate integer sizes by dividing out common denominator factors before multiplication. This speeds up mixed-denominator day-fraction addition while preserving exact results.
1 parent 36351ae commit 3f0219f

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

lib/elixir/lib/calendar/iso.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2151,11 +2151,11 @@ defmodule Calendar.ISO do
21512151
end
21522152

21532153
def add_day_fraction_to_iso_days({days, {parts, ppd}}, add, add_ppd) do
2154-
parts = parts * add_ppd
2155-
add = add * ppd
21562154
gcd = Integer.gcd(ppd, add_ppd)
2157-
result_parts = div(parts + add, gcd)
2158-
result_ppd = div(ppd * add_ppd, gcd)
2155+
ppd_factor = div(ppd, gcd)
2156+
add_ppd_factor = div(add_ppd, gcd)
2157+
result_parts = parts * add_ppd_factor + add * ppd_factor
2158+
result_ppd = ppd * add_ppd_factor
21592159
normalize_iso_days(days, result_parts, result_ppd)
21602160
end
21612161

0 commit comments

Comments
 (0)