# teaching machines

## Dates as Fractions

April 28, 2020 by . Filed under public.

Today is 4/28. As a fraction, today is $\frac{4}{28} = \frac{1}{7} \approx 0.143$. But how close is that to the proportion of the year that has passed? We are on day 119 of a year with 366 days, and $\frac{119}{366} \approx 0.325$. That’s a difference of approximately 0.182. That’s not very close.

Perhaps there are days whose rational numbers are closer? I had to find out, so I wrote a program. I’m gun-shy of date libraries, so I started with just this simple list of month lengths:

MONTH_LENGTHS = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
YEAR_LENGTH = MONTH_LENGTHS.sum


I hardcoded February as a non-leap year.

Next up, because I like pivoting around data rather than code, I modeled the subject of our study using a class. Its methods interpret the date as various rational numbers: as a month-over-day, as a day-of-year, and as a day-over-month.

class Date
attr_reader :month, :day, :doy

def initialize(month, day, doy)
@month = month
@day = day
@doy = doy
end

def doyProportion
@doy.to_f / YEAR_LENGTH
end

def monthOverDay
@month.to_f / @day
end

def dayOverMonth
@day.to_f / @month
end
end


To make a list of all the dates of the year, I decided to iterate through the interval [1, 365], maintaining separate month and day counters as I go. Upon reaching the end of month, I reset the day counter back to 1.

doy = 1
month = 1
day = 1

dates = []
while month < 13
dates << Date.new(month, day, doy)
doy += 1
if day < MONTH_LENGTHS[month - 1]
day += 1
else
day = 1
month += 1
end
end


Now I had all the pieces I needed to conduct my investigation. I sorted my list by the absolute difference between the month-over-day and the day-of-year proportion:

sorted = dates.sort_by do |date|
(date.monthOverDay - date.doyProportion).abs
end


Who do you think the winner was?

In Table 1, we have the top ten dates whose month-over-days are nearest to their day-of-year proportions.

Congratulations, January 19! Adding in the leap day changes this list very little.

Which dates do you think had the largest differences between their two rational numbers? You don’t really need a program to tell you the answers, which are shown in Table 2.

Congratulations, December 1!

America is one of the few countries that places months before days in dates. Sometimes the month-day-year ordering is called middle-endian ordering. Many more countries use little-endian ordering. They would say that today is 28/4.

Let’s examine figure out which dates are closest in little-endian ordering. We make just a slight change to our sorting criteria to consider the day-over-month rather than the month-over-day:

sorted = dates.sort_by do |date|
(date.dayOverMonth - date.doyProportion).abs
end


The dates whose rational numbers are nearest are shown in Table 3.

Congratulations, 1 April!

Once again, can you guess which dates are at the bottom of this list? You can, if you believe in yourself. The answers are shown in Table 4.

Congratulations, 31 January!

I am satisfied with the outcome of my investigation. I now have four new holidays to celebrate.