Tag algorithms

post 421

And while I’m at it, here’s a C function which returns the phase of the moon:

int moon_phase(int y, int m, int d)
{
    /*
      calculates the moon phase (0-7), accurate to 1 segment.
      0 = > new moon.
      4 => full moon.
      */
    int c,e;
    double jd;
    int b;

    if (m < 3) {
        y--;
        m += 12;
    }
    ++m;
    c = 365.25*y;
    e = 30.6*m;
    jd = c+e+d-694039.09;  /* jd is total days elapsed */
    jd /= 29.53;           /* divide by the moon cycle (29.53 days) */
    b = jd;     /* int(jd) -> b, take integer part of jd */
    jd -= b;     /* subtract integer part to leave fractional part of original jd */
    b = jd*8 + 0.5;    /* scale fraction from 0-8 and round by adding 0.5 */
    b = b & 7;     /* 0 and 8 are the same so turn 8 into 0 */
    return b;
}

post 420

Date algorithms. Part one. How to work out when Easter is:


    c = y / 100
    n = y - 19 * ( y / 19 )
    k = ( c - 17 ) / 25
    i = c - c / 4 - ( c - k ) / 3 + 19 * n + 15
    i = i - 30 * ( i / 30 )
    i = i - ( i / 28 ) * ( 1 - ( i / 28 ) * ( 29 / ( i + 1 ) )
        * ( ( 21 - n ) / 11 ) )
    j = y + y / 4 + i + 2 - c + c / 4
    j = j - 7 * ( j / 7 )
    l = i - j
    m = 3 + ( l + 40 ) / 44
    d = l + 28 - 31 * ( m / 4 )

Takes y, the year; and gives m and d, the month and day. Of course, it goes without saying (given lines like the second one) that these are all integer calculations.

Copyright © Found
Jim Finnis' personal blog

Built on Notes Blog Core
Powered by WordPress