Phases Of The Moon
January 22, 2010
We calculate the moon’s age in days since the last new moon using the formula given previously, using the julian
function from the Standard Prelude to number the days. Normalize
is a convenience function that extracts the fractional part of a real number:
(define (days year month day)
(define (normalize x)
(let ((x (- x (floor x))))
(if (negative? x) (+ x 1) x)))
(let ((new 2451550.1) (moon 29.530588853)
(j (julian year month day)))
(* (normalize (/ (- j new) moon)) moon)))
We could initialize a vector and calculate the phase as an index into the vector, but the calculation involves real numbers, so we choose a safer approach:
(define (phase year month day)
(let ((d (days year month day)))
(cond ((< d 1.84566) "New")
((< d 5.53699) "Waxing crescent")
((< d 9.22831) "First quarter")
((< d 12.91963) "Waxing gibbous")
((< d 16.61096) "Full")
((< d 20.30228) "Waning gibbous")
((< d 23.99361) "Last quarter")
((< d 27.68493) "Waning crescent")
(else "New"))))
Here are some examples:
> (phase 2000 1 6)
"New"
> (days 2010 1 22)
7.106982227906547
> (phase 2010 1 22)
"First quarter"
You can run the program at http://programmingpraxis.codepad.org/23rZcxsl.
My Haskell solution (see http://bonsaicode.wordpress.com/2010/01/22/programming-praxis-phases-of-the-moon/ for a version with comments):
ruby version
There’s my python version. I’m surprised python’s math.fmod(num, n) doesn’t give results into <0;n) but it is simple remainder – results are from interval (-n;n). Inverse indexing of list is also possible.
Listing of the code:
from datetime import date
from math import fmod
def phaseOfMoon(day):
period = 29.530588853
referenceDate = date(2000, 1, 6)
phases = ["new", "waxing crescent", "first quarter", "waxing gibbous",
"full", "waning gibbous", "last quarter", "waning crescent"]
daysDelta = (day - referenceDate).days
moonAge = fmod(daysDelta, period)
phaseNum = int( moonAge / (period / len(phases)) )
return phases[phaseNum]
if __name__ == "__main__":
samples = [date(2000, 1, 1), date(2000, 1, 6), date(2000, 2, 8), date.today()]
for sample in samples:
print sample, "=", phaseOfMoon(sample)
I didn’t have any time for actually coding this up, but John Conway has a way of computing the phase of the moon that can be done in your head as part of his (formerly two vollume, now republished by AK Peterson as 4 volume) series Winning Ways. The “nice” feature of this is that it doesn’t actually require any conversion to julian dates. I think that most of these proposed solutions are slightly off: some don’t perform proper rounding, and most seem to ignore the fact that the first new moon of January 2000 was January 6,
Sorry, hit the wrong button.
… was January 6, 18:14 UTC.
A quibble – the phases of the moon are only four:
1) waxing crescent
2) waxing gibbous
3) waning gibbous
4) waning crescent
The other four states (new, first quarter, full, third quarter) are more in the nature of events than phases… that is to say, the moon is only new for an instant.
Erlang version