1. conversion of moon_age.c

Hi to all,

I just converted a C function to Euphoria, that can be usefull for people=

making calendar programs, planners etc. It is called 'moon_age' and
calculates the phases of the moon on any day in the future and the past.
I was searching for a method to determine on which day Easter will be in
years to come. In our western (christian) society Easter Sunday is always=

the first sunday after the first full moon in Spring. And this routine ca=
n
calculate the days of full moon, as well as new moon, first quarter etc.
It is also an interesting example of how C code can be translated to bett=
er
readable Euphoria code. There is only one question I have: if you look at=

the last printf in de C code, the expression (phase + 2) * 16 / 59. If yo=
u
know that phase can have values from 0 to 29, this expression (floored)
ranges from 0 to 8. This can never be, since the sequence 'description'
only has got 8 values. In C these are 0 to 7, and in Euphoria 1 to 8. Can=

someone provide me with a better solution?
Further comments are also welcome.

Sincerely,

Ad Rienks
email Ad_Rienks at compuserve.com
writing at 23:07 , =

on maandag 6 oktober 1997
Using EMail Assist for WinCIM


----This is the program moon_age.c      ----
/* PD by Michelangelo Jones, 1:1/124. */

/*
**  Returns 0 for new moon, 15 for full moon,
**  29 for the day before new, and so forth.
*/

/*
**  This routine sometimes gets "off" by a few days,
**  but is self-correcting.
*/

int moon_age(int month, int day, int year)
{
      static short int ages[] =3D
            {18, 0, 11, 22, 3, 14, 25, 6, 17,
             28, 9, 20, 1, 12, 23, 4, 15, 26, 7};
      static short int offsets[] =3D
            {-1, 1, 0, 1, 2, 3, 4, 5, 7, 7, 9, 9};

      if (day =3D=3D 31)
            day =3D 1;
      return ((ages[(year + 1) % 19] + ((day + offsets[month-1]) % 30) +
            (year < 1900)) % 30);
}

#ifdef TEST

#include <stdio.h>
#include <stdlib.h>

static char *description[] =3D {
      "new",                  /* totally dark                         */
      "waxing crescent",      /* increasing to full & quarter light   */
      "in its first quarter", /* increasing to full & half light      */
      "waxing gibbous",       /* increasing to full & > than half     */
      "full",                 /* fully lighted                        */
      "waning gibbous",       /* decreasing from full & > than half   */
      "in its last quarter",  /* decreasing from full & half light    */
      "waning crescent"       /* decreasing from full & quarter light */
      };

static char *months[] =3D {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
                         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

int main(int argc, char *argv[])
{
      int month, day, year, phase;

      if (4 > argc)
      {
            puts("Usage: MOON_AGE month day year");
            return EXIT_FAILURE;
      }
      month =3D atoi(argv[1]);
      day   =3D atoi(argv[2]);
      year  =3D atoi(argv[3]);
      if (100 > year)
            year +=3D 1900;
      printf("moon_age(%d, %d, %d) returned %d\n", month, day, year,
            phase =3D moon_age(month, day, year));
      printf("Moon phase on %d %s %d is %s\n", day, months[month - 1],
year,
            description[(int)((phase + 2) * 16L / 59L)]);
}

#endif /* TEST */

---- And this is the EUPHORIA moon_age.ex program       ------
/* PD by Michelangelo Jones, 1:1/124. */
--
--/*
--**  Returns 0 for new moon, 15 for full moon,
--**  29 for the day before new, and so forth.
--*/
--
--/*
--**  This routine sometimes gets "off" by a few days,
--**  but is self-correcting.
--*/

include get.e

function moon_age(integer month, integer day, integer year)
integer result
sequence ages, offsets
    ages =3D {18, 0, 11, 22, 3, 14, 25, 6, 17,
            28, 9, 20, 1, 12, 23, 4, 15, 26, 7}
    offsets =3D {-1, 1, 0, 1, 2, 3, 4, 5, 7, 7, 9, 9}
    if day =3D 31 then
        day =3D 1
    end if
    result =3D ages[remainder(year + 1, 19) + 1]
    result =3D result + remainder(day + offsets[month], 30)
    result =3D result + (year < 1900)
    result =3D remainder(result, 30)
    return result
end function    -- moon_age

function input(sequence prompt)
object nummer
    puts(1, prompt)
    nummer =3D get(0)
    nummer =3D nummer[2]
    return nummer
end function    -- input

constant description =3D {
      "new",                  --/* totally dark                         *=
;
      "waxing crescent",      --/* increasing to full & quarter light   *=
;
      "in its first quarter", --/* increasing to full & half light      *=
;
      "waxing gibbous",       --/* increasing to full & > than half     *=
;
      "full",                 --/* fully lighted                        *=
;
      "waning gibbous",       --/* decreasing from full & > than half   *=
;
      "in its last quarter",  --/* decreasing from full & half light    *=
;
      "waning crescent"       --/* decreasing from full & quarter light *=
;
      }

constant months =3D {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
                    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}

integer day, month, year, phase

while 1 do =

    clear_screen()
    month =3D input("Month: ")
    if month =3D 0 then
         exit
    end if
    day =3D input("\nDay: ")
    year =3D input("\nYear: ")
    if 100 > year then
        year =3D year + 1900
    end if
    phase =3D moon_age(month, day, year)
    printf(1, "\nmoon_age(%d, %d, %d) returned %d\n", {month, day, year,
phase})
    phase =3D floor((phase + 2) * 16 / 59) + 1
    if phase > 8 then
        phase =3D 8
    end if
    printf(1, "Moon phase on %d %s %d is %s\n", {day, months[month], year=
,
                description[phase]})
    while get_key() =3D -1 do     =

    end while
end while

new topic     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu