# Can You Solve This Mind-Bending Conundrum?

Although Max dissected his algorithm operation by operation, trying different test values to see what would happen, he was getting nowhere fast...

I've just been fighting with a mind-bending conundrum. This is all part of my ongoing Inamorata Prognostication Engine project. I'm currently working on the code to calculate how many days it is until the next full moon, a time that -- experience has shown -- can be somewhat troublesome with regards to one's Inamorata.

The problem was that my code was producing unexpected results. I was starting to pull my hair out. By yesterday, I was contemplating posting a blog asking for your help. But I was mulling things over in the wee hours of this morning when I thought, "Hang on just a minute. Could it be that...?" It could indeed! The bottom line is that I've tracked down the root of my problem and everything is once again running smoothly in "The House of Max," but for a moment there I thought I was losing my mind.

What? You think you could have sorted this out faster than I? Well, let's see, shall we? The remainder of this blog reflects my musings from yesterday, February 20, 2014, before I'd worked out what I was doing wrong...

Remember that I'm working with an Arduino, so my float variables have only around 6 to 7 decimal digits of precision. The output from my real time clock shows me that today's date (at the time of this writing) is February 20, 2014. Since today is indeed the February 20, I'm happy so far.

I use this date to calculate today's Julian day number and obtain a value of 2456709. I bounce over to the USNO website to confirm that they are in agreement with me and that today does indeed correspond to a Julian day number of 2456709. I'm still smiling.

Next, I go to the MoonPhases.Info website and look up the full moon dates for 2014 as illustrated below.

Purely for the sake of a quick test, I use the date of the most recent full moon -- February 14, 2014 -- as my reference point (any full moon in the past 1,000 years would do, so later I intend to replace this reference with something more appropriate, like a full moon that fell on April 1, or one that occurred on the day that something interesting or relevant happened -- do you have any suggestions?).

But we digress... I enter February 14, 2014 as the date of the reference full moon into my code, which calculates that this corresponds to a Julian day number of 2456703. Since this was only six days ago, and since 2456709 – 2456703 = 6, I think we can safely say that we're still on track.

But this is where my smile turns upside down into a frown, because my Arduino now tells me that -- after performing some heroic calculations -- it has determined that there are 21 days until the next full moon and that the date of that full moon will be March 13, 2014. I would say "Close, but no cigar," except that this is not even particularly close. The actual date, as shown in the image above, should be March 16, 2014, which is 24 days in our future. "Oh dear," I said to myself (or words to that effect).

In a moment I'll show you the code I'm using to calculate how many days there are to the next full moon. From there, we can calculate the actual date of the next full moon. But first, let me walk you through the reasoning process I went through to determine what algorithm to use (remember that, as for most things in life, I'm making all of this up as I go along).

Purely for the sake of a starting-point example, let's assume that our universe has only been in existence for a little over 100 days. Let's also assume that a full moon occurs every 10 days on the dot -- that is, there was full moon on days 10, 20, 30, 40, etc. Let's pick one of these full moons as our reference full moon -- say the one that occurred on day 100.

Now, let's assume that we are currently on day number 134 in our hypothetical universe. So, the number of days (the "difference" or 'd') between today and our reference full moon is 134 - 100 = 34. We know that the period ('p') of our moon's orbit is 10 days. What we want to do is to divide the number of days 'd' by the period 'p' and keep the remainder. In computer terms, when performing integer math, we have two types of divide operation available to us:

// Standard divide operation; remainder is lost

d / p = 34 / 10 = 3

// Modulo divide operation; returns the remainder

d % p = 34 % 10 = 4

In our terms, this remainder of 4 equates to 4 days. Thus, since the period of our moon's orbit is 10 days, the number of days to the next full moon is p - 4 = 6. Furthermore, since we know that today's number is 134, we therefore know that the next full moon will occur on day 134 + 6 = 140. Tra la!

Of course, our test as described above was based on integer math operations. If we had been dealing with real numbers, then d / p = 34 / 10 = 3.4. If we now throw away the integer part of this result to leave a remainder of 0.4, we have to multiply this by the period to obtain the number of days; that is, 0.4 x p = 0.4 x 10 = 4 days.

Author

Max The Magnificent 2/24/2014 2:47:12 PM

I forgot to mention that this required ONLY integer math!Oooh -- now you're talking my language :-)

Author

Max The Magnificent 2/24/2014 2:46:16 PM

This "problem" was solved COMPLETELY about 2500 years ago. The Jewiah calendar is so-called "solunar"one.I've heard of this, but I have to admit that I'd then forgotten all about it. I will take a look -- it would be interesting to see how well that syncs with my current algorithm.

Author

mhrackin 2/24/2014 2:41:44 PM

Author

mhrackin 2/24/2014 2:39:59 PM

Author

Max The Magnificent 2/24/2014 12:42:18 PM

Beware the difference between the two definitions of Blue Moon! The newer one is two full moons in a month, the other is the third of four full moons in a season.I think I'm on top of this -- I have no truck with the new definition -- I hold to the original definition that it's the third of four full moons in a season.

There's also the problem that the Maine Farmer's Almanac, referenced by a lot of discussions on this topic, defined the start of each season according to the Right Ascension of the Mean Sun -- an idealised, fictional Sun which moves at a uniform speed, giving seasons of equal length. By comparison, the start of the astronomical seasons are defined according to the actual position of the Sun against the fixed stars, which leads to seasons of unequal length. I'm going with the astronomers on this one.

Thank goodness this isn't complicated LOL

Author

Stargzer 2/24/2014 12:26:01 PM

Now I'm moving on to calculating the date of the next blue moon (watch thsi space).Beware the difference between the two definitions of Blue Moon! The newer one is two full moons in a month, the other is the third of four full moons in a season. Also note that sometimes astronomical tables reference UTC (Uniform Time Coordinated), which is close enought to GMT for the rest of us. So something that happens in England can be on a different local date in the US (and elsewhere!).

Wikipedia has a good discussion on this topic, including the origin of the first definition by a misinterpretation of the second in a 1946 article in Sky & Telescope magazine. A later Sky & Telescope article (referenced in the WIkipedia article) goes into more detail.

Happy Computing!

Author

Max The Magnificent 2/22/2014 11:50:10 AM

Is the problem related with the reference day you took?Nope -- check out the other comments to see where my problem lay.

I've since verified my algorithm (using the synodic month) for multiple reference full moons from 1900 through to 2014 -- also I've tried it for ones that started as early as 0:13 in the morning and as late as 23:18 at night (just to check that small errors didn't creap up on me) and thus far it's worked in all cases.

Now I'm moving on to calculating the date of the next blue moon (watch thsi space).

Author

Max The Magnificent 2/22/2014 11:46:15 AM

the problem is that you used a sideral month instead of a lunar month (29.53059 days)You've got it -- I was using the sidereal month with a value of 27.321661 days, when I should have been using the synodic month value of 29.530588 days.

When I was laying in bed working this through in my mind, I knew that my algorithm was being calculated correctly -- by a process of elimination I realized that the only factor remaining was the value I was using for the period of the monthy cycle, so I went back to the web and looked up "lunar cycles" and saw a bunch of entries, including the difference in the definitions of sidereal and synodic months.

I was both "high-fiving" and kicking myself at the same time :-)

Author

Garcia-Lasheras 2/22/2014 8:48:08 AM

Is the problem related with the reference day you took?

If the sideral month 27.321661, and if we assign a integer "label" to each full moon, there will be potential reference days that deviate from its real value. i.e. we will need to assign a real "label" to every reference full moon or, alternatively, take a a reference full moon day in which the "label" is as close to a integer number as possible.

Author

elizabethsimon 2/21/2014 8:27:43 PM

But what if your inamora is affected by new moons instead of full moons?