Hey,

I have a few patches here that spruce up pom(6).

First up: use more libm.

adj360() is a modulo operation, so we ought to leverage fmod(3).

Adding 0.5 to the phase here to "steer" integer truncation toward
the desired principal phase classification is hokey.  Using round(3)
with its documented and, in this case, equivalent behavior is much
clearer.

thoughts?  ok?

--
Scott Cheloha

Index: games/pom/pom.c
===================================================================
RCS file: /cvs/src/games/pom/pom.c,v
retrieving revision 1.26
diff -u -p -r1.26 pom.c
--- games/pom/pom.c     23 Dec 2017 20:53:07 -0000      1.26
+++ games/pom/pom.c     23 Dec 2017 21:35:21 -0000
@@ -96,28 +96,24 @@ main(int argc, char *argv[])
        /* Selected time could be before EPOCH */
        for (cnt = GMT->tm_year; cnt < EPOCH; ++cnt)
                days -= isleap(cnt + 1900) ? 366 : 365;
-       today = potm(days) + 0.5;
+       today = potm(days);
        (void)printf("The Moon is ");
-       if ((int)today == 100)
+       if (round(today) == 100.0)
                (void)printf("Full\n");
-       else if (!(int)today)
+       else if (round(today) == 0.0)
                (void)printf("New\n");
        else {
                tomorrow = potm(days + 1);
-               if ((int)today == 50)
+               if (round(today) == 50.0)
                        (void)printf("%s\n", tomorrow > today ?
                            "at the First Quarter" : "at the Last Quarter");
-                       /* today is 0.5 too big, but it doesn't matter here
-                        * since the phase is changing fast enough
-                        */
                else {
-                       today -= 0.5;           /* Now it might matter */
                        (void)printf("%s ", tomorrow > today ?
                            "Waxing" : "Waning");
-                       if (today > 50)
+                       if (today >= 50.0)
                                (void)printf("Gibbous (%1.0f%% of Full)\n",
                                    today);
-                       else if (today < 50)
+                       else
                                (void)printf("Crescent (%1.0f%% of Full)\n",
                                    today);
                }
@@ -178,13 +174,9 @@ dtor(double deg)
 void
 adj360(double *deg)
 {
-       for (;;)
-               if (*deg < 0.0)
-                       *deg += 360.0;
-               else if (*deg > 360.0)
-                       *deg -= 360.0;
-               else
-                       break;
+       *deg = fmod(*deg, 360.0);
+       if (*deg < 0.0)
+               *deg += 360.0;
 }
 
 #define        ATOI2(ar)       ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 
2;

Reply via email to