You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
def leap_year?(year) when year > 0 do
Math.mod(year, 4) == 0
end
def leap_year?(year) do
Math.mod(year, 4) == 3
end
in module Calendrical.Calendar.Julian by an alternative that doesn't use the Math helper module and uses only one function:
def leap_year_alt?(year) do
(year > 0) && (rem(year, 4) == 0) || (rem(year, 4) == -1)
end
Afterall we know that year is an integer. The term (rem(year, 4) == -1) takes into account that Erlang produces a negative modulo for negative numbers. In other languages (i.e. Python) this would have been (year % 4) == 3 for negative years.
I tested the new function against the old one using:
test "Julian.leap_year?(year)" do
for year <- -4717..2020 do
assert Julian.leap_year_alt?(year) === Julian.leap_year?(year)
end
end
All tests produced the same results and passed.
Of course, if accepted, leap_year_alt?, should be renamed leap_year?, so that no outer depencies will be broken.
The text was updated successfully, but these errors were encountered:
May I suggest to replace the functions
in module
Calendrical.Calendar.Julian
by an alternative that doesn't use theMath
helper module and uses only one function:Afterall we know that
year
is an integer. The term(rem(year, 4) == -1)
takes into account that Erlang produces a negative modulo for negative numbers. In other languages (i.e. Python) this would have been(year % 4) == 3
for negative years.I tested the new function against the old one using:
All tests produced the same results and passed.
Of course, if accepted,
leap_year_alt?
, should be renamedleap_year?
, so that no outer depencies will be broken.The text was updated successfully, but these errors were encountered: