home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Applications / Gnuplot 3.5 / source / standard.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-11  |  20.0 KB  |  990 lines  |  [TEXT/R*ch]

  1. #ifndef lint
  2. static char *RCSid = "$Id: standard.c%v 3.50 1993/07/09 05:35:24 woo Exp $";
  3. #endif
  4.  
  5.  
  6. /* GNUPLOT - standard.c */
  7. /*
  8.  * Copyright (C) 1986 - 1993   Thomas Williams, Colin Kelley
  9.  *
  10.  * Permission to use, copy, and distribute this software and its
  11.  * documentation for any purpose with or without fee is hereby granted, 
  12.  * provided that the above copyright notice appear in all copies and 
  13.  * that both that copyright notice and this permission notice appear 
  14.  * in supporting documentation.
  15.  *
  16.  * Permission to modify the software is granted, but not the right to
  17.  * distribute the modified code.  Modifications are to be distributed 
  18.  * as patches to released version.
  19.  *  
  20.  * This software is provided "as is" without express or implied warranty.
  21.  * 
  22.  *
  23.  * AUTHORS
  24.  * 
  25.  *   Original Software:
  26.  *     Thomas Williams,  Colin Kelley.
  27.  * 
  28.  *   Gnuplot 2.0 additions:
  29.  *       Russell Lang, Dave Kotz, John Campbell.
  30.  *
  31.  *   Gnuplot 3.0 additions:
  32.  *       Gershon Elber and many others.
  33.  * 
  34.  */
  35.  
  36. #include <math.h>
  37. #include <stdio.h>
  38. #include "plot.h"
  39. #ifdef THINK_C
  40. #include "tout_protos.h"
  41. #endif
  42.  
  43. #ifdef vms
  44. #include <errno.h>
  45. #else
  46. extern int errno;
  47. #endif /* vms */
  48.  
  49.  
  50. extern struct value stack[STACK_DEPTH];
  51. extern int s_p;
  52. extern double zero;
  53.  
  54. struct value *pop(), *Gcomplex(), *Ginteger();
  55.  
  56. double magnitude(), angle(), real(), imag();
  57.  
  58. /* The bessel function approximations here are from
  59.  * "Computer Approximations"
  60.  * by Hart, Cheney et al.
  61.  * John Wiley & Sons, 1968
  62.  */
  63.  
  64. /* There appears to be a mistake in Hart, Cheney et al. on page 149.
  65.  * Where it list Qn(x)/x ~ P(z*z)/Q(z*z), z = 8/x, it should read
  66.  *               Qn(x)/z ~ P(z*z)/Q(z*z), z = 8/x
  67.  * In the functions below, Qn(x) is implementated using the later
  68.  * equation.
  69.  * These bessel functions are accurate to about 1e-13
  70.  */
  71.  
  72. #if defined (ATARI) && defined(__PUREC__)
  73. /* Sorry. But PUREC bugs here.
  74.  * These bessel functions are NOT accurate to about 1e-13
  75.  */
  76.  
  77. #define PI_ON_FOUR     0.785398163397448309615661
  78. #define PI_ON_TWO     1.570796326794896619231313
  79. #define THREE_PI_ON_FOUR 2.356194490192344928846982
  80. #define TWO_ON_PI     0.636619772367581343075535
  81.  
  82. static double dzero = 0.0;
  83.  
  84. /* jzero for x in [0,8]
  85.  * Index 5849, 19.22 digits precision
  86.  */
  87. static double pjzero[] = {
  88.      0.493378725179413356181681e+21,
  89.     -0.117915762910761053603844e+21,
  90.      0.638205934107235656228943e+19,
  91.     -0.136762035308817138686542e+18,
  92.      0.143435493914034611166432e+16,
  93.     -0.808522203485379387119947e+13,
  94.      0.250715828553688194555516e+11,
  95.     -0.405041237183313270636066e+8,
  96.      0.268578685698001498141585e+5
  97. };
  98.  
  99. static double qjzero[] = {
  100.      0.493378725179413356211328e+21,
  101.      0.542891838409228516020019e+19,
  102.      0.302463561670946269862733e+17,
  103.      0.112775673967979850705603e+15,
  104.      0.312304311494121317257247e+12,
  105.      0.669998767298223967181403e+9,
  106.      0.111463609846298537818240e+7,
  107.      0.136306365232897060444281e+4,
  108.      0.1e+1
  109. };
  110.  
  111. /* pzero for x in [8,inf]
  112.  * Index 6548, 18.16 digits precision
  113.  */
  114. static double ppzero[] = {
  115.      0.227790901973046843022700e+5,
  116.      0.413453866395807657967802e+5,
  117.      0.211705233808649443219340e+5,
  118.      0.348064864432492703474453e+4,
  119.      0.153762019090083542957717e+3,
  120.      0.889615484242104552360748e+0
  121. };
  122.  
  123. static double qpzero[] = {
  124.      0.227790901973046843176842e+5,
  125.      0.413704124955104166398920e+5,
  126.      0.212153505618801157304226e+5,
  127.      0.350287351382356082073561e+4,
  128.      0.157111598580808936490685e+3,
  129.      0.1e+1
  130. };
  131.  
  132. /* qzero for x in [8,inf]
  133.  * Index 6948, 18.33 digits precision
  134.  */
  135. static double pqzero[] = {
  136.     -0.892266002008000940984692e+2,
  137.     -0.185919536443429938002522e+3,
  138.     -0.111834299204827376112621e+3,
  139.     -0.223002616662141984716992e+2,
  140.     -0.124410267458356384591379e+1,
  141.     -0.8803330304868075181663e-2,
  142. };
  143.  
  144. static double qqzero[] = {
  145.      0.571050241285120619052476e+4,
  146.      0.119511315434346136469526e+5,
  147.      0.726427801692110188369134e+4,
  148.      0.148872312322837565816135e+4,
  149.      0.905937695949931258588188e+2,
  150.      0.1e+1
  151. };
  152.  
  153.  
  154. /* yzero for x in [0,8]
  155.  * Index 6245, 18.78 digits precision
  156.  */
  157. static double pyzero[] = {
  158.     -0.275028667862910958370193e+20,
  159.      0.658747327571955492599940e+20,
  160.     -0.524706558111276494129735e+19,
  161.      0.137562431639934407857134e+18,
  162.     -0.164860581718572947312208e+16,
  163.      0.102552085968639428450917e+14,
  164.     -0.343637122297904037817103e+11,
  165.      0.591521346568688965427383e+8,
  166.     -0.413703549793314855412524e+5
  167. };
  168.  
  169. static double qyzero[] = {
  170.      0.372645883898616588198998e+21,
  171.      0.419241704341083997390477e+19,
  172.      0.239288304349978185743936e+17,
  173.      0.916203803407518526248915e+14,
  174.      0.261306575504108124956848e+12,
  175.      0.579512264070072953738009e+9,
  176.      0.100170264128890626566665e+7,
  177.      0.128245277247899380417633e+4,
  178.      0.1e+1
  179. };
  180.  
  181.  
  182. /* jone for x in [0,8]
  183.  * Index 6050, 20.98 digits precision
  184.  */
  185. static double pjone[] = {
  186.      0.581199354001606143928051e+21,
  187.     -0.667210656892491629802094e+20,
  188.      0.231643358063400229793182e+19,
  189.     -0.358881756991010605074364e+17,
  190.      0.290879526383477540973760e+15,
  191.     -0.132298348033212645312547e+13,
  192.      0.341323418230170053909129e+10,
  193.     -0.469575353064299585976716e+7,
  194.      0.270112271089232341485679e+4
  195. };
  196.  
  197. static double qjone[] = {
  198.      0.116239870800321228785853e+22,
  199.      0.118577071219032099983711e+20,
  200.      0.609206139891752174610520e+17,
  201.      0.208166122130760735124018e+15,
  202.      0.524371026216764971540673e+12,
  203.      0.101386351435867398996705e+10,
  204.      0.150179359499858550592110e+7,
  205.      0.160693157348148780197092e+4,
  206.      0.1e+1
  207. };
  208.  
  209.  
  210. /* pone for x in [8,inf]
  211.  * Index 6749, 18.11 digits precision
  212.  */
  213. static double ppone[] = {
  214.      0.352246649133679798341724e+5,
  215.      0.627588452471612812690057e+5,
  216.      0.313539631109159574238670e+5,
  217.      0.498548320605943384345005e+4,
  218.      0.211152918285396238210572e+3,
  219.      0.12571716929145341558495e+1
  220. };
  221.  
  222. static double qpone[] = {
  223.      0.352246649133679798068390e+5,
  224.      0.626943469593560511888834e+5,
  225.      0.312404063819041039923016e+5,
  226.      0.493039649018108897938610e+4,
  227.      0.203077518913475932229357e+3,
  228.      0.1e+1
  229. };
  230.  
  231. /* qone for x in [8,inf]
  232.  * Index 7149, 18.28 digits precision
  233.  */
  234. static double pqone[] = {
  235.      0.351175191430355282253332e+3,
  236.      0.721039180490447503928086e+3,
  237.      0.425987301165444238988699e+3,
  238.      0.831898957673850827325226e+2,
  239.      0.45681716295512267064405e+1,
  240.      0.3532840052740123642735e-1
  241. };
  242.  
  243. static double qqone[] = {
  244.      0.749173741718091277145195e+4,
  245.      0.154141773392650970499848e+5,
  246.      0.915223170151699227059047e+4,
  247.      0.181118670055235135067242e+4,
  248.      0.103818758546213372877664e+3,
  249.      0.1e+1
  250. };
  251.  
  252.  
  253. /* yone for x in [0,8]
  254.  * Index 6444, 18.24 digits precision
  255.  */
  256. static double pyone[] = {
  257.     -0.292382196153296254310105e+20,
  258.      0.774852068218683964508809e+19,
  259.     -0.344104806308411444618546e+18,
  260.      0.591516076049007061849632e+16,
  261.     -0.486331694256717507482813e+14,
  262.      0.204969667374566218261980e+12,
  263.     -0.428947196885524880182182e+9,
  264.      0.355692400983052605669132e+6
  265. };
  266.  
  267. static double qyone[] = {
  268.      0.149131151130292035017408e+21,
  269.      0.181866284170613498688507e+19,
  270.      0.113163938269888452690508e+17,
  271.      0.475517358888813771309277e+14,
  272.      0.150022169915670898716637e+12,
  273.      0.371666079862193028559693e+9,
  274.      0.726914730719888456980191e+6,
  275.      0.107269614377892552332213e+4,
  276.      0.1e+1
  277. };
  278.  
  279. #else
  280.  
  281. #define PI_ON_FOUR       0.78539816339744830961566084581987572
  282. #define PI_ON_TWO        1.57079632679489661923131269163975144
  283. #define THREE_PI_ON_FOUR 2.35619449019234492884698253745962716
  284. #define TWO_ON_PI        0.63661977236758134307553505349005744
  285.  
  286. static double dzero = 0.0;
  287.  
  288. /* jzero for x in [0,8]
  289.  * Index 5849, 19.22 digits precision
  290.  */
  291. static double pjzero[] = {
  292.      0.4933787251794133561816813446e+21,
  293.     -0.11791576291076105360384408e+21,
  294.      0.6382059341072356562289432465e+19,
  295.     -0.1367620353088171386865416609e+18,
  296.      0.1434354939140346111664316553e+16,
  297.     -0.8085222034853793871199468171e+13,
  298.      0.2507158285536881945555156435e+11,
  299.     -0.4050412371833132706360663322e+8,
  300.      0.2685786856980014981415848441e+5
  301. };
  302.  
  303. static double qjzero[] = {
  304.     0.4933787251794133562113278438e+21,
  305.     0.5428918384092285160200195092e+19,
  306.     0.3024635616709462698627330784e+17,
  307.     0.1127756739679798507056031594e+15,
  308.     0.3123043114941213172572469442e+12,
  309.     0.669998767298223967181402866e+9,
  310.     0.1114636098462985378182402543e+7,
  311.     0.1363063652328970604442810507e+4,
  312.     0.1e+1
  313. };
  314.  
  315. /* pzero for x in [8,inf]
  316.  * Index 6548, 18.16 digits precision
  317.  */
  318. static double ppzero[] = {
  319.     0.2277909019730468430227002627e+5,
  320.     0.4134538663958076579678016384e+5,
  321.     0.2117052338086494432193395727e+5,
  322.     0.348064864432492703474453111e+4,
  323.     0.15376201909008354295771715e+3,
  324.     0.889615484242104552360748e+0
  325. };
  326.  
  327. static double qpzero[] = {
  328.     0.2277909019730468431768423768e+5,
  329.     0.4137041249551041663989198384e+5,
  330.     0.2121535056188011573042256764e+5,
  331.     0.350287351382356082073561423e+4,
  332.     0.15711159858080893649068482e+3,
  333.     0.1e+1
  334. };
  335.  
  336. /* qzero for x in [8,inf]
  337.  * Index 6948, 18.33 digits precision
  338.  */
  339. static double pqzero[] = {
  340.     -0.8922660020080009409846916e+2,
  341.     -0.18591953644342993800252169e+3,
  342.     -0.11183429920482737611262123e+3,
  343.     -0.2230026166621419847169915e+2,
  344.     -0.124410267458356384591379e+1,
  345.     -0.8803330304868075181663e-2,
  346. };
  347.  
  348. static double qqzero[] = {
  349.     0.571050241285120619052476459e+4,
  350.     0.1195113154343461364695265329e+5,
  351.     0.726427801692110188369134506e+4,
  352.     0.148872312322837565816134698e+4,
  353.     0.9059376959499312585881878e+2,
  354.     0.1e+1
  355. };
  356.  
  357.  
  358. /* yzero for x in [0,8]
  359.  * Index 6245, 18.78 digits precision
  360.  */
  361. static double pyzero[] = {
  362.     -0.2750286678629109583701933175e+20,
  363.      0.6587473275719554925999402049e+20,
  364.     -0.5247065581112764941297350814e+19,
  365.      0.1375624316399344078571335453e+18,
  366.     -0.1648605817185729473122082537e+16,
  367.      0.1025520859686394284509167421e+14,
  368.     -0.3436371222979040378171030138e+11,
  369.      0.5915213465686889654273830069e+8,
  370.     -0.4137035497933148554125235152e+5
  371. };
  372.  
  373. static double qyzero[] = {
  374.     0.3726458838986165881989980739e+21,
  375.     0.4192417043410839973904769661e+19,
  376.     0.2392883043499781857439356652e+17,
  377.     0.9162038034075185262489147968e+14,
  378.     0.2613065755041081249568482092e+12,
  379.     0.5795122640700729537380087915e+9,
  380.     0.1001702641288906265666651753e+7,
  381.     0.1282452772478993804176329391e+4,
  382.     0.1e+1
  383. };
  384.  
  385.  
  386. /* jone for x in [0,8]
  387.  * Index 6050, 20.98 digits precision
  388.  */
  389. static double pjone[] = {
  390.      0.581199354001606143928050809e+21,
  391.     -0.6672106568924916298020941484e+20,
  392.      0.2316433580634002297931815435e+19,
  393.     -0.3588817569910106050743641413e+17,
  394.      0.2908795263834775409737601689e+15,
  395.     -0.1322983480332126453125473247e+13,
  396.      0.3413234182301700539091292655e+10,
  397.     -0.4695753530642995859767162166e+7,
  398.      0.270112271089232341485679099e+4
  399. };
  400.  
  401. static double qjone[] = {
  402.     0.11623987080032122878585294e+22,
  403.     0.1185770712190320999837113348e+20,
  404.     0.6092061398917521746105196863e+17,
  405.     0.2081661221307607351240184229e+15,
  406.     0.5243710262167649715406728642e+12,
  407.     0.1013863514358673989967045588e+10,
  408.     0.1501793594998585505921097578e+7,
  409.     0.1606931573481487801970916749e+4,
  410.     0.1e+1
  411. };
  412.  
  413.  
  414. /* pone for x in [8,inf]
  415.  * Index 6749, 18.11 digits precision
  416.  */
  417. static double ppone[] = {
  418.     0.352246649133679798341724373e+5,
  419.     0.62758845247161281269005675e+5,
  420.     0.313539631109159574238669888e+5,
  421.     0.49854832060594338434500455e+4,
  422.     0.2111529182853962382105718e+3,
  423.     0.12571716929145341558495e+1
  424. };
  425.  
  426. static double qpone[] = {
  427.     0.352246649133679798068390431e+5,
  428.     0.626943469593560511888833731e+5,
  429.     0.312404063819041039923015703e+5,
  430.     0.4930396490181088979386097e+4,
  431.     0.2030775189134759322293574e+3,
  432.     0.1e+1
  433. };
  434.  
  435. /* qone for x in [8,inf]
  436.  * Index 7149, 18.28 digits precision
  437.  */
  438. static double pqone[] = {
  439.     0.3511751914303552822533318e+3,
  440.     0.7210391804904475039280863e+3,
  441.     0.4259873011654442389886993e+3,
  442.     0.831898957673850827325226e+2,
  443.     0.45681716295512267064405e+1,
  444.     0.3532840052740123642735e-1
  445. };
  446.  
  447. static double qqone[] = {
  448.     0.74917374171809127714519505e+4,
  449.     0.154141773392650970499848051e+5,
  450.     0.91522317015169922705904727e+4,
  451.     0.18111867005523513506724158e+4,
  452.     0.1038187585462133728776636e+3,
  453.     0.1e+1
  454. };
  455.  
  456.  
  457. /* yone for x in [0,8]
  458.  * Index 6444, 18.24 digits precision
  459.  */
  460. static double pyone[] = {
  461.     -0.2923821961532962543101048748e+20,
  462.      0.7748520682186839645088094202e+19,
  463.     -0.3441048063084114446185461344e+18,
  464.      0.5915160760490070618496315281e+16,
  465.     -0.4863316942567175074828129117e+14,
  466.      0.2049696673745662182619800495e+12,
  467.     -0.4289471968855248801821819588e+9,
  468.      0.3556924009830526056691325215e+6
  469. };
  470.  
  471. static double qyone[] = {
  472.     0.1491311511302920350174081355e+21,
  473.     0.1818662841706134986885065935e+19,
  474.     0.113163938269888452690508283e+17,
  475.     0.4755173588888137713092774006e+14,
  476.     0.1500221699156708987166369115e+12,
  477.     0.3716660798621930285596927703e+9,
  478.     0.726914730719888456980191315e+6,
  479.     0.10726961437789255233221267e+4,
  480.     0.1e+1
  481. };
  482.  
  483. #endif /* ATARI && __PUREC__ */
  484.  
  485. f_real()
  486. {
  487. struct value a;
  488.     push( Gcomplex(&a,real(pop(&a)), 0.0) );
  489. }
  490.  
  491. f_imag()
  492. {
  493. struct value a;
  494.     push( Gcomplex(&a,imag(pop(&a)), 0.0) );
  495. }
  496.  
  497. f_arg()
  498. {
  499. struct value a;
  500.     push( Gcomplex(&a,angle(pop(&a)), 0.0) );
  501. }
  502.  
  503. f_conjg()
  504. {
  505. struct value a;
  506.     (void) pop(&a);
  507.     push( Gcomplex(&a,real(&a),-imag(&a) ));
  508. }
  509.  
  510. f_sin()
  511. {
  512. struct value a;
  513.     (void) pop(&a);
  514.     push( Gcomplex(&a,sin(real(&a))*cosh(imag(&a)), cos(real(&a))*sinh(imag(&a))) );
  515. }
  516.  
  517. f_cos()
  518. {
  519. struct value a;
  520.     (void) pop(&a);
  521.     push( Gcomplex(&a,cos(real(&a))*cosh(imag(&a)), -sin(real(&a))*sinh(imag(&a))));
  522. }
  523.  
  524. f_tan()
  525. {
  526. struct value a;
  527. register double den;
  528.     (void) pop(&a);
  529.     if (imag(&a) == 0.0)
  530.         push( Gcomplex(&a,tan(real(&a)),0.0) );
  531.     else {
  532.         den = cos(2*real(&a))+cosh(2*imag(&a));
  533.         if (den == 0.0) {
  534.             undefined = TRUE;
  535.             push( &a );
  536.         }
  537.         else
  538.             push( Gcomplex(&a,sin(2*real(&a))/den, sinh(2*imag(&a))/den) );
  539.     }
  540. }
  541.  
  542. f_asin()
  543. {
  544. struct value a;
  545. register double alpha, beta, x, y;
  546.     (void) pop(&a);
  547.     x = real(&a); y = imag(&a);
  548.     if (y == 0.0) {
  549.         if (fabs(x) > 1.0) {
  550.             undefined = TRUE;
  551.             push(Gcomplex(&a,0.0, 0.0));
  552.         } else
  553.             push( Gcomplex(&a,asin(x),0.0) );
  554.     } else {
  555.         beta  = sqrt((x + 1)*(x + 1) + y*y)/2 - sqrt((x - 1)*(x - 1) + y*y)/2;
  556.         alpha = sqrt((x + 1)*(x + 1) + y*y)/2 + sqrt((x - 1)*(x - 1) + y*y)/2;
  557.         push( Gcomplex(&a,asin(beta), log(alpha + sqrt(alpha*alpha-1))) );
  558.     }
  559. }
  560.  
  561. f_acos()
  562. {
  563. struct value a;
  564. register double alpha, beta, x, y;
  565.     (void) pop(&a);
  566.     x = real(&a); y = imag(&a);
  567.     if (y == 0.0) {
  568.         if (fabs(x) > 1.0) {
  569.             undefined = TRUE;
  570.             push(Gcomplex(&a,0.0, 0.0));
  571.         } else
  572.             push( Gcomplex(&a,acos(x),0.0) );
  573.     } else {
  574.         alpha = sqrt((x + 1)*(x + 1) + y*y)/2 + sqrt((x - 1)*(x - 1) + y*y)/2;
  575.         beta  = sqrt((x + 1)*(x + 1) + y*y)/2 - sqrt((x - 1)*(x - 1) + y*y)/2;
  576.         push( Gcomplex(&a,acos(beta), log(alpha + sqrt(alpha*alpha-1))) );
  577.     }
  578. }
  579.  
  580. f_atan()
  581. {
  582. struct value a;
  583. register double x, y, u, v, w, z;
  584.     (void) pop(&a);
  585.     x = real(&a); y = imag(&a);
  586.     if (y == 0.0)
  587.         push( Gcomplex(&a,atan(x), 0.0) );
  588.     else if (x == 0.0 && fabs(y) == 1.0) {
  589.         undefined = TRUE;
  590.         push(Gcomplex(&a,0.0, 0.0));
  591.     } else {
  592.             if (x >= 0) {
  593.                 u = x;
  594.             v = y;
  595.         } else {
  596.                 u = -x;
  597.             v = -y;
  598.         }
  599.         
  600.             z = atan(2*u/(1-u*u-v*v));
  601.         w = log((u*u+(v+1)*(v+1))/(u*u+(v-1)*(v-1)))/4;
  602.         if (z < 0)
  603.                 z = z + 2*PI_ON_TWO;
  604.         if (x < 0) {
  605.                 z = -z;
  606.             w = -w;
  607.         }
  608.         push( Gcomplex(&a,0.5*z, w) );
  609.     }
  610. }
  611.  
  612. f_sinh()
  613. {
  614. struct value a;
  615.     (void) pop(&a);
  616.     push( Gcomplex(&a,sinh(real(&a))*cos(imag(&a)), cosh(real(&a))*sin(imag(&a))) );
  617. }
  618.  
  619. f_cosh()
  620. {
  621. struct value a;
  622.     (void) pop(&a);
  623.     push( Gcomplex(&a,cosh(real(&a))*cos(imag(&a)), sinh(real(&a))*sin(imag(&a))) );
  624. }
  625.  
  626. f_tanh()
  627. {
  628. struct value a;
  629. register double den;
  630.     (void) pop(&a);
  631.     den = cosh(2*real(&a)) + cos(2*imag(&a));
  632.     push( Gcomplex(&a,sinh(2*real(&a))/den, sin(2*imag(&a))/den) );
  633. }
  634.  
  635. f_int()
  636. {
  637. struct value a;
  638.     push( Ginteger(&a,(int)real(pop(&a))) );
  639. }
  640.  
  641.  
  642. f_abs()
  643. {
  644. struct value a;
  645.     (void) pop(&a);
  646.     switch (a.type) {
  647.         case INTGR:
  648.             push( Ginteger(&a,abs(a.v.int_val)) );            
  649.             break;
  650.         case CMPLX:
  651.             push( Gcomplex(&a,magnitude(&a), 0.0) );
  652.     }
  653. }
  654.  
  655. f_sgn()
  656. {
  657. struct value a;
  658.     (void) pop(&a);
  659.     switch(a.type) {
  660.         case INTGR:
  661.             push( Ginteger(&a,(a.v.int_val > 0) ? 1 : 
  662.                     (a.v.int_val < 0) ? -1 : 0) );
  663.             break;
  664.         case CMPLX:
  665.             push( Ginteger(&a,(a.v.cmplx_val.real > 0.0) ? 1 : 
  666.                     (a.v.cmplx_val.real < 0.0) ? -1 : 0) );
  667.             break;
  668.     }
  669. }
  670.  
  671.  
  672. f_sqrt()
  673. {
  674. struct value a;
  675. register double mag, ang;
  676.     (void) pop(&a);
  677.     mag = sqrt(magnitude(&a));
  678.     if (imag(&a) == 0.0 && real(&a) < 0.0)
  679.         push( Gcomplex(&a,0.0,mag) );
  680.     else
  681.     {
  682.         if ( (ang = angle(&a)) < 0.0)
  683.             ang += 2*Pi;
  684.         ang /= 2;
  685.         push( Gcomplex(&a,mag*cos(ang), mag*sin(ang)) );
  686.     }
  687. }
  688.  
  689.  
  690. f_exp()
  691. {
  692. struct value a;
  693. register double mag, ang;
  694.     (void) pop(&a);
  695.     mag = exp(real(&a));
  696.     ang = imag(&a);
  697.     push( Gcomplex(&a,mag*cos(ang), mag*sin(ang)) );
  698. }
  699.  
  700.  
  701. f_log10()
  702. {
  703. struct value a;
  704. register double l10;;
  705.     (void) pop(&a);
  706.     l10 = log(10.0);    /***** replace with a constant! ******/
  707.     push( Gcomplex(&a,log(magnitude(&a))/l10, angle(&a)/l10) );
  708. }
  709.  
  710.  
  711. f_log()
  712. {
  713. struct value a;
  714.     (void) pop(&a);
  715.     push( Gcomplex(&a,log(magnitude(&a)), angle(&a)) );
  716. }
  717.  
  718.  
  719. f_floor()
  720. {
  721. struct value a;
  722.  
  723.     (void) pop(&a);
  724.     switch (a.type) {
  725.         case INTGR:
  726.             push( Ginteger(&a,(int)floor((double)a.v.int_val)));            
  727.             break;
  728.         case CMPLX:
  729.             push( Ginteger(&a,(int)floor(a.v.cmplx_val.real)));
  730.     }
  731. }
  732.  
  733.  
  734. f_ceil()
  735. {
  736. struct value a;
  737.  
  738.     (void) pop(&a);
  739.     switch (a.type) {
  740.         case INTGR:
  741.             push( Ginteger(&a,(int)ceil((double)a.v.int_val)));            
  742.             break;
  743.         case CMPLX:
  744.             push( Ginteger(&a,(int)ceil(a.v.cmplx_val.real)));
  745.     }
  746. }
  747.  
  748. /* bessel function approximations */
  749. double jzero(x)
  750. double x;
  751. {
  752. double p, q, x2;
  753. int n;
  754.  
  755.     x2 = x * x;
  756.     p = pjzero[8];
  757.     q = qjzero[8];
  758.     for (n=7; n>=0; n--) {
  759.         p = p*x2 + pjzero[n];
  760.         q = q*x2 + qjzero[n];
  761.     }
  762.     return(p/q);
  763. }
  764.  
  765. double pzero(x)
  766. double x;
  767. {
  768. double p, q, z, z2;
  769. int n;
  770.  
  771.     z = 8.0 / x;
  772.     z2 = z * z;
  773.     p = ppzero[5];
  774.     q = qpzero[5];
  775.     for (n=4; n>=0; n--) {
  776.         p = p*z2 + ppzero[n];
  777.         q = q*z2 + qpzero[n];
  778.     }
  779.     return(p/q);
  780. }
  781.  
  782. double qzero(x)
  783. double x;
  784. {
  785. double p, q, z, z2;
  786. int n;
  787.  
  788.     z = 8.0 / x;
  789.     z2 = z * z;
  790.     p = pqzero[5];
  791.     q = qqzero[5];
  792.     for (n=4; n>=0; n--) {
  793.         p = p*z2 + pqzero[n];
  794.         q = q*z2 + qqzero[n];
  795.     }
  796.     return(p/q);
  797. }
  798.  
  799. double yzero(x)
  800. double x;
  801. {
  802. double p, q, x2;
  803. int n;
  804.  
  805.     x2 = x * x;
  806.     p = pyzero[8];
  807.     q = qyzero[8];
  808.     for (n=7; n>=0; n--) {
  809.         p = p*x2 + pyzero[n];
  810.         q = q*x2 + qyzero[n];
  811.     }
  812.     return(p/q);
  813. }
  814.  
  815. double rj0(x)
  816. double x;
  817. {
  818.     if ( x <= 0.0 )
  819.         x = -x;
  820.     if ( x < 8.0 )
  821.         return(jzero(x));
  822.     else
  823.         return( sqrt(TWO_ON_PI/x) *
  824.             (pzero(x)*cos(x-PI_ON_FOUR) - 8.0/x*qzero(x)*sin(x-PI_ON_FOUR)) );
  825.  
  826. }
  827.  
  828. double ry0(x)
  829. double x;
  830. {
  831.     if ( x < 0.0 )
  832.         return(dzero/dzero); /* error */
  833.     if ( x < 8.0 )
  834.         return( yzero(x) + TWO_ON_PI*rj0(x)*log(x) );
  835.     else
  836.         return( sqrt(TWO_ON_PI/x) *
  837.             (pzero(x)*sin(x-PI_ON_FOUR) + 
  838.             (8.0/x)*qzero(x)*cos(x-PI_ON_FOUR)) );
  839.  
  840. }
  841.  
  842.  
  843. double jone(x)
  844. double x;
  845. {
  846. double p, q, x2;
  847. int n;
  848.  
  849.     x2 = x * x;
  850.     p = pjone[8];
  851.     q = qjone[8];
  852.     for (n=7; n>=0; n--) {
  853.         p = p*x2 + pjone[n];
  854.         q = q*x2 + qjone[n];
  855.     }
  856.     return(p/q);
  857. }
  858.  
  859. double pone(x)
  860. double x;
  861. {
  862. double p, q, z, z2;
  863. int n;
  864.  
  865.     z = 8.0 / x;
  866.     z2 = z * z;
  867.     p = ppone[5];
  868.     q = qpone[5];
  869.     for (n=4; n>=0; n--) {
  870.         p = p*z2 + ppone[n];
  871.         q = q*z2 + qpone[n];
  872.     }
  873.     return(p/q);
  874. }
  875.  
  876. double qone(x)
  877. double x;
  878. {
  879. double p, q, z, z2;
  880. int n;
  881.  
  882.     z = 8.0 / x;
  883.     z2 = z * z;
  884.     p = pqone[5];
  885.     q = qqone[5];
  886.     for (n=4; n>=0; n--) {
  887.         p = p*z2 + pqone[n];
  888.         q = q*z2 + qqone[n];
  889.     }
  890.     return(p/q);
  891. }
  892.  
  893. double yone(x)
  894. double x;
  895. {
  896. double p, q, x2;
  897. int n;
  898.  
  899.     x2 = x * x;
  900.     p = 0.0;
  901.     q = qyone[8];
  902.     for (n=7; n>=0; n--) {
  903.         p = p*x2 + pyone[n];
  904.         q = q*x2 + qyone[n];
  905.     }
  906.     return(p/q);
  907. }
  908.  
  909. double rj1(x)
  910. double x;
  911. {
  912. double v,w;
  913.     v = x;
  914.     if ( x < 0.0 )
  915.         x = -x;
  916.     if ( x < 8.0 )
  917.         return(v*jone(x));
  918.     else {
  919.         w = sqrt(TWO_ON_PI/x) *
  920.             (pone(x)*cos(x-THREE_PI_ON_FOUR) - 
  921.                8.0/x*qone(x)*sin(x-THREE_PI_ON_FOUR)) ;
  922.         if (v < 0.0)
  923.             w = -w;
  924.         return( w );
  925.     }
  926. }
  927.  
  928. double ry1(x)
  929. double x;
  930. {
  931.     if ( x <= 0.0 )
  932.         return(dzero/dzero); /* error */
  933.     if ( x < 8.0 )
  934.         return( x*yone(x) + TWO_ON_PI*(rj1(x)*log(x) - 1.0/x) );
  935.     else
  936.         return( sqrt(TWO_ON_PI/x) *
  937.             (pone(x)*sin(x-THREE_PI_ON_FOUR) + 
  938.             (8.0/x)*qone(x)*cos(x-THREE_PI_ON_FOUR)) );
  939. }
  940.  
  941.  
  942. f_besj0()    
  943. {
  944. struct value a;
  945.     (void) pop(&a);
  946.     if (fabs(imag(&a)) > zero)
  947.         int_error("can only do bessel functions of reals",NO_CARET);
  948.     push( Gcomplex(&a,rj0(real(&a)),0.0) );
  949. }
  950.  
  951.  
  952. f_besj1()    
  953. {
  954. struct value a;
  955.     (void) pop(&a);
  956.     if (fabs(imag(&a)) > zero)
  957.         int_error("can only do bessel functions of reals",NO_CARET);
  958.     push( Gcomplex(&a,rj1(real(&a)),0.0) );
  959. }
  960.  
  961.  
  962. f_besy0()    
  963. {
  964. struct value a;
  965.     (void) pop(&a);
  966.     if (fabs(imag(&a)) > zero)
  967.         int_error("can only do bessel functions of reals",NO_CARET);
  968.     if (real(&a) > 0.0)
  969.         push( Gcomplex(&a,ry0(real(&a)),0.0) );
  970.     else {
  971.         push( Gcomplex(&a,0.0,0.0) );
  972.         undefined = TRUE ;
  973.     }
  974. }
  975.  
  976.  
  977. f_besy1()    
  978. {
  979. struct value a;
  980.     (void) pop(&a);
  981.     if (fabs(imag(&a)) > zero)
  982.         int_error("can only do bessel functions of reals",NO_CARET);
  983.     if (real(&a) > 0.0)
  984.         push( Gcomplex(&a,ry1(real(&a)),0.0) );
  985.     else {
  986.         push( Gcomplex(&a,0.0,0.0) );
  987.         undefined = TRUE ;
  988.     }
  989. }
  990.