home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume19 / xephem / part08 / sex_dec.c < prev   
Encoding:
C/C++ Source or Header  |  1993-05-15  |  1.1 KB  |  62 lines

  1. #include <math.h>
  2.  
  3. /* given hours (or degrees), hd, minutes, m, and seconds, s, 
  4.  * return decimal hours (or degrees), *d.
  5.  * in the case of hours (angles) < 0, only the first non-zero element should
  6.  *   be negative.
  7.  */
  8. void
  9. sex_dec (hd, m, s, d)
  10. int hd, m, s;
  11. double *d;
  12. {
  13.     int sign = 1;
  14.  
  15.     if (hd < 0) {
  16.         sign = -1;
  17.         hd = -hd;
  18.     } else if (m < 0) {
  19.         sign = -1;
  20.         m = -m;
  21.     } else if (s < 0) {
  22.         sign = -1;
  23.         s = -s;
  24.     }
  25.  
  26.     *d = (((double)s/60.0 + (double)m)/60.0 + (double)hd) * sign;
  27. }
  28.  
  29. /* given decimal hours (or degrees), d.
  30.  * return nearest hours (or degrees), *hd, minutes, *m, and seconds, *s, 
  31.  * each always non-negative; *isneg is set to 1 if d is < 0, else to 0.
  32.  */
  33. void
  34. dec_sex (d, hd, m, s, isneg)
  35. double d;
  36. int *hd, *m, *s, *isneg;
  37. {
  38.     double min;
  39.  
  40.     if (d < 0) {
  41.         *isneg = 1;
  42.         d = -d;
  43.     } else
  44.         *isneg = 0;
  45.  
  46.     *hd = (int)d;
  47.     min = (d - *hd)*60.;
  48.     *m = (int)min;
  49.     *s = (int)((min - *m)*60. + 0.5);
  50.  
  51.     if (*s == 60) {
  52.         if ((*m += 1) == 60) {
  53.         *hd += 1;
  54.         *m = 0;
  55.         }
  56.         *s = 0;
  57.     }
  58.     /* no  negative 0's */
  59.     if (*hd == 0 && *m == 0 && *s == 0)
  60.         *isneg = 0;
  61. }
  62.