home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume11 / rpl / part02 / logcmd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-03-10  |  2.2 KB  |  134 lines

  1. /****************************************************************
  2.  
  3. Module:
  4.     LogCmd
  5.  
  6. Description:
  7.     Commands related to logarithms (The LOGS menu on HP28C)
  8.  
  9. Modification history:
  10.  
  11.     0.0    hjp    89-12-03
  12.  
  13.         initial version
  14.         LN and EXP extracted from ArithCmd.
  15.  
  16. ****************************************************************/
  17.  
  18. #include "errors.h"
  19. #include "globvar.h"
  20. #include "rpl.h"
  21. #include "intcmd.h"
  22. #include "logcmd.h"
  23. #include "stackcmd.h"
  24.  
  25. /*
  26.     ln    --    compute natural logarithm of object in level 1
  27.  
  28.     x    ->    ln(x)
  29.  
  30.     REAL    ->    REAL
  31.     REAL    ->    COMPLEX
  32.     COMPLEX ->    COMPLEX
  33. */
  34.  
  35. void c_ln (void)
  36. {
  37.     genobj        * a;
  38.     realobj     * c;
  39.     complexobj    * b;
  40.  
  41.     if (! stack) {
  42.         error ("ln", ERR_2FEWARG);
  43.         return;
  44.     }
  45.  
  46.     if ((a = stack->obj)->id == REAL) {
  47.         if (((realobj *)a)->val >= 0.0) {
  48.             if (!(c = mallocobj (REAL)))
  49.             {
  50.                 error ("LN", ERR_NOMEM);
  51.                 return;
  52.             }
  53.             c->id = REAL;
  54.             c->link = 0;
  55.             c->size = sizeof (realobj);
  56.             c->val = log (((realobj *) a)->val);
  57.             c_drop ();
  58.             push (c);
  59.         } else {
  60.             if (! (b = mallocobj (COMPLEX)))
  61.             {
  62.                 error ("LN", ERR_NOMEM);
  63.                 return;
  64.             }
  65.             b->val.x = log (-((realobj *) a)->val);
  66.             b->val.y = M_PI;
  67.             c_drop ();
  68.             push (b);
  69.         }
  70.     } else if (a->id == COMPLEX) {
  71.         double    x = ((complexobj *) a)->val.x,
  72.             y = ((complexobj *) a)->val.y;
  73.  
  74.         if (! (b = mallocobj (COMPLEX)))
  75.         {
  76.             error ("LN", ERR_NOMEM);
  77.             return;
  78.         }
  79.         b->val.x = log (sqrt (x * x + y * y));
  80.         b->val.y = x || y ? atan2 (y, x) : 0.0;
  81.         c_drop ();
  82.         push (b);
  83.     } else {
  84.         error ("inv", ERR_WRTYPE);
  85.     }
  86. }
  87.  
  88. /*
  89.     EXP    compute e to the power of object in level 1
  90.  
  91.     x    ->    e ^ x
  92.  
  93.     real    ->    real
  94.     complex ->    complex
  95. */
  96.  
  97. void c_exp (void)
  98. {
  99.     genobj        * a;
  100.     realobj     * c;
  101.     complexobj    * b;
  102.  
  103.     if (! stack) {
  104.         error ("EXP", ERR_2FEWARG);
  105.         return;
  106.     }
  107.  
  108.     if ((a = stack->obj)->id == REAL) {
  109.         if (!(c = mallocobj (REAL)))
  110.         {
  111.             error ("EXP", ERR_NOMEM);
  112.             return;
  113.         }
  114.         c->val = exp (((realobj *) a)->val);
  115.         c_drop ();
  116.         push (c);
  117.     } else if (a->id == COMPLEX) {
  118.         double    x = ((complexobj *) a)->val.x,
  119.             y = ((complexobj *) a)->val.y;
  120.  
  121.         if (! (b = mallocobj (COMPLEX)))
  122.         {
  123.             error ("EXP", ERR_NOMEM);
  124.             return;
  125.         }
  126.         b->val.x = exp (x) * cos (y);
  127.         b->val.y = exp (x) * sin (y);
  128.         c_drop ();
  129.         push (b);
  130.     } else {
  131.         error ("EXP", ERR_WRTYPE);
  132.     }
  133. }
  134.