6 Mar 2008 09:37
dynamic code generation to superoptimize calendar calculations
Kragen Javier Sitaker <kragen <at> pobox.com>
2008-03-06 08:37:02 GMT
2008-03-06 08:37:02 GMT
In http://blog.plover.com/calendar/leapday.html Mark-Jason Dominus suggests this algorithm for calculating leap years, as a proposed replacement for the Gregorian system: 1. Divide the year by 33. If the result [remainder?] is 0, it is not a leap year. Otherwise, 2. If the result is divisible by 4, it is a leap year. This Dominus Calendar has an average leap-day correction of 0.24242424... leap-days per year, against the Gregorian calendar's 0.2425, the tropical year's 0.24219 or so leap-days per year, and the vernal equinox year's 0.2422 or so leap-days per year. Perhaps it would be slightly simpler, and equally accurate, to do the following: 1. Divide the year by 33. 2. Divide the remainder by 4. 3. If the remainder is 1, it is a leap year. Other values that would work in place of 1 are 2 and 3, but not 0. I suspect there is some simpler algorithm (in the sense of not requiring division by a large number such as 33) that is also more accurate. You could write the second one as \ y . ((y % 33) % 4) == 1 and the first one as \ y . (\ r . (r != 0) && ((r % 4) == 0)) (y % 33), or if you're into flat representations, r = y % 33 r2 = r % 4 result = r2 == 1(Continue reading)
RSS Feed