<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: Phases Of The Moon</title>
	<atom:link href="http://programmingpraxis.com/2010/01/22/phases-of-the-moon/feed/" rel="self" type="application/rss+xml" />
	<link>http://programmingpraxis.com/2010/01/22/phases-of-the-moon/</link>
	<description>A collection of etudes, updated weekly, for the education and enjoyment of the savvy programmer</description>
	<lastBuildDate>Mon, 28 May 2012 03:30:45 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: Mike</title>
		<link>http://programmingpraxis.com/2010/01/22/phases-of-the-moon/#comment-1150</link>
		<dc:creator><![CDATA[Mike]]></dc:creator>
		<pubDate>Mon, 12 Apr 2010 20:51:43 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1920#comment-1150</guid>
		<description><![CDATA[[sourcecode lang=&quot;python&quot;]
import datetime
from math import fmod

def moonphase( date=None ):
    &#039;&#039;&#039;return phase of the moon on a given date.

    date is a datetime.date object.  Defaults to today.
    &#039;&#039;&#039;
    
    if date is None:
        date = datetime.date.today()

    known_new_moon = datetime.date(2000,1,6)
    lunar_cycle = 29.530588853   # days per lunation
    phase_length = lunar_cycle/8 # days per phase

    days = (date - known_new_moon).days
    days = fmod(days + phase_length/2, lunar_cycle)

    phase = int( days * ( 8 / lunar_cycle ) )

    phasetext = (&quot;new&quot;, &quot;waxing crescent&quot;,
                 &quot;first quarter&quot;, &quot;waxing gibbous&quot;,
                 &quot;full&quot;, &quot;waning gibbous&quot;,
                 &quot;last quater&quot;, &quot;waning crescent&quot;)

    return phase, phasetext[ phase ]
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<pre class="brush: python;">
import datetime
from math import fmod

def moonphase( date=None ):
    '''return phase of the moon on a given date.

    date is a datetime.date object.  Defaults to today.
    '''
    
    if date is None:
        date = datetime.date.today()

    known_new_moon = datetime.date(2000,1,6)
    lunar_cycle = 29.530588853   # days per lunation
    phase_length = lunar_cycle/8 # days per phase

    days = (date - known_new_moon).days
    days = fmod(days + phase_length/2, lunar_cycle)

    phase = int( days * ( 8 / lunar_cycle ) )

    phasetext = (&quot;new&quot;, &quot;waxing crescent&quot;,
                 &quot;first quarter&quot;, &quot;waxing gibbous&quot;,
                 &quot;full&quot;, &quot;waning gibbous&quot;,
                 &quot;last quater&quot;, &quot;waning crescent&quot;)

    return phase, phasetext[ phase ]
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Maurits</title>
		<link>http://programmingpraxis.com/2010/01/22/phases-of-the-moon/#comment-992</link>
		<dc:creator><![CDATA[Maurits]]></dc:creator>
		<pubDate>Wed, 03 Feb 2010 22:21:32 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1920#comment-992</guid>
		<description><![CDATA[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.]]></description>
		<content:encoded><![CDATA[<p>A quibble &#8211; the phases of the moon are only four:</p>
<p>1) waxing crescent<br />
2) waxing gibbous<br />
3) waning gibbous<br />
4) waning crescent</p>
<p>The other four states (new, first quarter, full, third quarter) are more in the nature of events than phases&#8230; that is to say, the moon is only new for an instant.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mark VandeWettering</title>
		<link>http://programmingpraxis.com/2010/01/22/phases-of-the-moon/#comment-961</link>
		<dc:creator><![CDATA[Mark VandeWettering]]></dc:creator>
		<pubDate>Mon, 25 Jan 2010 18:22:55 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1920#comment-961</guid>
		<description><![CDATA[Sorry, hit the wrong button.

... was January 6, 18:14 UTC.]]></description>
		<content:encoded><![CDATA[<p>Sorry, hit the wrong button.</p>
<p>&#8230; was January 6, 18:14 UTC.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mark VandeWettering</title>
		<link>http://programmingpraxis.com/2010/01/22/phases-of-the-moon/#comment-960</link>
		<dc:creator><![CDATA[Mark VandeWettering]]></dc:creator>
		<pubDate>Mon, 25 Jan 2010 18:21:44 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1920#comment-960</guid>
		<description><![CDATA[I didn&#039;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 &quot;nice&quot; feature of this is that it doesn&#039;t actually require any conversion to julian dates.  I think that most of these proposed solutions are slightly off: some don&#039;t perform proper rounding, and most seem to ignore the fact that the first new moon of January 2000 was January 6,]]></description>
		<content:encoded><![CDATA[<p>I didn&#8217;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 &#8220;nice&#8221; feature of this is that it doesn&#8217;t actually require any conversion to julian dates.  I think that most of these proposed solutions are slightly off: some don&#8217;t perform proper rounding, and most seem to ignore the fact that the first new moon of January 2000 was January 6,</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: iyo</title>
		<link>http://programmingpraxis.com/2010/01/22/phases-of-the-moon/#comment-949</link>
		<dc:creator><![CDATA[iyo]]></dc:creator>
		<pubDate>Sat, 23 Jan 2010 15:09:02 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1920#comment-949</guid>
		<description><![CDATA[There&#039;s my python version. I&#039;m surprised python&#039;s math.fmod(num, n) doesn&#039;t give results into &lt;0;n) but it is simple remainder - results are from interval (-n;n). Inverse indexing of list is also possible.

Listing of the code:

&lt;code&gt;
from datetime import date
from math import fmod


def phaseOfMoon(day):
	period = 29.530588853
	referenceDate = date(2000, 1, 6)
	phases = [&quot;new&quot;, &quot;waxing crescent&quot;, &quot;first quarter&quot;, &quot;waxing gibbous&quot;,
		&quot;full&quot;, &quot;waning gibbous&quot;, &quot;last quarter&quot;, &quot;waning crescent&quot;]

	daysDelta = (day - referenceDate).days
	moonAge = fmod(daysDelta, period)

	phaseNum = int( moonAge / (period / len(phases)) )

	return phases[phaseNum]

if __name__ == &quot;__main__&quot;:
	samples = [date(2000, 1, 1), date(2000, 1, 6), date(2000, 2, 8), date.today()]
	for sample in samples:
		print sample, &quot;=&quot;, phaseOfMoon(sample)
&lt;/code&gt;]]></description>
		<content:encoded><![CDATA[<p>There&#8217;s my python version. I&#8217;m surprised python&#8217;s math.fmod(num, n) doesn&#8217;t give results into &lt;0;n) but it is simple remainder &#8211; results are from interval (-n;n). Inverse indexing of list is also possible.</p>
<p>Listing of the code:</p>
<p><code><br />
from datetime import date<br />
from math import fmod</p>
<p>def phaseOfMoon(day):<br />
	period = 29.530588853<br />
	referenceDate = date(2000, 1, 6)<br />
	phases = ["new", "waxing crescent", "first quarter", "waxing gibbous",<br />
		"full", "waning gibbous", "last quarter", "waning crescent"]</p>
<p>	daysDelta = (day - referenceDate).days<br />
	moonAge = fmod(daysDelta, period)</p>
<p>	phaseNum = int( moonAge / (period / len(phases)) )</p>
<p>	return phases[phaseNum]</p>
<p>if __name__ == "__main__":<br />
	samples = [date(2000, 1, 1), date(2000, 1, 6), date(2000, 2, 8), date.today()]<br />
	for sample in samples:<br />
		print sample, "=", phaseOfMoon(sample)<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: novatech</title>
		<link>http://programmingpraxis.com/2010/01/22/phases-of-the-moon/#comment-948</link>
		<dc:creator><![CDATA[novatech]]></dc:creator>
		<pubDate>Fri, 22 Jan 2010 23:48:04 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1920#comment-948</guid>
		<description><![CDATA[ruby version
[sourcecode lang=&quot;ruby&quot;]
def julian (year, month, day)
a = (14-month)/12
y = year+4800-a
m = (12*a)-3+month
return day + (153*m+2)/5 + (365*y) + y/4 - y/100 + y/400 - 32045
end
def phase (year,month,day)
p=(julian(year,month,day)-julian(2000,1,6))%29.530588853
if p&lt;1.84566: return &quot;New&quot;
elsif p&lt;5.53699: return &quot;Waxing crescent&quot;
elsif p&lt;9.22831: return &quot;First quarter&quot;
elsif p&lt;12.91963: return &quot;Waxing gibbous&quot;
elsif p&lt;16.61096: return &quot;Full&quot;
elsif p&lt;20.30228: return &quot;Waning gibbous&quot;
elsif p&lt;23.99361: return &quot;Last quarter&quot;
elsif p&lt;27.68493: return &quot;Waning crescent&quot;
else return &quot;New&quot;
end
end

print &quot;#{phase(2020,1,23)}\n&quot;
print &quot;#{phase(1999,1,6)}\n&quot;
print &quot;#{phase(2010,2,10)}\n&quot;
print &quot;#{phase(1987,5,10)}\n&quot;

[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>ruby version</p>
<pre class="brush: ruby;">
def julian (year, month, day)
a = (14-month)/12
y = year+4800-a
m = (12*a)-3+month
return day + (153*m+2)/5 + (365*y) + y/4 - y/100 + y/400 - 32045
end
def phase (year,month,day)
p=(julian(year,month,day)-julian(2000,1,6))%29.530588853
if p&lt;1.84566: return &quot;New&quot;
elsif p&lt;5.53699: return &quot;Waxing crescent&quot;
elsif p&lt;9.22831: return &quot;First quarter&quot;
elsif p&lt;12.91963: return &quot;Waxing gibbous&quot;
elsif p&lt;16.61096: return &quot;Full&quot;
elsif p&lt;20.30228: return &quot;Waning gibbous&quot;
elsif p&lt;23.99361: return &quot;Last quarter&quot;
elsif p&lt;27.68493: return &quot;Waning crescent&quot;
else return &quot;New&quot;
end
end

print &quot;#{phase(2020,1,23)}\n&quot;
print &quot;#{phase(1999,1,6)}\n&quot;
print &quot;#{phase(2010,2,10)}\n&quot;
print &quot;#{phase(1987,5,10)}\n&quot;

</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Remco Niemeijer</title>
		<link>http://programmingpraxis.com/2010/01/22/phases-of-the-moon/#comment-946</link>
		<dc:creator><![CDATA[Remco Niemeijer]]></dc:creator>
		<pubDate>Fri, 22 Jan 2010 09:44:08 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1920#comment-946</guid>
		<description><![CDATA[My Haskell solution (see http://bonsaicode.wordpress.com/2010/01/22/programming-praxis-phases-of-the-moon/ for a version with comments):

[sourcecode lang=&quot;css&quot;]
import Data.Time
import Data.Time.Calendar.Julian
import Data.Fixed

moonPhase :: Integer -&gt; Int -&gt; Int -&gt; String
moonPhase y m d = phase . flip mod&#039; 29.530588853 . fromIntegral $
    diffDays (fromJulian y m d) (fromJulian 2000 1 6) where
    phase x &#124; x &lt;  1.84566 = &quot;New&quot;
            &#124; x &lt;  5.53699 = &quot;Waxing crescent&quot;
            &#124; x &lt;  9.22831 = &quot;First quarter&quot;
            &#124; x &lt; 12.91963 = &quot;Waxing gibbous&quot;
            &#124; x &lt; 16.61096 = &quot;Full&quot;
            &#124; x &lt; 20.30228 = &quot;Waning gibbous&quot;
            &#124; x &lt; 23.99361 = &quot;Last quarter&quot;
            &#124; x &lt; 27.68493 = &quot;Waning crescent&quot;
            &#124; otherwise    = &quot;New&quot;
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>My Haskell solution (see <a href="http://bonsaicode.wordpress.com/2010/01/22/programming-praxis-phases-of-the-moon/" rel="nofollow">http://bonsaicode.wordpress.com/2010/01/22/programming-praxis-phases-of-the-moon/</a> for a version with comments):</p>
<pre class="brush: css;">
import Data.Time
import Data.Time.Calendar.Julian
import Data.Fixed

moonPhase :: Integer -&gt; Int -&gt; Int -&gt; String
moonPhase y m d = phase . flip mod' 29.530588853 . fromIntegral $
    diffDays (fromJulian y m d) (fromJulian 2000 1 6) where
    phase x | x &lt;  1.84566 = &quot;New&quot;
            | x &lt;  5.53699 = &quot;Waxing crescent&quot;
            | x &lt;  9.22831 = &quot;First quarter&quot;
            | x &lt; 12.91963 = &quot;Waxing gibbous&quot;
            | x &lt; 16.61096 = &quot;Full&quot;
            | x &lt; 20.30228 = &quot;Waning gibbous&quot;
            | x &lt; 23.99361 = &quot;Last quarter&quot;
            | x &lt; 27.68493 = &quot;Waning crescent&quot;
            | otherwise    = &quot;New&quot;
</pre>
]]></content:encoded>
	</item>
</channel>
</rss>

