Skip to content
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

Alternate calculation of Julian leap year #3

Open
rengel-de opened this issue Jan 26, 2018 · 0 comments
Open

Alternate calculation of Julian leap year #3

rengel-de opened this issue Jan 26, 2018 · 0 comments

Comments

@rengel-de
Copy link

rengel-de commented Jan 26, 2018

May I suggest to replace the functions

  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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant