Skip to content

Support for extreme year values, string helpers for C#.

Compare
Choose a tag to compare
@cosinekitty cosinekitty released this 26 Feb 23:59
· 68 commits to master since this release

Extreme year values

When converting Gregorian calendar dates to day values and back, I have been using an algorithm by Fliegel and Van Flandern that was published in October 1968. It is a wonderful formula because it doesn't use any conditional logic to handle all the leap year rules. Rather, it uses simple integer arithmetic to work in an almost magic way.

However, I recently discovered a few problems for extreme year values, especially years before 4317 BC, the Julian Day epoch. These caused the formulas to fail due to quirks of negative numbers in the calculations. I also had concerns about using 32-bit integers to do the calculations.

So I decided to write unit tests that require year values to work over a nearly 2-million year span: the range of years from −999999 to +999999. I did this for all languages except JavaScript, where I don't use Fliegel / Van Flandern formulas because the built-in type Date effortlessly supports years from −199999 to +199999:

> new Date('+199999-01-01T00:00:00Z')
+199999-01-01T00:00:00.000Z
> new Date('-199999-01-01T00:00:00Z')
-199999-01-01T00:00:00.000Z

Then I worked until I got all the tests to pass. This work mainly consisted of

  • Adjusting Fliegel / Van Flandern to avoid negative values over the wider year range.
  • In other parts of the code, handling Python's different idea of integer division when numbers are negative. In Python, 10 // 3 == 3, but -10 // 3 == -4.
  • In C, casting a floating point number to an integer causes it to be rounded toward zero, when I needed it to be rounded down, whether positive or negative. So I had to change (long)x to (long)floor(x).

Now all the languages except JavaScript have consistent logic for handling calendar dates that passes all the tests.

String helpers for C#

In the C# version of Astronomy Engine, I added ToString methods for the types AstroVector, StateVector, and Observer. These are not only helpful for easy printing of values, but are handy while using the debugger. Just hover your mouse over a variable and you can see what's in it!

I added TryParse functions for AstroTime and AstroVector to parse the string representations back into a time or vector.