home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 13 / 13.iso / p / p024 / 12.img / ADS2.LIB / SQR.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-17  |  5.2 KB  |  181 lines

  1. /* Next available MSG number is   6 */
  2.  
  3. /*    SQR.C
  4.       ¬⌐┼v (C) 1990-1992  Autodesk ñ╜Ñq
  5.  
  6.       Ñ╗│n┼ΘºK╢O¿╤▒z╢iªµÑ⌠ª≤Ñ╬│~╗▌¿D¬║½■¿⌐íB¡╫º∩ñ╬╡oªµ, ª²¼O░╚╜╨┐φ┤`ñU¡z
  7.       ¡∞½h :
  8.  
  9.       1)  ñWªC¬║¬⌐┼v│qºi░╚╗▌ÑX▓{ªb¿Cñ@Ñ≈½■¿⌐∙╪íC
  10.       2)  ¼█├÷¬║╗í⌐·ñσÑ≤ñ]Ñ▓╢╖⌐·╕ⁿ¬⌐┼v│qºiñ╬Ñ╗╢╡│\Ñi│qºiíC
  11.  
  12.       Ñ╗│n┼Θ╢╚┤ú¿╤º@¼░└│Ñ╬ñW¬║░╤ª╥, ª╙Ñ╝┴n⌐·⌐╬┴⌠ºtÑ⌠ª≤½O├╥; ╣∩⌐≤Ñ⌠ª≤»S«φ
  13.       Ñ╬│~ñº╛A║┘⌐╩, ÑHñ╬░╙╖~╛P░Γ⌐╥┴⌠ºtÑX¿π¬║½O├╥, ªbª╣ñ@╖ºñ⌐ÑHº_╗{íC
  14.  
  15.  
  16.  **********************************************************************
  17.  * ADS SQUARE ROOT application - by Mark Barrow - 3/14/90
  18.  * Comments and revisions - Randy Clark, 4/23/90
  19.  **********************************************************************
  20.  */
  21.  
  22. #include  <stdio.h>
  23. #include  <math.h>
  24. #include  "adslib.h"
  25.  
  26. #define ExtFuncCount    1             /* Must increase this count if new
  27.                                          external functions are added */
  28.  
  29. int funcload _((void));
  30. int funcunload _((void));
  31. int dofun _((void));
  32.  
  33. char *exfun[] = {/*MSG0*/"sqr"};      /* No "C:" -- to be called as an AutoLISP
  34.                                          function, not as an AutoCAD command */
  35.  
  36. /* More functions could be added to this table.  For example:
  37.    char *exfun[] = {"C:sqr", "C:shaft", "PIN"}; */
  38.  
  39. /*-----------------------------------------------------------------------*/
  40. /* MAIN - the main routine */
  41.  
  42. void main(argc,argv)
  43.   int argc; char *argv[];
  44. {
  45.     short scode = RSRSLT;             /* Normal result code (default) */
  46.     int stat;
  47.  
  48.     ads_init(argc, argv);             /* Initiate communication with AutoLISP */
  49.  
  50.     for ( ;; ) {                      /* Request/Result loop */
  51.  
  52.         if ((stat = ads_link(scode)) < 0) {
  53.             printf(/*MSG1*/"SQRT: Ñ╤ ads_link() ╢╟ª^¬║ñú¿╬¬¼║A = %d\n", stat);
  54.             fflush(stdout);
  55.             exit(1);
  56.         }
  57.  
  58.         scode = RSRSLT;               /* Reset result code */
  59.  
  60.         switch (stat) {
  61.  
  62.         case RQXLOAD:                 /* Load & define functions */
  63.             scode = funcload() ? -RSRSLT : -RSERR;
  64.             break;
  65.  
  66.         case RQXUNLD:                 /* Unload functions */
  67.             scode = funcunload() ? -RSRSLT : -RSERR;
  68.             ads_printf(/*MSG2*/"─└⌐±íC\n");
  69.             break;
  70.  
  71.         case RQSUBR:                  /* Handle request for external function */
  72.             scode = dofun() ? RSRSLT : RSERR;
  73.             break;
  74.  
  75.         default:
  76.             break;
  77.         }
  78.     }
  79. }
  80. /*-----------------------------------------------------------------------*/
  81. /* FUNCLOAD  --  Define this application's external functions  */
  82.  
  83. int funcload()
  84. {
  85.     int i;
  86.     for (i = 0; i < ExtFuncCount; i++) {
  87.         if (!ads_defun(exfun[i], i))
  88.             return RTERROR;
  89.     }
  90.     return RTNORM;
  91. }
  92. /*-----------------------------------------------------------------------*/
  93. /* FUNCUNLOAD  --  Unload external functions */
  94.  
  95. int funcunload()
  96. {
  97.     int i;
  98.  
  99.     /* Undefine each function we defined */
  100.  
  101.     for (i = 0; i < ExtFuncCount; i++) {
  102.         ads_undef(exfun[i],i);
  103.     }
  104.     return RTNORM;
  105. }
  106. /*-----------------------------------------------------------------------*/
  107. /* DOFUN -- Execute external function (called upon an RQSUBR request) */
  108.  
  109. int dofun()
  110. {
  111.     struct resbuf *rb;
  112.     int val;
  113.     ads_real x;
  114.     extern ads_real rsqr _((ads_real));
  115.  
  116.     if ((val = ads_getfuncode()) < 0)
  117.         return 0;
  118.  
  119.     if ((rb = ads_getargs()) == NULL)
  120.         return 0;
  121.  
  122.     switch (val) {                    /* The 'sqr' function was defined
  123.                                          to have a funcode of 0 */
  124.  
  125.     case 0:
  126.         if (rb->restype == RTSHORT) {   /* Save in local variable */
  127.             x = (ads_real) rb->resval.rint;
  128.         } else if (rb->restype == RTREAL) {
  129.             x = rb->resval.rreal;     /* Can accept either real
  130.                                          or integer */
  131.         } else {
  132.             ads_fail(/*MSG3*/"ñ▐╝╞└│¼░íu╣Ω╝╞ív⌐╬íu╛π╝╞ívíC");
  133.             return 0;
  134.         }
  135.         if (x < 0) {                  /* Check argument range */
  136.             ads_fail(/*MSG4*/"íuñ▐╝╞ív└│¼░íuÑ┐¡╚ívíC");
  137.             return 0;
  138.         }
  139.  
  140.         ads_retreal(rsqr(x));         /* Call the function itself, and
  141.                                          return the value to AutoLISP */
  142.  
  143.         return 1;
  144.  
  145.     default:
  146.         ads_fail(/*MSG5*/"▒╡ª¼¿∞ñúªsªb¬║íuÑ\134»α¿τ╝╞╜XívíC");
  147.         return 0;
  148.     }
  149. }
  150. /*-----------------------------------------------------------------------*/
  151. /* This is the implementation of the actual external function */
  152.  
  153. ads_real rsqr(x)              /* Figure square root (Newton-Raphson method) */
  154.   ads_real x;
  155. {
  156.     ads_real y, c, cl;
  157.     int n;
  158.  
  159.     if (x == 0.0) {
  160.         return 0.0;
  161.     }
  162.  
  163. #ifdef __STDC__
  164.     y = frexp(x, &n);        /* split x to get exponent */
  165.     y = ldexp(1.0, n / 2);   /* good guess at sqrt */
  166.     n = 10;                  /* it will converge faster than this */
  167. #else
  168.     y = (0.154116 + 1.893872 * x) / (1.0 + 1.047988 * x);
  169.     /* It takes a lot of iterations to get sqrt(1E22) from
  170.        an initial guess of 1.9 */
  171.     n = 50;
  172. #endif
  173.     c = 0.0;
  174.     do {
  175.         cl = c;
  176.         c = (y - x / y) * 0.5;
  177.         y -=  c;
  178.     } while (c != cl && --n);
  179.     return y;
  180. }
  181.