Technology Microsoft Software & solutions

The Visual Basic 2005 Cookbook Redux

< Continued from page 1

One of the most interesting parts of Peter's analysis was his dissection of Section 7.19, "Calculating the Phases of the Moon." My problem with the book is that the code presented is full of "magic numbers" that are not explained well enough for me.

Although the book does explain the code, it mainly relies on the explanation for the "magic numbers" that, "the linear curve fit equation presented here was researched and created only recently, using data from the Internet."


Dim cycles as Double = days * 0.3386319 - 12.5
Return Math.IEEERemainder(cycles, 1.0) * 29.53059


I like the rule that if you use a "magic number" in your code, you have an obligation to explain where it came from. So for your benefit, I include Peter's complete analysis.

----------------------

Section 7.19 - Calculating the Phase of the Moon

The constant 0.03386319 is the reciprocal of 29.53059, which is very close to the accepted value in days for the lunar month. So the algorithm, in words, says
  • compute the number of days since 1600 Jan 1 at 00:00
  • divide that by the number of days in a lunar month, to give the total number of lunar months since 1600 Jan 1 at 00:00
  • Subtract 12.5 days. I have computed independently that the moon is exactly halfway through its cycle (a full moon) on 1600 Jan 1 at 09:45, so subtracting 12.5 days moves it to 1599 Dec 19 at 21:45, with 8.94% of the moon's disk illuminated at 7.78% of the cycle.

But this is the crux of the algorithm ... finding the nearest new moon to 1600 Jan 1 at 00:00.

The authors seem to have this being 12.5 days earlier, which may be sufficiently accurate for the purpose of the algorithm, but does not take into account all of the possible perturbations affecting the moon.
  • Once the "first" new moon has been located, the Math.IEEERemainder function is used to reduce the number of lunar months to the fraction of a lunar month before or after the new moon, and this is then multiplied by the number of days in a lunar month to get the number of days before or after the new moon.

As the authors say, it's good enough to determine when one might dance by the light of the moon, but it's nowhere near accurate enough for calendric calculations such as the Chinese Lunisolar calendar.

----------------------

That's expert analysis!

In an email, Peter commented, "The bit about the moon phases was actually fun!"

Peter also pointed out lots of other possible historical and cultural problems that could interfere with a program that had to be completely accurate. I'll keep Peter's complete analysis on file in case an About Visual Basic reader needs more than the book provides!

Related posts "Technology : Microsoft Software & solutions"

How to Choose a Picture for a User Account and Start Menu in Windows Vista

Microsoft

How to Add Tracfone to the Start Menu

Microsoft

How to Stop the Reboot on the Blue Screen

Microsoft

How Do I Tell What Version of Windows XP I Am Using?

Microsoft

How to Make a Boot CD for Windows Vista

Microsoft

How to Move a Windows XP Installation

Microsoft

How to Install Ubuntu in VirtualBox

Microsoft

How to Fix a Problem With a System Idle Process

Microsoft

How to Add a VLK to Windows XP Home

Microsoft

Leave a Comment