arith10.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235
  1. /* arith10.c Copyright (C) 1990-96 Codemist Ltd */
  2. /*
  3. * Arithmetic functions.
  4. */
  5. /* Signature: 76591977 07-Mar-2000 */
  6. #include <stdarg.h>
  7. #include <string.h>
  8. #include <ctype.h>
  9. #include <math.h>
  10. #include "machine.h"
  11. #include "tags.h"
  12. #include "cslerror.h"
  13. #include "externs.h"
  14. #include "arith.h"
  15. #include "entries.h"
  16. #ifdef TIMEOUT
  17. #include "timeout.h"
  18. #endif
  19. /*****************************************************************************/
  20. /*** Transcendental functions etcetera. ***/
  21. /*****************************************************************************/
  22. /*
  23. * Much of the code here is extracted from the portable Fortran library
  24. * used by Codemist with its Fortran compiler.
  25. */
  26. #define CSL_log_2 0.6931471805599453094
  27. #ifdef COMMON
  28. static Complex MS_CDECL Cdiv_z(Complex, Complex);
  29. static Complex MS_CDECL cpowi(Complex z, int n)
  30. {
  31. /*
  32. * Raises a complex number to an integer power by repeated squaring.
  33. */
  34. if (n == 0)
  35. { Complex one;
  36. one.real = 1.0;
  37. one.imag = 0.0;
  38. return one;
  39. }
  40. else if (n < 0)
  41. { Complex one;
  42. one.real = 1.0;
  43. one.imag = 0.0;
  44. return Cdiv_z(one, cpowi(z, -n));
  45. }
  46. else if (n == 1) return z;
  47. else
  48. { Complex r = cpowi(z, n/2);
  49. double x1 = r.real, y1 = r.imag;
  50. double x2, y2;
  51. if (n & 1)
  52. { double x = z.real, y = z.imag;
  53. x2 = x*x1 - y*y1;
  54. y2 = x*y1 + y*x1;
  55. }
  56. else
  57. { x2 = x1;
  58. y2 = y1;
  59. }
  60. /*
  61. * This computes r = z^(n/2) and then evaluates either
  62. * r*r
  63. * or (r*z)*r
  64. * where the the calculation or (r*z) will not overflow unless the
  65. * final result is going to. Note that if the sum had been done as
  66. * (r*r)*z
  67. * then in some cases r*r might have overflowed even though r*r*z would
  68. * not (e.g. r*r giving a just-out-of-range real value, and having
  69. * magnitude between 1 and sqrt(2), and argument pi/4).
  70. * Although it is SHODDY, for at least the present I will not bother
  71. * about overflow in during the process of the final complex
  72. * multiplication. After all I suspect ALL complex multiplications
  73. * generated by the compiler will have the same limitation.
  74. */
  75. z.real = x1*x2 - y1*y2;
  76. z.imag = x1*y2 + y1*x2;
  77. return z;
  78. }
  79. }
  80. static Complex MS_CDECL csin(Complex z)
  81. {
  82. double x = z.real, y = z.imag;
  83. /*
  84. * sin(x + iy) = sin(x)*cosh(y) + i cos(x)*sinh(y)
  85. * For smallish y this can be used directly. For |y| > 50 I will
  86. * compute sinh and cosh as just +/- exp(|y|)/2
  87. */
  88. double s = sin(x), c = cos(x);
  89. double absy = fabs(y);
  90. if (absy <= 50.0)
  91. { double sh = sinh(y), ch = cosh(y);
  92. z.real = s * ch;
  93. z.imag = c * sh;
  94. return z;
  95. }
  96. else
  97. {
  98. double w;
  99. int n = _reduced_exp(absy, &w) - 1;
  100. z.real = ldexp(s*w, n);
  101. if (y < 0.0) z.imag = ldexp(-c*w, n);
  102. else z.imag = ldexp(c*w, n);
  103. return z;
  104. }
  105. }
  106. #define CSL_sqrt_starter 0.7071
  107. #define CSL_sqrt_iters 6
  108. static Complex MS_CDECL csqrt(Complex z)
  109. {
  110. double x = z.real, y = z.imag;
  111. /*
  112. *
  113. */
  114. int j, n;
  115. double scale;
  116. double vx, vy;
  117. if (y == 0.0)
  118. { if (x < 0.0)
  119. { z.real = 0.0;
  120. z.imag = sqrt(-x);
  121. }
  122. else
  123. { z.real = sqrt(x);
  124. z.imag = 0.0;
  125. }
  126. return z; /* Pure real arguments are easy */
  127. }
  128. (void)frexp(y, &n);
  129. /* Exact value returned by frexp not critical */
  130. if (x != 0.0)
  131. { int n1;
  132. (void)frexp(x, &n1);
  133. if (n1 > n) n = n1;
  134. }
  135. n &= ~1; /* ensure it is even */
  136. scale = ldexp(1.0, n);
  137. x = x / scale;
  138. y = y / scale; /* Now max(|x|, |y|) is in [0.5, 1) */
  139. /*
  140. * Generate some kind of starting approximation for a NR iteration.
  141. * The value selected here here will give a relative error of 1.6e-5
  142. * after 4 iterations, and so would give about 6.0e-20 after 6, which
  143. * is more accurate (just) than the machines that I use can cope with.
  144. * Note that for z near the real axis the starting approximation is
  145. * either real or (pure) imaginary, thus helping ensure that the near-
  146. * zero component of the answer comes out to decent accuracy.
  147. */
  148. if (y < x)
  149. { if (x > -y) vx = CSL_sqrt_starter, vy = 0.0;
  150. else vx = CSL_sqrt_starter, vy = -CSL_sqrt_starter;
  151. }
  152. else if (x > -y) vx = CSL_sqrt_starter, vy = CSL_sqrt_starter;
  153. else if (y > 0.0) vx = 0.0, vy = CSL_sqrt_starter;
  154. else vx = 0.0, vy = -CSL_sqrt_starter;
  155. /*
  156. * For starting values as preconditioned here 6 iterations will converge
  157. * to about adequate accuracy. For some arguments fewer iterations would
  158. * suffice, but I am taking the view that the cost of providing a more
  159. * elaborate end-test might well exceed the cost of the extra iterations
  160. * that this code performs. Experiment shows that the worst relative
  161. * error after 4 iterations is 1.3e-5, so after 6 it will be about
  162. * 3.0e-20, which is better than machine accuracy. 5 iterations would
  163. * not be enough.
  164. */
  165. for (j=0; j<CSL_sqrt_iters; j++)
  166. { double t = vx*vx + vy*vy;
  167. double qx = (x*vx + y*vy)/t,
  168. qy = (y*vx - x*vy)/t;
  169. vx = (vx + qx)*0.5;
  170. vy = (vy + qy)*0.5;
  171. }
  172. n = n/2;
  173. z.real = ldexp(vx, n);
  174. z.imag = ldexp(vy, n);
  175. return z;
  176. }
  177. #undef CSL_sqrt_starter
  178. #undef CSL_sqrt_iters
  179. static Complex MS_CDECL ctan(Complex z)
  180. {
  181. double x = z.real, y = z.imag;
  182. /*
  183. * tan(x + iy) = (tan(x) + i tanh(y))/(1 - i tan(x)*tanh(y))
  184. */
  185. double t = tan(x), th = tanh(y);
  186. double t2 = t*t, th2 = th*th;
  187. /* many risks of premature overflow here */
  188. double d = 1.0 + t2*th2;
  189. z.real = t*(1.0-th2)/d; /* /* if th2 is very near 1.0 this is inaccurate */
  190. z.imag = th*(1.0+t2)/d;
  191. return z;
  192. }
  193. static Complex MS_CDECL Cdiv_z(Complex p, Complex q)
  194. {
  195. /*
  196. * (p/q) as a complex number. Note abominable issues about scaling so
  197. * the large values of p and q can be handled properly.
  198. * The easy scheme for (a + ib)/(c + id) would have been
  199. * (ac+bd)/w + i(bc - ad)/w with w = cc + dd
  200. */
  201. double a = p.real, b = p.imag;
  202. double c = q.real, d = q.imag;
  203. Complex r;
  204. if (d == 0.0) /* dividing by a real number */
  205. { r.real = a/c;
  206. r.imag = b/c;
  207. return r;
  208. }
  209. else if (c == 0.0) /* dividing by a purely imaginary number */
  210. { r.real = b/d;
  211. r.imag = -a/d;
  212. return r;
  213. }
  214. else
  215. {
  216. double scalep, scaleq, w;
  217. int n1, n2, n;
  218. /*
  219. * I avoid going frexp(0.0, &n1) since the exponent handed back in that
  220. * case is zero, which is not actually very helpful here, where I would
  221. * rather it was minus infinity.
  222. */
  223. if (a == 0.0)
  224. { if (b == 0.0) return p; /* (0.0, 0.0)/z */
  225. /* Exact results from frexp unimportant */
  226. else (void)frexp(b, &n1);
  227. }
  228. else if (b == 0.0) (void)frexp(a, &n1);
  229. else
  230. { (void)frexp(a, &n1);
  231. (void)frexp(b, &n2);
  232. if (n2>n1) n1 = n2;
  233. }
  234. n = n1;
  235. scalep = ldexp(1.0, n1); /* scale numerator */
  236. a = a / scalep;
  237. b = b / scalep;
  238. /* At this stage I know that the denominator has nonzero real & imag parts */
  239. (void)frexp(c, &n1);
  240. (void)frexp(d, &n2);
  241. if (n2>n1) n1 = n2;
  242. n = n - n1;
  243. scaleq = ldexp(1.0, n1); /* scale denominator */
  244. c = c / scaleq;
  245. d = d / scaleq;
  246. w = c*c + d*d; /* no overflow */
  247. r.real = ldexp((a*c + b*d)/w, n); /* rescale final result */
  248. r.imag = ldexp((b*c - a*d)/w, n);
  249. return r;
  250. }
  251. }
  252. static Lisp_Object make_complex_float(Complex v, Lisp_Object a)
  253. /*
  254. * Here a complex result has been been computed (with double precision values
  255. * for both real and imag parts). Squash to the required floating point
  256. * types and package up as a complex value, where (a) was the original input
  257. * value and so should defined the type needed. Note that both
  258. * components of a will have the same type so only one needs testing.
  259. * I do the 'onevalue' here.
  260. */
  261. {
  262. int32 type;
  263. Lisp_Object a1, a2, nil;
  264. a = real_part(a);
  265. if (is_sfloat(a))
  266. { Float_union r, i;
  267. r.f = (float)v.real;
  268. i.f = (float)v.imag;
  269. a1 = make_complex((r.i & ~(int32)0xf) + TAG_SFLOAT,
  270. (i.i & ~(int32)0xf) + TAG_SFLOAT);
  271. errexit();
  272. return onevalue(a1);
  273. }
  274. if (is_bfloat(a)) type = type_of_header(flthdr(a));
  275. else type = TYPE_SINGLE_FLOAT;
  276. a1 = make_boxfloat(v.real, type);
  277. errexit();
  278. a2 = make_boxfloat(v.imag, type);
  279. errexit();
  280. a1 = make_complex(a1, a2);
  281. errexit();
  282. return onevalue(a1);
  283. }
  284. #endif
  285. #ifdef __alpha
  286. /*
  287. * NAG report that they need this for the DEC Alpha... some variant on it
  288. * may be necessary on other systems too?
  289. */
  290. #define isnegative(x) ((x) < 0.0 && !isnan(x))
  291. #else
  292. #define isnegative(x) ((x) < 0.0)
  293. #endif
  294. static double MS_CDECL rln(double x)
  295. {
  296. if (isnegative(x)) x = -x;
  297. return log(x);
  298. }
  299. static double MS_CDECL iln(double x)
  300. {
  301. if (isnegative(x)) return _pi;
  302. else return 0.0;
  303. }
  304. static double MS_CDECL rsqrt(double x)
  305. {
  306. if (isnegative(x)) return 0.0;
  307. else return sqrt(x);
  308. }
  309. static double MS_CDECL isqrt(double x)
  310. {
  311. if (isnegative(x)) return sqrt(-x);
  312. else return 0.0;
  313. }
  314. static double MS_CDECL rasin(double x)
  315. {
  316. if (1.0 < x) return _half_pi;
  317. else if (x <= -1.0) return -_half_pi;
  318. else return asin(x);
  319. }
  320. static double MS_CDECL iasin(double x)
  321. {
  322. CSLbool sign;
  323. if (-1.0 <= x && x <= 1.0) return 0.0;
  324. else if (x < 0.0) x = -x, sign = YES;
  325. else sign = NO;
  326. if (x < 2.0)
  327. { x += sqrt(x*x - 1.0);
  328. x = log(x); /* /* serious inaccuracy here */
  329. }
  330. else if (x < 1.0e9)
  331. { x += sqrt(x*x - 1.0);
  332. x = log(x);
  333. }
  334. else x = CSL_log_2 + log(x);
  335. if (sign) return -x;
  336. else return x;
  337. }
  338. static double MS_CDECL racos(double x)
  339. {
  340. if (x <= -1.0) return _pi;
  341. else if (1.0 <= x) return 0.0;
  342. else return acos(x);
  343. }
  344. static double MS_CDECL iacos(double x)
  345. {
  346. CSLbool sign;
  347. if (x < -1.0) x = -x, sign = YES;
  348. else if (1.0 < x) sign = NO;
  349. else return 0.0;
  350. if (x < 2.0)
  351. { x += sqrt(x*x - 1.0);
  352. x = log(x); /* /* serious inaccuracy here */
  353. }
  354. else if (x < 1.0e9)
  355. { x += sqrt(x*x - 1.0);
  356. x = log(x);
  357. }
  358. else x = CSL_log_2 + log(x);
  359. if (sign) return x;
  360. else return -x;
  361. }
  362. static double MS_CDECL CSLasinh(double x)
  363. {
  364. CSLbool sign;
  365. if (x < 0.0) x = -x, sign = YES;
  366. else sign = NO;
  367. if (x < 1.0e-3)
  368. { double xx = x*x;
  369. x = x*(1 - xx*((1.0/6.0) - (3.0/40.0)*xx));
  370. }
  371. else if (x < 1.0e9)
  372. { x += sqrt(1.0 + x*x);
  373. x = log(x);
  374. }
  375. else x = log(x) + CSL_log_2;
  376. if (sign) x = -x;
  377. return x;
  378. }
  379. static double acosh_coeffs[] =
  380. {
  381. -0.15718655513711019382e-5, /* x^11 */
  382. +0.81758779765416234142e-5, /* x^10 */
  383. -0.24812280287135584149e-4, /* x^9 */
  384. +0.62919005027033514743e-4, /* x^8 */
  385. -0.15404104307204835991e-3, /* x^7 */
  386. +0.38339903706706128921e-3, /* x^6 */
  387. -0.98871347029548821795e-3, /* x^5 */
  388. +0.26854094489454297811e-2, /* x^4 */
  389. -0.78918167367399344521e-2, /* x^3 */
  390. +0.26516504294146930609e-1, /* x^2 */
  391. -0.11785113019775570984, /* x */
  392. +1.41421356237309504786 /* 1 */
  393. };
  394. static double MS_CDECL racosh(double x)
  395. {
  396. CSLbool sign;
  397. if (x < -1.0) x = -x, sign = YES;
  398. else if (1.0 < x) sign = NO;
  399. else return 0.0;
  400. if (x < 1.5)
  401. { int i;
  402. double r = acosh_coeffs[0];
  403. x = (x - 0.5) - 0.5;
  404. /*
  405. * This is a minimax approximation to acosh(1+x)/sqrt(x) over the
  406. * range x=0 to 0.5
  407. */
  408. for (i=1; i<=11; i++) r = x*r + acosh_coeffs[i];
  409. x = sqrt(x)*r;
  410. }
  411. else if (x < 1.0e9)
  412. { x += sqrt((x - 1.0)*(x + 1.0));
  413. x = log(x);
  414. }
  415. else x = log(x) + CSL_log_2;
  416. if (sign) return -x;
  417. else return x;
  418. }
  419. static double MS_CDECL iacosh(double x)
  420. {
  421. if (1.0 <= x) return 0.0;
  422. else if (x <= -1.0) return _pi;
  423. else return acos(x);
  424. }
  425. static double MS_CDECL ratanh(double z)
  426. {
  427. if (z > -0.01 && z < -0.01)
  428. { double zz = z*z;
  429. return z * (1 + zz*((1.0/3.0) + zz*((1.0/5.0) + zz*(1.0/7.0))));
  430. }
  431. z = (1.0 + z) / (1.0 - z);
  432. if (z < 0.0) z = -z;
  433. return log(z) / 2.0;
  434. }
  435. static double MS_CDECL iatanh(double x)
  436. {
  437. if (x < -1.0) return _half_pi;
  438. else if (1.0 < x) return -_half_pi;
  439. else return 0.0;
  440. }
  441. /*
  442. * The functions from here on (for a bit) represent re-work to support the
  443. * full set of elementary functions that Reduce would (maybe) like. They
  444. * have not been adjusted to allow use with the software floating point
  445. * option.
  446. */
  447. #define n180pi 57.2957795130823208768 /* 180/pi */
  448. #define pi180 0.017453292519943295769 /* pi/180 */
  449. #define sqrthalf 0.7071 /* sqrt(0.5), low accuracy OK */
  450. static double MS_CDECL racosd(double a)
  451. {
  452. if (a <= -1.0) return 180.0;
  453. else if (a < -sqrthalf) return 180.0 - n180pi*acos(-a);
  454. else if (a < 0.0) return 90.0 + n180pi*asin(-a);
  455. else if (a < sqrthalf) return 90.0 - n180pi*asin(a);
  456. else if (a < 1.0) return n180pi*acos(a);
  457. else return 0.0;
  458. }
  459. static double MS_CDECL iacosd(double a)
  460. /*
  461. * This version is only good enough for real-mode CSL, not for CCL
  462. */
  463. {
  464. if (a >= -1.0 && a <= 1.0) return 0.0;
  465. else return 1.0;
  466. }
  467. static double MS_CDECL racot(double a)
  468. {
  469. if (a >= 0.0)
  470. if (a > 1.0) return atan(1.0/a);
  471. else return _half_pi - atan(a);
  472. else if (a < -1.0) return _pi - atan(-1.0/a);
  473. else return _half_pi + atan(-a);
  474. }
  475. static double MS_CDECL racotd(double a)
  476. {
  477. if (a >= 0.0)
  478. if (a > 1.0) return n180pi*atan(1.0/a);
  479. else return 90.0 - n180pi*atan(a);
  480. else if (a < -1.0) return 180.0 - n180pi*atan(-1.0/a);
  481. else return 90.0 + n180pi*atan(-a);
  482. }
  483. static double MS_CDECL racoth(double a)
  484. /*
  485. * No good in complex case
  486. */
  487. {
  488. if (a >= -1.0 && a <= 1.0) return 0.0;
  489. else return ratanh(1.0/a);
  490. }
  491. static double MS_CDECL iacoth(double a)
  492. {
  493. if (a >= -1.0 && a <= 1.0) return 1.0;
  494. else return 0.0;
  495. }
  496. static double MS_CDECL racsc(double a)
  497. {
  498. if (a > -1.0 && a < 1.0) return 0.0;
  499. else return asin(1.0/a);
  500. }
  501. static double MS_CDECL iacsc(double a)
  502. {
  503. if (a > -1.0 && a < 1.0) return 1.0;
  504. else return 0.0;
  505. }
  506. static double MS_CDECL racscd(double a)
  507. {
  508. if (a > -1.0 && a < 1.0) return 0.0;
  509. /*
  510. * I could do better than this, I suspect...
  511. */
  512. else return n180pi*asin(1.0/a);
  513. }
  514. static double MS_CDECL iacscd(double a)
  515. {
  516. if (a > -1.0 && a < 1.0) return 1.0;
  517. else return 0.0;
  518. }
  519. static double MS_CDECL racsch(double a)
  520. {
  521. if (a == 0.0) return HUGE_VAL;
  522. else return CSLasinh(1.0/a);
  523. }
  524. static double MS_CDECL rasec(double a)
  525. {
  526. if (a > -1.0 && a <= 1.0) return 0.0;
  527. else return acos(1.0/a);
  528. }
  529. static double MS_CDECL iasec(double a)
  530. {
  531. if (a > -1.0 && a < 1.0) return 1.0;
  532. else return 0.0;
  533. }
  534. static double MS_CDECL rasecd(double a)
  535. {
  536. if (a > -1.0 && a <= 1.0) return 0.0;
  537. /*
  538. * I could do better than this, I suspect...
  539. */
  540. else return n180pi*acos(1.0/a);
  541. }
  542. static double MS_CDECL iasecd(double a)
  543. {
  544. if (a > -1.0 && a < 1.0) return 1.0;
  545. else return 0.0;
  546. }
  547. static double MS_CDECL rasech(double a)
  548. {
  549. if (a <= 0.0 || a >= 1.0) return 0.0;
  550. else return racosh(1.0/a);
  551. }
  552. static double MS_CDECL iasech(double a)
  553. {
  554. if (a <= 0.0 || a > 1.0) return 1.0;
  555. else return 0.0;
  556. }
  557. static double MS_CDECL rasind(double a)
  558. {
  559. if (a <= -1.0) return -90.0;
  560. else if (a < -sqrthalf) return -90.0 + n180pi*acos(-a);
  561. else if (a < sqrthalf) return n180pi*asin(a);
  562. else if (a < 1.0) return 90.0 - n180pi*acos(a);
  563. else return 90.0;
  564. }
  565. static double MS_CDECL iasind(double a)
  566. {
  567. if (a >= -1.0 && a <= 1.0) return 0.0;
  568. else return 1.0;
  569. }
  570. static double MS_CDECL ratand(double a)
  571. {
  572. if (a < -1.0) return -90.0 + n180pi*atan(-1.0/a);
  573. else if (a < 1.0) return n180pi*atan(a);
  574. else return 90.0 - n180pi*atan(1.0/a);
  575. }
  576. static double MS_CDECL rcbrt(double a)
  577. {
  578. int xx, x, i, neg = 0;
  579. double b;
  580. if (a == 0.0) return 0.0;
  581. else if (a < 0.0) a = -a, neg = 1;
  582. b = frexp(a, &xx); /* end-conditions unimportant */
  583. x = xx;
  584. /*
  585. * b is now in the range 0.5 to 1. The next line produces an
  586. * approximately minimax linear approximation to the cube root
  587. % function over the range concerned.
  588. */
  589. b = 0.5996 + 0.4081*b;
  590. while (x % 3 != 0) x--; b *= 1.26;
  591. b = ldexp(b, x/3);
  592. /*
  593. * Experiment shows that there are values of the input variable
  594. * that lead to the last of the following iterations making a
  595. * (small) change to the result, but almost all of the time I
  596. * could get away with one less. Still, I do not consider cbrt
  597. * important enough to optimise any further (e.g. I could go to
  598. * use of a minimax rational first approximation...)
  599. */
  600. for (i=0; i<6; i++)
  601. b = (2.0*b + a/(b*b))/3.0;
  602. if (neg) return -b;
  603. else return b;
  604. }
  605. static double MS_CDECL rcot(double a)
  606. /*
  607. * Compare this code with rcotd(). Here I just compute a tangent and
  608. * form its reciprocal. What about an arg of pi/2 then, where the
  609. * tangent calculation might overflow? Well it should not, since no
  610. * integer multiple of pi/2 has an exact machine representation, so
  611. * if you try to compute tan(pi/2) in floating point you should get a
  612. * large but finite value. For very large a where the tan() function
  613. * loses resolution there could still be trouble, which is why I use
  614. * a slower formula for big a.
  615. */
  616. {
  617. if (a > 1000.0 || a < -1000.0) return cos(a)/sin(a);
  618. else return 1.0/tan(a);
  619. }
  620. static double MS_CDECL arg_reduce_degrees(double a, int *quadrant)
  621. /*
  622. * Reduce argument to the range -45 to 45, and set quadrant to the
  623. * relevant quadant. Returns arg converted to radians.
  624. */
  625. {
  626. double w = a / 90.0;
  627. int32 n = (int)w;
  628. w = a - 90.0*n;
  629. while (w < -45.0)
  630. { n--;
  631. w = a - 90.0*n;
  632. }
  633. while (w >= 45.0)
  634. { n++;
  635. w = a - 90.0*n;
  636. }
  637. *quadrant = (int)(n & 3);
  638. return pi180*w;
  639. }
  640. static double MS_CDECL rsind(double a)
  641. {
  642. int quadrant;
  643. a = arg_reduce_degrees(a, &quadrant);
  644. switch (quadrant)
  645. {
  646. default:
  647. case 0: return sin(a);
  648. case 1: return cos(a);
  649. case 2: return sin(-a);
  650. case 3: return -cos(a);
  651. }
  652. }
  653. static double MS_CDECL rcosd(double a)
  654. {
  655. int quadrant;
  656. a = arg_reduce_degrees(a, &quadrant);
  657. switch (quadrant)
  658. {
  659. default:
  660. case 0: return cos(a);
  661. case 1: return sin(-a);
  662. case 2: return -cos(a);
  663. case 3: return sin(a);
  664. }
  665. }
  666. static double MS_CDECL rtand(double a)
  667. {
  668. int quadrant;
  669. a = arg_reduce_degrees(a, &quadrant);
  670. switch (quadrant)
  671. {
  672. default:
  673. case 0:
  674. case 2: return tan(a);
  675. case 1:
  676. case 3: return 1.0/tan(-a);
  677. }
  678. }
  679. static double MS_CDECL rcotd(double a)
  680. {
  681. int quadrant;
  682. a = arg_reduce_degrees(a, &quadrant);
  683. switch (quadrant)
  684. {
  685. default:
  686. case 0:
  687. case 2: return 1.0/tan(a);
  688. case 1:
  689. case 3: return tan(-a);
  690. }
  691. }
  692. static double MS_CDECL rcoth(double a)
  693. {
  694. if (a == 0.0) return HUGE_VAL;
  695. else return 1.0/tanh(a);
  696. }
  697. static double MS_CDECL rcsc(double a)
  698. {
  699. a = sin(a);
  700. if (a == 0.0) return HUGE_VAL;
  701. else return 1.0/a;
  702. }
  703. static double MS_CDECL rcscd(double a)
  704. {
  705. a = rsind(a);
  706. if (a == 0.0) return HUGE_VAL;
  707. else return 1.0/a;
  708. }
  709. static double MS_CDECL rcsch(double a)
  710. {
  711. /*
  712. * This code is imperfect in that (at least!) exp(-a) can underflow to zero
  713. * before 2*exp(-a) ought to have. I will not worry about such refinement
  714. * here. Much.
  715. */
  716. if (a == 0.0) return HUGE_VAL;
  717. else if (a > 20.0) return 2.0*exp(-a);
  718. else if (a < -20.0) return -2.0*exp(a);
  719. else return 1.0/sinh(a);
  720. }
  721. #define CSL_log10 2.302585092994045684
  722. static double MS_CDECL rlog10(double a)
  723. {
  724. if (a > 0.0) return log(a)/CSL_log10;
  725. else return 0.0;
  726. }
  727. static double MS_CDECL ilog10(double a)
  728. {
  729. if (a <= 0) return 1.0;
  730. else return 0.0;
  731. }
  732. static double MS_CDECL rsec(double a)
  733. {
  734. a = cos(a);
  735. if (a == 0.0) return HUGE_VAL;
  736. else return 1.0/a;
  737. }
  738. static double MS_CDECL rsecd(double a)
  739. {
  740. a = rcosd(a);
  741. if (a == 0.0) return HUGE_VAL;
  742. else return 1.0/a;
  743. }
  744. static double MS_CDECL rsech(double a)
  745. {
  746. /*
  747. * When |a| is big I ought to return 0.0
  748. */
  749. return 1.0/cosh(a);
  750. }
  751. #ifdef COMMON
  752. #define i_times(z) \
  753. { double temp = z.imag; z.imag = z.real; z.real = -temp; }
  754. #define m_i_times(z) \
  755. { double temp = z.imag; z.imag = -z.real; z.real = temp; }
  756. #endif
  757. /*
  758. * The calculations in the next few procedures are numerically
  759. * crummy, but they should get branch cuts correct. Re-work later.
  760. */
  761. #ifdef COMMON
  762. static Complex MS_CDECL casinh(Complex z)
  763. /* log(z + sqrt(1 + z^2)) */
  764. {
  765. int quadrant = 0;
  766. Complex w;
  767. if (z.real < 0.0)
  768. { z.real = -z.real;
  769. z.imag = -z.imag;
  770. quadrant = 1;
  771. }
  772. if (z.imag < 0.0)
  773. { z.imag = -z.imag;
  774. quadrant |= 2;
  775. }
  776. /* /* The next line can overflow or lose precision */
  777. w.real = z.real*z.real - z.imag*z.imag + 1.0;
  778. w.imag = 2*z.real*z.imag;
  779. w = csqrt(w);
  780. w.real += z.real;
  781. w.imag += z.imag;
  782. w = Cln(w);
  783. if (quadrant & 2) w.imag = -w.imag;
  784. if (quadrant & 1) w.real = -w.real, w.imag = -w.imag;
  785. return w;
  786. }
  787. static Complex MS_CDECL cacosh(Complex z)
  788. /* 2*log(sqrt((z+1)/2) + sqrt((z-1)/2)) */
  789. {
  790. Complex w1, w2;
  791. w1.real = (z.real + 1.0)/2.0;
  792. w2.real = (z.real - 1.0)/2.0;
  793. w1.imag = w2.imag = z.imag/2.0;
  794. w1 = csqrt(w1);
  795. w2 = csqrt(w2);
  796. w1.real += w2.real;
  797. w1.imag += w2.imag;
  798. z = Cln(w1);
  799. z.real *= 2.0;
  800. z.imag *= 2.0;
  801. return z;
  802. }
  803. static Complex MS_CDECL catanh(Complex z)
  804. /* (log(1+z) - log(1-z))/2 */
  805. {
  806. Complex w1, w2;
  807. w1.real = 1.0 + z.real;
  808. w2.real = 1.0 - z.real;
  809. w1.imag = z.imag;
  810. w2.imag = -z.imag;
  811. w1 = Cln(w1);
  812. w2 = Cln(w2);
  813. w1.real -= w2.real;
  814. w1.imag -= w2.imag;
  815. w1.real /= 2.0;
  816. w1.imag /= 2.0;
  817. return w1;
  818. }
  819. static Complex MS_CDECL casin(Complex z)
  820. {
  821. i_times(z);
  822. z = casinh(z);
  823. m_i_times(z);
  824. return z;
  825. }
  826. static Complex MS_CDECL cacos(Complex z)
  827. {
  828. /* This is the code I had originally had...
  829. z = cacosh(z);
  830. m_i_times(z);
  831. return z;
  832. */
  833. /*
  834. * The following is asserted to behave better. I believe that the
  835. * calculation (pi/2 - z.real) is guaranteed to introduce severe error
  836. * when the answer is close to zero, and so this is probably not the ultimate
  837. * proper formula to use.
  838. */
  839. z = casin(z);
  840. z.real = _half_pi - z.real;
  841. z.imag = - z.imag;
  842. return z;
  843. }
  844. static Complex MS_CDECL catan(Complex z)
  845. {
  846. i_times(z);
  847. z = catanh(z);
  848. m_i_times(z);
  849. return z;
  850. }
  851. static Complex MS_CDECL csinh(Complex z)
  852. {
  853. i_times(z);
  854. z = csin(z);
  855. m_i_times(z);
  856. return z;
  857. }
  858. static Complex MS_CDECL ccosh(Complex z)
  859. {
  860. i_times(z);
  861. return Ccos(z);
  862. }
  863. static Complex MS_CDECL ctanh(Complex z)
  864. {
  865. i_times(z);
  866. z = ctan(z);
  867. m_i_times(z);
  868. return z;
  869. }
  870. /*
  871. * The next collection of things have not been implemented yet,
  872. * but might be wanted in a full extended system.... maybe.
  873. */
  874. static Complex MS_CDECL cacosd(Complex a)
  875. {
  876. return a;
  877. }
  878. static Complex MS_CDECL cacot(Complex a)
  879. {
  880. return a;
  881. }
  882. static Complex MS_CDECL cacotd(Complex a)
  883. {
  884. return a;
  885. }
  886. static Complex MS_CDECL cacoth(Complex a)
  887. {
  888. return a;
  889. }
  890. static Complex MS_CDECL cacsc(Complex a)
  891. {
  892. return a;
  893. }
  894. static Complex MS_CDECL cacscd(Complex a)
  895. {
  896. return a;
  897. }
  898. static Complex MS_CDECL cacsch(Complex a)
  899. {
  900. return a;
  901. }
  902. static Complex MS_CDECL casec(Complex a)
  903. {
  904. return a;
  905. }
  906. static Complex MS_CDECL casecd(Complex a)
  907. {
  908. return a;
  909. }
  910. static Complex MS_CDECL casech(Complex a)
  911. {
  912. return a;
  913. }
  914. static Complex MS_CDECL casind(Complex a)
  915. {
  916. return a;
  917. }
  918. static Complex MS_CDECL catand(Complex a)
  919. {
  920. return a;
  921. }
  922. static Complex MS_CDECL ccbrt(Complex a)
  923. {
  924. return a;
  925. }
  926. static Complex MS_CDECL ccosd(Complex a)
  927. {
  928. return a;
  929. }
  930. static Complex MS_CDECL ccot(Complex a)
  931. {
  932. return a;
  933. }
  934. static Complex MS_CDECL ccotd(Complex a)
  935. {
  936. return a;
  937. }
  938. static Complex MS_CDECL ccoth(Complex a)
  939. {
  940. return a;
  941. }
  942. static Complex MS_CDECL ccsc(Complex a)
  943. {
  944. return a;
  945. }
  946. static Complex MS_CDECL ccscd(Complex a)
  947. {
  948. return a;
  949. }
  950. static Complex MS_CDECL ccsch(Complex a)
  951. {
  952. return a;
  953. }
  954. static Complex MS_CDECL clog10(Complex a)
  955. {
  956. return a;
  957. }
  958. static Complex MS_CDECL csec(Complex a)
  959. {
  960. return a;
  961. }
  962. static Complex MS_CDECL csecd(Complex a)
  963. {
  964. return a;
  965. }
  966. static Complex MS_CDECL csech(Complex a)
  967. {
  968. return a;
  969. }
  970. static Complex MS_CDECL csind(Complex a)
  971. {
  972. return a;
  973. }
  974. static Complex MS_CDECL ctand(Complex a)
  975. {
  976. return a;
  977. }
  978. /* end of unimplemented bunch */
  979. #endif
  980. /*
  981. * Now the Lisp callable entrypoints for the above
  982. */
  983. typedef double MS_CDECL real_arg_fn(double);
  984. #ifdef COMMON
  985. typedef Complex MS_CDECL complex_arg_fn(Complex);
  986. #endif
  987. typedef struct trigfn
  988. { double (MS_CDECL *real)(double);
  989. double (MS_CDECL *imag)(double);
  990. #ifdef COMMON
  991. Complex (MS_CDECL *complex)(Complex);
  992. #endif
  993. } trigfn_record;
  994. #ifdef NeXT
  995. /*
  996. * The NeXT header files declare some functions from <math.h> with an
  997. * extra keyword __const__ in the signature, with a result that placing a
  998. * pointer to the function in a regularly-typed location leads to a
  999. * warning. The little wrapper functions here should sort that one out.
  1000. */
  1001. static double CSL_atan(double x){ return atan(x); }
  1002. #define atan(x) CSL_atan(x)
  1003. static double CSL_cos(double x) { return cos(x); }
  1004. #define cos(x) CSL_cos(x)
  1005. static double CSL_cosh(double x){ return cosh(x); }
  1006. #define cosh(x) CSL_cosh(x)
  1007. static double CSL_exp(double x) { return exp(x); }
  1008. #define exp(x) CSL_exp(x)
  1009. static double CSL_sin(double x) { return sin(x); }
  1010. #define sin(x) CSL_sin(x)
  1011. static double CSL_sinh(double x){ return sinh(x); }
  1012. #define sinh(x) CSL_sinh(x)
  1013. static double CSL_tan(double x) { return tan(x); }
  1014. #define tan(x) CSL_tan(x)
  1015. static double CSL_tanh(double x){ return tanh(x); }
  1016. #define tanh(x) CSL_tanh(x)
  1017. #endif
  1018. #ifdef COMMON
  1019. static trigfn_record const trig_functions[] =
  1020. {
  1021. {racos, iacos, cacos}, /* acos 0 inverse cos, rads, [0, pi) */
  1022. {racosd, iacosd, cacosd}, /* acosd 1 inverse cos, degs, [0, 180) */
  1023. {racosh, iacosh, cacosh}, /* acosh 2 inverse hyperbolic cosine */
  1024. {racot, 0, cacot}, /* acot 3 inverse cot, rads, (0, pi) */
  1025. {racotd, 0, cacotd}, /* acotd 4 inverse cot, degs, (0, 180) */
  1026. {racoth, iacoth, cacoth}, /* acoth 5 inverse hyperbolic cotangent */
  1027. {racsc, iacsc, cacsc}, /* acsc 6 inverse cosec, [-pi/2, pi/2] */
  1028. {racscd, iacscd, cacscd}, /* acscd 7 inverse cosec, degs, [-90, 90] */
  1029. {racsch, 0, cacsch}, /* acsch 8 inverse hyperbolic cosecant */
  1030. {rasec, iasec, casec}, /* asec 9 inverse sec, rads, [0, pi) */
  1031. {rasecd, iasecd, casecd}, /* asecd 10 inverse sec, degs, [0, 180) */
  1032. {rasech, iasech, casech}, /* asech 11 inverse hyperbolic secant */
  1033. {rasin, iasin, casin}, /* asin 12 inverse sin, rads, [-pi/2, pi/2] */
  1034. {rasind, iasind, casind}, /* asind 13 inverse sin, degs, [-90, 90] */
  1035. {CSLasinh, 0, casinh}, /* asinh 14 inverse hyperbolic sin */
  1036. {atan, 0, catan}, /* atan 15 1-arg inverse tan, (-pi/2, pi/2) */
  1037. {ratand, 0, catand}, /* atand 16 inverse tan, degs, (-90, 90) */
  1038. {0, 0, 0}, /* atan2 17 2-arg inverse tan, [0, 2pi) */
  1039. {0, 0, 0}, /* atan2d 18 2-arg inverse tan, degs, [0, 360) */
  1040. {ratanh, iatanh, catanh}, /* atanh 19 inverse hyperbolic tan */
  1041. {rcbrt, 0, ccbrt}, /* cbrt 20 cube root */
  1042. {cos, 0, Ccos}, /* cos 21 cosine, rads */
  1043. {rcosd, 0, ccosd}, /* cosd 22 cosine, degs */
  1044. {cosh, 0, ccosh}, /* cosh 23 hyperbolic cosine */
  1045. {rcot, 0, ccot}, /* cot 24 cotangent, rads */
  1046. {rcotd, 0, ccotd}, /* cotd 25 cotangent, degs */
  1047. {rcoth, 0, ccoth}, /* coth 26 hyperbolic cotangent */
  1048. {rcsc, 0, ccsc}, /* csc 27 cosecant, rads */
  1049. {rcscd, 0, ccscd}, /* cscd 28 cosecant, degs */
  1050. {rcsch, 0, ccsch}, /* csch 29 hyperbolic cosecant */
  1051. {exp, 0, Cexp}, /* exp 30 exp(x) = e^z, e approx 2.71828 */
  1052. {0, 0, 0}, /* expt 31 expt(a,b) = a^b */
  1053. {0, 0, 0}, /* hypot 32 hypot(a,b) = sqrt(a^2+b^2) */
  1054. {rln, iln, Cln}, /* ln 33 log base e, e approx 2.71828 */
  1055. {0, 0, 0}, /* log 34 2-arg log */
  1056. {rlog10, ilog10, clog10}, /* log10 35 log to base 10 */
  1057. {rsec, 0, csec}, /* sec 36 secant, rads */
  1058. {rsecd, 0, csecd}, /* secd 37 secant, degs */
  1059. {rsech, 0, csech}, /* sech 38 hyperbolic secant */
  1060. {sin, 0, csin}, /* sin 39 sine, rads */
  1061. {rsind, 0, csind}, /* sind 40 sine, degs */
  1062. {sinh, 0, csinh}, /* sinh 41 hyperbolic sine */
  1063. {rsqrt, isqrt, csqrt}, /* sqrt 42 square root */
  1064. {tan, 0, ctan}, /* tan 43 tangent, rads */
  1065. {rtand, 0, ctand}, /* tand 44 tangent, degs */
  1066. {tanh, 0, ctanh} /* tanh 45 hyperbolic tangent */
  1067. };
  1068. #else
  1069. static trigfn_record const trig_functions[] =
  1070. {
  1071. {racos, iacos}, /* acos 0 inverse cosine, rads, [0, pi) */
  1072. {racosd, iacosd}, /* acosd 1 inverse cosine, degs, [0, 180) */
  1073. {racosh, iacosh}, /* acosh 2 inverse hyperbolic cosine */
  1074. {racot, 0}, /* acot 3 inverse cotangent, rads, (0, pi) */
  1075. {racotd, 0}, /* acotd 4 inverse cotangent, degs, (0, 180) */
  1076. {racoth, iacoth}, /* acoth 5 inverse hyperbolic cotangent */
  1077. {racsc, iacsc}, /* acsc 6 inverse cosecant, rads, [-pi/2, pi/2] */
  1078. {racscd, iacscd}, /* acscd 7 inverse cosecant, degs, [-90, 90] */
  1079. {racsch, 0}, /* acsch 8 inverse hyperbolic cosecant */
  1080. {rasec, iasec}, /* asec 9 inverse secant, rads, [0, pi) */
  1081. {rasecd, iasecd}, /* asecd 10 inverse secant, degs, [0, 180) */
  1082. {rasech, iasech}, /* asech 11 inverse hyperbolic secant */
  1083. {rasin, iasin}, /* asin 12 inverse sine, rads, [-pi/2, pi/2] */
  1084. {rasind, iasind}, /* asind 13 inverse sine, degs, [-90, 90] */
  1085. {CSLasinh, 0}, /* asinh 14 inverse hyperbolic sine */
  1086. {atan, 0}, /* atan 15 1-arg inverse tangent, (-pi/2, pi/2) */
  1087. {ratand, 0}, /* atand 16 1-arg inverse tangent, degs, (-90, 90) */
  1088. {0, 0}, /* atan2 17 2-arg inverse tangent, [0, 2pi) */
  1089. {0, 0}, /* atan2d 18 2-arg inverse tangent, degs, [0, 360) */
  1090. {ratanh, iatanh}, /* atanh 19 inverse hyperbolic tangent */
  1091. {rcbrt, 0}, /* cbrt 20 cube root */
  1092. {cos, 0}, /* cos 21 cosine, rads */
  1093. {rcosd, 0}, /* cosd 22 cosine, degs */
  1094. {cosh, 0}, /* cosh 23 hyperbolic cosine */
  1095. {rcot, 0}, /* cot 24 cotangent, rads */
  1096. {rcotd, 0}, /* cotd 25 cotangent, degs */
  1097. {rcoth, 0}, /* coth 26 hyperbolic cotangent */
  1098. {rcsc, 0}, /* csc 27 cosecant, rads */
  1099. {rcscd, 0}, /* cscd 28 cosecant, degs */
  1100. {rcsch, 0}, /* csch 29 hyperbolic cosecant */
  1101. {exp, 0}, /* exp 30 exp(x) = e^z, e approx 2.71828 */
  1102. {0, 0}, /* expt 31 expt(a,b) = a^b */
  1103. {0, 0}, /* hypot 32 hypot(a,b) = sqrt(a^2+b^2) */
  1104. {rln, iln}, /* ln 33 log base e, e approx 2.71828 */
  1105. {0, 0}, /* log 34 2-arg log. log(a,b) is log of a base b */
  1106. {rlog10, ilog10}, /* log10 35 log to base 10 */
  1107. {rsec, 0}, /* sec 36 secant, rads */
  1108. {rsecd, 0}, /* secd 37 secant, degs */
  1109. {rsech, 0}, /* sech 38 hyperbolic secant */
  1110. {sin, 0}, /* sin 39 sine, rads */
  1111. {rsind, 0}, /* sind 40 sine, degs */
  1112. {sinh, 0}, /* sinh 41 hyperbolic sine */
  1113. {rsqrt, isqrt}, /* sqrt 42 square root */
  1114. {tan, 0}, /* tan 43 tangent, rads */
  1115. {rtand, 0}, /* tand 44 tangent, degs */
  1116. {tanh, 0} /* tanh 45 hyperbolic tangent */
  1117. };
  1118. #endif
  1119. static Lisp_Object Ltrigfn(unsigned int which_one, Lisp_Object a)
  1120. /*
  1121. * This one piece of code does the type-dispatch for the main collection
  1122. * of elementary functions.
  1123. */
  1124. {
  1125. double d;
  1126. Lisp_Object nil = C_nil;
  1127. #ifndef COMMON
  1128. int32 restype = TYPE_DOUBLE_FLOAT;
  1129. #else
  1130. int32 restype = TYPE_SINGLE_FLOAT;
  1131. #endif
  1132. if (which_one > 45) return aerror("trigfn internal error");
  1133. switch ((int)a & TAG_BITS)
  1134. {
  1135. case TAG_FIXNUM:
  1136. d = (double)int_of_fixnum(a);
  1137. break;
  1138. #ifdef COMMON
  1139. case TAG_SFLOAT:
  1140. { Float_union aa;
  1141. aa.i = a - TAG_SFLOAT;
  1142. d = (double)aa.f;
  1143. restype = 0;
  1144. break;
  1145. }
  1146. #endif
  1147. case TAG_NUMBERS:
  1148. { int32 ha = type_of_header(numhdr(a));
  1149. switch (ha)
  1150. {
  1151. case TYPE_BIGNUM:
  1152. #ifdef COMMON
  1153. case TYPE_RATNUM:
  1154. #endif
  1155. d = float_of_number(a);
  1156. break;
  1157. #ifdef COMMON
  1158. case TYPE_COMPLEX_NUM:
  1159. { Complex c1, c2;
  1160. c1.real = float_of_number(real_part(a));
  1161. c1.imag = float_of_number(imag_part(a));
  1162. c2 = (*trig_functions[which_one].complex)(c1);
  1163. /* make_complex_float does the onevalue() for me */
  1164. return make_complex_float(c2, a);
  1165. }
  1166. #endif
  1167. default:
  1168. return aerror1("bad arg for trig function", a);
  1169. }
  1170. break;
  1171. }
  1172. case TAG_BOXFLOAT:
  1173. restype = type_of_header(flthdr(a));
  1174. d = float_of_number(a);
  1175. break;
  1176. default:
  1177. return aerror1("bad arg for trig function", a);
  1178. }
  1179. { double (MS_CDECL *im)(double) = trig_functions[which_one].imag;
  1180. if (im == 0)
  1181. /*
  1182. * I really suspect I should do something writh errno here to
  1183. * keep track of when things go wrong. Doing so feels fairly
  1184. * messy, but it is necessary if I am to raise exceptions for
  1185. * Lisp if an elementary function leads to overflow.
  1186. */
  1187. { double (MS_CDECL *rl)(double) = trig_functions[which_one].real;
  1188. if (rl == 0) return aerror("unimplemented trig function");
  1189. d = (*rl)(d);
  1190. a = make_boxfloat(d, restype);
  1191. errexit();
  1192. return onevalue(a);
  1193. }
  1194. else
  1195. { double c1r, c1i;
  1196. Lisp_Object nil;
  1197. #ifdef COMMON
  1198. Lisp_Object rp, ip;
  1199. #endif
  1200. double (MS_CDECL *rl)(double) = trig_functions[which_one].real;
  1201. if (rl == 0) return aerror("unimplemented trig function");
  1202. c1r = (*rl)(d);
  1203. c1i = (*im)(d);
  1204. /*
  1205. * if the imaginary part of the value is zero then I will return a real
  1206. * answer - this is correct since the original argument was real, but
  1207. * it has to be done by hand here because normally complex values with
  1208. * zero imaginary part remain complex.
  1209. */
  1210. if (c1i == 0.0)
  1211. {
  1212. a = make_boxfloat(c1r, restype);
  1213. errexit();
  1214. return onevalue(a);
  1215. }
  1216. #ifdef COMMON
  1217. rp = make_boxfloat(c1r, restype);
  1218. errexit();
  1219. ip = make_boxfloat(c1i, restype);
  1220. errexit();
  1221. a = make_complex(rp, ip);
  1222. errexit();
  1223. return onevalue(a);
  1224. #else
  1225. return aerror1("bad arg for trig function", a);
  1226. #endif
  1227. }
  1228. }
  1229. }
  1230. static Lisp_Object makenum(Lisp_Object a, int32 n)
  1231. /*
  1232. * Make the value n, but type-consistent with the object a. Usually
  1233. * used with n=0 or n=1
  1234. */
  1235. {
  1236. #ifndef COMMON
  1237. int32 restype = TYPE_DOUBLE_FLOAT;
  1238. #else
  1239. int32 restype = TYPE_SINGLE_FLOAT;
  1240. #endif
  1241. Lisp_Object nil = C_nil;
  1242. switch ((int)a & TAG_BITS)
  1243. {
  1244. case TAG_FIXNUM:
  1245. return fixnum_of_int(n);
  1246. #ifdef COMMON
  1247. case TAG_SFLOAT:
  1248. { Float_union aa;
  1249. aa.f = (float)n;
  1250. return (aa.i & ~(int32)0xf) + TAG_SFLOAT;
  1251. }
  1252. #endif
  1253. case TAG_NUMBERS:
  1254. { int32 ha = type_of_header(numhdr(a));
  1255. switch (ha)
  1256. {
  1257. case TYPE_BIGNUM:
  1258. #ifdef COMMON
  1259. case TYPE_RATNUM:
  1260. #endif
  1261. return fixnum_of_int(n);
  1262. #ifdef COMMON
  1263. case TYPE_COMPLEX_NUM:
  1264. { Lisp_Object rr, ii;
  1265. a = real_part(a);
  1266. rr = makenum(a, 1);
  1267. errexit();
  1268. ii = makenum(a, 0);
  1269. errexit();
  1270. a = make_complex(rr, ii);
  1271. errexit();
  1272. return onevalue(a);
  1273. }
  1274. #endif
  1275. default:
  1276. return aerror1("bad arg for makenumber", a);
  1277. }
  1278. break;
  1279. }
  1280. case TAG_BOXFLOAT:
  1281. restype = type_of_header(flthdr(a));
  1282. a = make_boxfloat((double)n, restype);
  1283. errexit();
  1284. return onevalue(a);
  1285. default:
  1286. return aerror1("bad arg for makenumber", a);
  1287. }
  1288. }
  1289. static Lisp_Object CSLpowi(Lisp_Object a, unsigned32 n)
  1290. /*
  1291. * Raise a to the power n by repeated multiplication. The name is CSLpowi
  1292. * rather than just powi because some miserable C compilers come with an
  1293. * external function called powi in <math.h> and then moan about the
  1294. * name clash.
  1295. */
  1296. {
  1297. Lisp_Object nil;
  1298. if (n == 0) return makenum(a, 1); /* value 1 of appropriate type */
  1299. else if (n == 1) return a;
  1300. else if ((n & 1) == 0)
  1301. { a = CSLpowi(a, n/2);
  1302. errexit();
  1303. return times2(a, a);
  1304. }
  1305. else
  1306. { Lisp_Object b;
  1307. push(a);
  1308. b = CSLpowi(a, n/2);
  1309. nil = C_nil;
  1310. if (!exception_pending()) b = times2(b, b);
  1311. pop(a);
  1312. errexit();
  1313. return times2(a, b);
  1314. }
  1315. }
  1316. #ifdef COMMON
  1317. static Complex MS_CDECL complex_of_number(Lisp_Object a)
  1318. {
  1319. Complex z;
  1320. if (is_numbers(a) && is_complex(a))
  1321. { z.real = float_of_number(real_part(a));
  1322. z.imag = float_of_number(imag_part(a));
  1323. }
  1324. else
  1325. { z.real = float_of_number(a);
  1326. z.imag = 0.0;
  1327. }
  1328. return z;
  1329. }
  1330. #endif
  1331. static Lisp_Object Lhypot(Lisp_Object nil, Lisp_Object a, Lisp_Object b)
  1332. {
  1333. double u, v, r;
  1334. u = float_of_number(a);
  1335. v = float_of_number(b);
  1336. if (u < 0.0) u = -u;
  1337. if (v < 0.0) v = -v;
  1338. if (u > v) { r = u; u = v; v = r; }
  1339. /* Now 0.0 < u < v */
  1340. if (u + v == v) r = v; /* u is very small compared to v */
  1341. else
  1342. { r = u/v;
  1343. /*
  1344. * A worry I have is that the multiplication on the following line can
  1345. * overflow, blowing me out of the water.
  1346. */
  1347. r = v * sqrt(1.0 + r*r);
  1348. }
  1349. a = make_boxfloat(r, TYPE_DOUBLE_FLOAT);
  1350. errexit();
  1351. return onevalue(a);
  1352. }
  1353. Lisp_Object Lexpt(Lisp_Object nil, Lisp_Object a, Lisp_Object b)
  1354. {
  1355. double d, e;
  1356. int32 restype, n;
  1357. #ifdef COMMON
  1358. Lisp_Object w;
  1359. Complex c1, c2, c3;
  1360. #endif
  1361. /*
  1362. * I take special action on 1, 0 and -1 raised to a power that is an integer
  1363. * or a bignum. In part this is because raising 1 to a power may be a fairly
  1364. * common case worthy of special care, but the main pressure came because
  1365. * these numbers raised to bignum powers can still have fixnum results, and
  1366. * the value can be computed fast.
  1367. */
  1368. if (a == fixnum_of_int(1) ||
  1369. a == fixnum_of_int(0) ||
  1370. a == fixnum_of_int(-1))
  1371. { if (is_fixnum(b))
  1372. { n = int_of_fixnum(b);
  1373. switch (int_of_fixnum(a))
  1374. {
  1375. case 1: return onevalue(a);
  1376. case 0: if (n < 0) return aerror2("expt", a, b);
  1377. /* In Common Lisp (expt 0 0) is defined to be 0 */
  1378. else if (n == 0) return onevalue(fixnum_of_int(1));
  1379. else return onevalue(a);
  1380. case -1: if (n & 1) return onevalue(a);
  1381. else return onevalue(fixnum_of_int(1));
  1382. }
  1383. }
  1384. else if (is_numbers(b) && is_bignum(b))
  1385. { switch (int_of_fixnum(a))
  1386. {
  1387. case 1: return onevalue(a);
  1388. case 0: n = bignum_digits(b)[(bignum_length(b)>>2)-2];
  1389. if (n <= 0) return aerror2("expt", a, b);
  1390. else return onevalue(a);
  1391. case -1: n = bignum_digits(b)[0];
  1392. if (n & 1) return onevalue(a);
  1393. else return onevalue(fixnum_of_int(1));
  1394. }
  1395. }
  1396. }
  1397. #ifdef COMMON
  1398. /*
  1399. * In a similar vein I will take special action on #C(0 1) and #C(0 -1)
  1400. * raise to integer (including bignum) powers.
  1401. */
  1402. if (is_numbers(a) && is_complex(a) && real_part(a)==fixnum_of_int(0) &&
  1403. (imag_part(a) == fixnum_of_int(1) ||
  1404. imag_part(a) == fixnum_of_int(-1)))
  1405. { n = -1;
  1406. if (is_fixnum(b)) n = int_of_fixnum(b) & 3;
  1407. else if (is_numbers(b) && is_bignum(b))
  1408. n = bignum_digits(b)[0] & 3;
  1409. switch (n)
  1410. {
  1411. case 0: return onevalue(fixnum_of_int(1));
  1412. case 2: return onevalue(fixnum_of_int(-1));
  1413. case 1:
  1414. case 3: if (int_of_fixnum(imag_part(a)) == 1) n ^= 2;
  1415. a = make_complex(fixnum_of_int(0),
  1416. fixnum_of_int((n & 2) ? 1 : -1));
  1417. errexit();
  1418. return onevalue(a);
  1419. default: break;
  1420. }
  1421. }
  1422. #endif
  1423. if (is_fixnum(b)) /* bignum exponents would yield silly values! */
  1424. { n = int_of_fixnum(b);
  1425. if (n < 0)
  1426. { a = CSLpowi(a, (unsigned32)(-n));
  1427. nil = C_nil;
  1428. #ifdef COMMON
  1429. if (!exception_pending()) a = CLquot2(fixnum_of_int(1), a);
  1430. #else
  1431. if (!exception_pending()) a = quot2(fixnum_of_int(1), a);
  1432. #endif
  1433. }
  1434. else a = CSLpowi(a, (unsigned32)n);
  1435. return onevalue(a);
  1436. }
  1437. #ifdef COMMON
  1438. if (is_numbers(a) && is_complex(a)) w = real_part(a);
  1439. else w = a;
  1440. if (is_sfloat(w)) restype = 0;
  1441. else
  1442. if (is_bfloat(w)) restype = type_of_header(flthdr(w));
  1443. else restype = TYPE_SINGLE_FLOAT;
  1444. if (is_numbers(b) && is_complex(b)) w = real_part(b);
  1445. else w = b;
  1446. if (is_bfloat(w))
  1447. { n = type_of_header(flthdr(w));
  1448. if (restype == 0 || restype == TYPE_SINGLE_FLOAT ||
  1449. (restype == TYPE_DOUBLE_FLOAT && n == TYPE_LONG_FLOAT))
  1450. restype = n;
  1451. }
  1452. else if (restype == 0) restype = TYPE_SINGLE_FLOAT;
  1453. #else
  1454. restype = TYPE_DOUBLE_FLOAT;
  1455. #endif
  1456. #ifdef COMMON
  1457. if ((is_numbers(a) && is_complex(a)) ||
  1458. (is_numbers(b) && is_complex(b)))
  1459. { c1 = complex_of_number(a);
  1460. c2 = complex_of_number(b);
  1461. c3 = Cpow(c1, c2);
  1462. a = make_boxfloat(c3.real, restype);
  1463. errexit();
  1464. b = make_boxfloat(c3.imag, restype);
  1465. errexit();
  1466. a = make_complex(a, b);
  1467. errexit();
  1468. return onevalue(a);
  1469. }
  1470. #endif
  1471. d = float_of_number(a);
  1472. e = float_of_number(b);
  1473. if (d < 0.0)
  1474. #ifdef COMMON
  1475. { c1.real = d; c1.imag = 0.0;
  1476. c2.real = e; c2.imag = 0.0;
  1477. c3 = Cpow(c1, c2);
  1478. a = make_boxfloat(c3.real, restype);
  1479. errexit();
  1480. b = make_boxfloat(c3.imag, restype);
  1481. errexit();
  1482. a = make_complex(a, b);
  1483. errexit();
  1484. return onevalue(a);
  1485. }
  1486. #else
  1487. return aerror1("bad arg for expt", b);
  1488. #endif
  1489. d = pow(d, e);
  1490. a = make_boxfloat(d, restype);
  1491. errexit();
  1492. return onevalue(a);
  1493. }
  1494. Lisp_Object Llog_2(Lisp_Object nil, Lisp_Object a, Lisp_Object b)
  1495. /*
  1496. * Log with specified base.
  1497. */
  1498. {
  1499. push(b);
  1500. a = Ltrigfn(33, a);
  1501. pop(b);
  1502. errexit();
  1503. push(a);
  1504. b = Ltrigfn(33, b);
  1505. pop(a);
  1506. errexit();
  1507. return quot2(a, b);
  1508. }
  1509. #ifdef COMMON
  1510. static Lisp_Object Lisqrt(Lisp_Object nil, Lisp_Object a)
  1511. {
  1512. double d;
  1513. CSL_IGNORE(nil);
  1514. switch ((int)a & TAG_BITS)
  1515. {
  1516. case TAG_FIXNUM:
  1517. d = (double)int_of_fixnum(a);
  1518. break;
  1519. case TAG_NUMBERS:
  1520. { int32 ha = type_of_header(numhdr(a));
  1521. switch (ha)
  1522. {
  1523. case TYPE_BIGNUM:
  1524. d = float_of_number(a);
  1525. break;
  1526. default:
  1527. return aerror1("bad arg for isqrt", a);
  1528. }
  1529. break;
  1530. }
  1531. default:
  1532. return aerror1("bad arg for isqrt", a);
  1533. }
  1534. d = sqrt(d);
  1535. /* /* This is not anything like good enough yet */
  1536. return onevalue(fixnum_of_int((int32)d));
  1537. }
  1538. #endif
  1539. Lisp_Object Labsval(Lisp_Object nil, Lisp_Object a)
  1540. /*
  1541. * I call this Labsval not Labs because a non-case-sensitive linker
  1542. * would confuse Labs with labs, and labs is defined in the C libraries...
  1543. * Of course I do not think that case-insensitive linkers should be allowed
  1544. * to remain in service....
  1545. */
  1546. {
  1547. switch ((int)a & TAG_BITS)
  1548. {
  1549. case TAG_FIXNUM:
  1550. #ifdef COMMON
  1551. case TAG_SFLOAT:
  1552. #endif
  1553. break;
  1554. case TAG_NUMBERS:
  1555. { int32 ha = type_of_header(numhdr(a));
  1556. switch (ha)
  1557. {
  1558. case TYPE_BIGNUM:
  1559. #ifdef COMMON
  1560. case TYPE_RATNUM:
  1561. #endif
  1562. break;
  1563. #ifdef COMMON
  1564. case TYPE_COMPLEX_NUM:
  1565. { Complex c1;
  1566. double d;
  1567. c1.real = float_of_number(real_part(a));
  1568. c1.imag = float_of_number(imag_part(a));
  1569. d = Cabs(c1);
  1570. /* /* I wonder if I am allowed to promote short or single values to
  1571. double precision here? */
  1572. a = make_boxfloat(d, TYPE_DOUBLE_FLOAT);
  1573. errexit();
  1574. return onevalue(a);
  1575. }
  1576. #endif
  1577. default:
  1578. return aerror1("bad arg for abs", a);
  1579. }
  1580. break;
  1581. }
  1582. case TAG_BOXFLOAT:
  1583. break;
  1584. default:
  1585. return aerror1("bad arg for abs", a);
  1586. }
  1587. if (minusp(a))
  1588. { nil = C_nil;
  1589. if (!exception_pending()) a = negate(a);
  1590. }
  1591. errexit();
  1592. return onevalue(a);
  1593. }
  1594. #ifdef COMMON
  1595. static Lisp_Object Lphase(Lisp_Object nil, Lisp_Object a)
  1596. {
  1597. CSLbool s;
  1598. double d;
  1599. if (is_numbers(a) && is_complex(a))
  1600. return Latan2(nil, imag_part(a), real_part(a));
  1601. s = minusp(a);
  1602. errexit();
  1603. if (s) d = -_pi;
  1604. else d = _pi;
  1605. a = make_boxfloat(d, TYPE_DOUBLE_FLOAT);
  1606. errexit();
  1607. return onevalue(a);
  1608. /* /* Wrong precision, I guess */
  1609. }
  1610. static Lisp_Object Lsignum(Lisp_Object nil, Lisp_Object a)
  1611. {
  1612. /*
  1613. * This seems an expensive way of doing things - huh? Maybe complex values? */
  1614. CSLbool z;
  1615. Lisp_Object w;
  1616. z = zerop(a);
  1617. nil = C_nil;
  1618. if (z || exception_pending()) return onevalue(a);
  1619. push(a);
  1620. w = Labsval(nil, a);
  1621. pop(a);
  1622. errexit();
  1623. a = quot2(a, w);
  1624. errexit();
  1625. return onevalue(a);
  1626. }
  1627. static Lisp_Object Lcis(Lisp_Object env, Lisp_Object a)
  1628. /*
  1629. * Implement as exp(i*a) - this permits complex args which goes
  1630. * beyond the specification of Common Lisp.
  1631. */
  1632. {
  1633. Lisp_Object ii, nil;
  1634. CSL_IGNORE(env);
  1635. push(a);
  1636. ii = make_complex(fixnum_of_int(0), fixnum_of_int(1));
  1637. pop(a);
  1638. errexit();
  1639. /*
  1640. * it seems a bit gross to multiply by i by calling times2(), but
  1641. * doing so avoids loads of messy type dispatch code here and
  1642. * I am not over-worried about performance at this level (yet).
  1643. */
  1644. a = times2(a, ii);
  1645. errexit();
  1646. return Ltrigfn(30, a); /* exp() */
  1647. }
  1648. #endif
  1649. #ifdef COMMON
  1650. Lisp_Object Latan(Lisp_Object env, Lisp_Object a)
  1651. {
  1652. CSL_IGNORE(env);
  1653. return Ltrigfn(15, a); /* atan() */
  1654. }
  1655. Lisp_Object Latan_2(Lisp_Object env, Lisp_Object a, Lisp_Object b)
  1656. {
  1657. CSL_IGNORE(env);
  1658. return Latan2(env, a, b);
  1659. }
  1660. #else
  1661. Lisp_Object Latan(Lisp_Object env, Lisp_Object a)
  1662. {
  1663. CSL_IGNORE(env);
  1664. return Ltrigfn(15, a); /* atan() */
  1665. }
  1666. #endif
  1667. Lisp_Object Latan2(Lisp_Object nil, Lisp_Object a, Lisp_Object b)
  1668. {
  1669. double u, v, r;
  1670. int q = 0;
  1671. v = float_of_number(a);
  1672. u = float_of_number(b);
  1673. if (u < 0.0) u = -u, q = 1;
  1674. if (v < 0.0) v = -v, q |= 2;
  1675. if (v > u) { r = u; u = v; v = r; q |= 4; }
  1676. if (u == 0.0 && v == 0.0) r = 0.0;
  1677. else
  1678. { r = atan(v/u);
  1679. switch (q)
  1680. {
  1681. default:
  1682. case 0: break;
  1683. case 1: r = _pi - r;
  1684. break;
  1685. case 2: r = -r;
  1686. break;
  1687. case 3: r = -_pi + r;
  1688. break;
  1689. case 4: r = _half_pi - r;
  1690. break;
  1691. case 5: r = _half_pi + r;
  1692. break;
  1693. case 6: r = -_half_pi + r;
  1694. break;
  1695. case 7: r = -_half_pi - r;
  1696. break;
  1697. }
  1698. }
  1699. a = make_boxfloat(r, TYPE_DOUBLE_FLOAT);
  1700. errexit();
  1701. return onevalue(a);
  1702. }
  1703. Lisp_Object Latan2d(Lisp_Object nil, Lisp_Object a, Lisp_Object b)
  1704. {
  1705. double u, v, r;
  1706. int q = 0;
  1707. v = float_of_number(a);
  1708. u = float_of_number(b);
  1709. if (u < 0.0) u = -u, q = 1;
  1710. if (v < 0.0) v = -v, q |= 2;
  1711. if (v > u) { r = u; u = v; v = r; q |= 4; }
  1712. if (u == 0.0 && v == 0.0) r = 0.0;
  1713. else
  1714. { r = n180pi*atan(v/u);
  1715. switch (q)
  1716. {
  1717. default:
  1718. case 0: break;
  1719. case 1: r = 180.0 - r;
  1720. break;
  1721. case 2: r = -r;
  1722. break;
  1723. case 3: r = -180.0 + r;
  1724. break;
  1725. case 4: r = 90.0 - r;
  1726. break;
  1727. case 5: r = 90.0 + r;
  1728. break;
  1729. case 6: r = -90.0 + r;
  1730. break;
  1731. case 7: r = -90.0 - r;
  1732. break;
  1733. }
  1734. }
  1735. a = make_boxfloat(r, TYPE_DOUBLE_FLOAT);
  1736. errexit();
  1737. return onevalue(a);
  1738. }
  1739. Lisp_Object Lacos(Lisp_Object nil, Lisp_Object a)
  1740. {
  1741. CSL_IGNORE(nil);
  1742. return Ltrigfn(0, a);
  1743. }
  1744. Lisp_Object Lacosd(Lisp_Object nil, Lisp_Object a)
  1745. {
  1746. CSL_IGNORE(nil);
  1747. return Ltrigfn(1, a);
  1748. }
  1749. Lisp_Object Lacosh(Lisp_Object nil, Lisp_Object a)
  1750. {
  1751. CSL_IGNORE(nil);
  1752. return Ltrigfn(2, a);
  1753. }
  1754. Lisp_Object Lacot(Lisp_Object nil, Lisp_Object a)
  1755. {
  1756. CSL_IGNORE(nil);
  1757. return Ltrigfn(3, a);
  1758. }
  1759. Lisp_Object Lacotd(Lisp_Object nil, Lisp_Object a)
  1760. {
  1761. CSL_IGNORE(nil);
  1762. return Ltrigfn(4, a);
  1763. }
  1764. Lisp_Object Lacoth(Lisp_Object nil, Lisp_Object a)
  1765. {
  1766. CSL_IGNORE(nil);
  1767. return Ltrigfn(5, a);
  1768. }
  1769. Lisp_Object Lacsc(Lisp_Object nil, Lisp_Object a)
  1770. {
  1771. CSL_IGNORE(nil);
  1772. return Ltrigfn(6, a);
  1773. }
  1774. Lisp_Object Lacscd(Lisp_Object nil, Lisp_Object a)
  1775. {
  1776. CSL_IGNORE(nil);
  1777. return Ltrigfn(7, a);
  1778. }
  1779. Lisp_Object Lacsch(Lisp_Object nil, Lisp_Object a)
  1780. {
  1781. CSL_IGNORE(nil);
  1782. return Ltrigfn(8, a);
  1783. }
  1784. Lisp_Object Lasec(Lisp_Object nil, Lisp_Object a)
  1785. {
  1786. CSL_IGNORE(nil);
  1787. return Ltrigfn(9, a);
  1788. }
  1789. Lisp_Object Lasecd(Lisp_Object nil, Lisp_Object a)
  1790. {
  1791. CSL_IGNORE(nil);
  1792. return Ltrigfn(10, a);
  1793. }
  1794. Lisp_Object Lasech(Lisp_Object nil, Lisp_Object a)
  1795. {
  1796. CSL_IGNORE(nil);
  1797. return Ltrigfn(11, a);
  1798. }
  1799. Lisp_Object Lasin(Lisp_Object nil, Lisp_Object a)
  1800. {
  1801. CSL_IGNORE(nil);
  1802. return Ltrigfn(12, a);
  1803. }
  1804. Lisp_Object Lasind(Lisp_Object nil, Lisp_Object a)
  1805. {
  1806. CSL_IGNORE(nil);
  1807. return Ltrigfn(13, a);
  1808. }
  1809. Lisp_Object Lasinh(Lisp_Object nil, Lisp_Object a)
  1810. {
  1811. CSL_IGNORE(nil);
  1812. return Ltrigfn(14, a);
  1813. }
  1814. /* code 15 is for atan */
  1815. Lisp_Object Latand(Lisp_Object nil, Lisp_Object a)
  1816. {
  1817. CSL_IGNORE(nil);
  1818. return Ltrigfn(16, a);
  1819. }
  1820. /* code 17 is atan2, 18 is atan2d */
  1821. Lisp_Object Latanh(Lisp_Object nil, Lisp_Object a)
  1822. {
  1823. CSL_IGNORE(nil);
  1824. return Ltrigfn(19, a);
  1825. }
  1826. Lisp_Object Lcbrt(Lisp_Object nil, Lisp_Object a)
  1827. {
  1828. CSL_IGNORE(nil);
  1829. return Ltrigfn(20, a);
  1830. }
  1831. Lisp_Object Lcos(Lisp_Object nil, Lisp_Object a)
  1832. {
  1833. CSL_IGNORE(nil);
  1834. return Ltrigfn(21, a);
  1835. }
  1836. Lisp_Object Lcosd(Lisp_Object nil, Lisp_Object a)
  1837. {
  1838. CSL_IGNORE(nil);
  1839. return Ltrigfn(22, a);
  1840. }
  1841. Lisp_Object Lcosh(Lisp_Object nil, Lisp_Object a)
  1842. {
  1843. CSL_IGNORE(nil);
  1844. return Ltrigfn(23, a);
  1845. }
  1846. Lisp_Object Lcot(Lisp_Object nil, Lisp_Object a)
  1847. {
  1848. CSL_IGNORE(nil);
  1849. return Ltrigfn(24, a);
  1850. }
  1851. Lisp_Object Lcotd(Lisp_Object nil, Lisp_Object a)
  1852. {
  1853. CSL_IGNORE(nil);
  1854. return Ltrigfn(25, a);
  1855. }
  1856. Lisp_Object Lcoth(Lisp_Object nil, Lisp_Object a)
  1857. {
  1858. CSL_IGNORE(nil);
  1859. return Ltrigfn(26, a);
  1860. }
  1861. Lisp_Object Lcsc(Lisp_Object nil, Lisp_Object a)
  1862. {
  1863. CSL_IGNORE(nil);
  1864. return Ltrigfn(27, a);
  1865. }
  1866. Lisp_Object Lcscd(Lisp_Object nil, Lisp_Object a)
  1867. {
  1868. CSL_IGNORE(nil);
  1869. return Ltrigfn(28, a);
  1870. }
  1871. Lisp_Object Lcsch(Lisp_Object nil, Lisp_Object a)
  1872. {
  1873. CSL_IGNORE(nil);
  1874. return Ltrigfn(29, a);
  1875. }
  1876. Lisp_Object Lexp(Lisp_Object nil, Lisp_Object a)
  1877. {
  1878. CSL_IGNORE(nil);
  1879. return Ltrigfn(30, a);
  1880. }
  1881. /* 31 is expt, 32 is hypot */
  1882. Lisp_Object Lln(Lisp_Object nil, Lisp_Object a)
  1883. {
  1884. CSL_IGNORE(nil);
  1885. return Ltrigfn(33, a);
  1886. }
  1887. /* 34 is 2-arg log */
  1888. Lisp_Object Llog10(Lisp_Object nil, Lisp_Object a)
  1889. {
  1890. CSL_IGNORE(nil);
  1891. return Ltrigfn(35, a);
  1892. }
  1893. Lisp_Object Lsec(Lisp_Object nil, Lisp_Object a)
  1894. {
  1895. CSL_IGNORE(nil);
  1896. return Ltrigfn(36, a);
  1897. }
  1898. Lisp_Object Lsecd(Lisp_Object nil, Lisp_Object a)
  1899. {
  1900. CSL_IGNORE(nil);
  1901. return Ltrigfn(37, a);
  1902. }
  1903. Lisp_Object Lsech(Lisp_Object nil, Lisp_Object a)
  1904. {
  1905. CSL_IGNORE(nil);
  1906. return Ltrigfn(38, a);
  1907. }
  1908. Lisp_Object Lsin(Lisp_Object nil, Lisp_Object a)
  1909. {
  1910. CSL_IGNORE(nil);
  1911. return Ltrigfn(39, a);
  1912. }
  1913. Lisp_Object Lsind(Lisp_Object nil, Lisp_Object a)
  1914. {
  1915. CSL_IGNORE(nil);
  1916. return Ltrigfn(40, a);
  1917. }
  1918. Lisp_Object Lsinh(Lisp_Object nil, Lisp_Object a)
  1919. {
  1920. CSL_IGNORE(nil);
  1921. return Ltrigfn(41, a);
  1922. }
  1923. Lisp_Object Lsqrt(Lisp_Object nil, Lisp_Object a)
  1924. {
  1925. CSL_IGNORE(nil);
  1926. return Ltrigfn(42, a);
  1927. }
  1928. Lisp_Object Ltan(Lisp_Object nil, Lisp_Object a)
  1929. {
  1930. CSL_IGNORE(nil);
  1931. return Ltrigfn(43, a);
  1932. }
  1933. Lisp_Object Ltand(Lisp_Object nil, Lisp_Object a)
  1934. {
  1935. CSL_IGNORE(nil);
  1936. return Ltrigfn(44, a);
  1937. }
  1938. Lisp_Object Ltanh(Lisp_Object nil, Lisp_Object a)
  1939. {
  1940. CSL_IGNORE(nil);
  1941. return Ltrigfn(45, a);
  1942. }
  1943. setup_type const arith10_setup[] =
  1944. {
  1945. {"abs", Labsval, too_many_1, wrong_no_1},
  1946. {"acos", Lacos, too_many_1, wrong_no_1},
  1947. {"acosd", Lacosd, too_many_1, wrong_no_1},
  1948. {"acosh", Lacosh, too_many_1, wrong_no_1},
  1949. {"acot", Lacot, too_many_1, wrong_no_1},
  1950. {"acotd", Lacotd, too_many_1, wrong_no_1},
  1951. {"acoth", Lacoth, too_many_1, wrong_no_1},
  1952. {"acsc", Lacsc, too_many_1, wrong_no_1},
  1953. {"acscd", Lacscd, too_many_1, wrong_no_1},
  1954. {"acsch", Lacsch, too_many_1, wrong_no_1},
  1955. {"asec", Lasec, too_many_1, wrong_no_1},
  1956. {"asecd", Lasecd, too_many_1, wrong_no_1},
  1957. {"asech", Lasech, too_many_1, wrong_no_1},
  1958. {"asin", Lasin, too_many_1, wrong_no_1},
  1959. {"asind", Lasind, too_many_1, wrong_no_1},
  1960. {"asinh", Lasinh, too_many_1, wrong_no_1},
  1961. {"atand", Latand, too_many_1, wrong_no_1},
  1962. {"atan2", too_few_2, Latan2, wrong_no_2},
  1963. {"atan2d", too_few_2, Latan2d, wrong_no_2},
  1964. {"atanh", Latanh, too_many_1, wrong_no_1},
  1965. {"cbrt", Lcbrt, too_many_1, wrong_no_1},
  1966. {"cos", Lcos, too_many_1, wrong_no_1},
  1967. {"cosd", Lcosd, too_many_1, wrong_no_1},
  1968. {"cosh", Lcosh, too_many_1, wrong_no_1},
  1969. {"cot", Lcot, too_many_1, wrong_no_1},
  1970. {"cotd", Lcotd, too_many_1, wrong_no_1},
  1971. {"coth", Lcoth, too_many_1, wrong_no_1},
  1972. {"csc", Lcsc, too_many_1, wrong_no_1},
  1973. {"cscd", Lcscd, too_many_1, wrong_no_1},
  1974. {"csch", Lcsch, too_many_1, wrong_no_1},
  1975. {"exp", Lexp, too_many_1, wrong_no_1},
  1976. {"expt", too_few_2, Lexpt, wrong_no_2},
  1977. {"hypot", too_few_2, Lhypot, wrong_no_2},
  1978. {"ln", Lln, too_many_1, wrong_no_1},
  1979. {"log", Lln, Llog_2, wrong_no_2},
  1980. {"log10", Llog10, too_many_1, wrong_no_1},
  1981. {"sec", Lsec, too_many_1, wrong_no_1},
  1982. {"secd", Lsecd, too_many_1, wrong_no_1},
  1983. {"sech", Lsech, too_many_1, wrong_no_1},
  1984. {"sin", Lsin, too_many_1, wrong_no_1},
  1985. {"sind", Lsind, too_many_1, wrong_no_1},
  1986. {"sinh", Lsinh, too_many_1, wrong_no_1},
  1987. {"sqrt", Lsqrt, too_many_1, wrong_no_1},
  1988. {"tan", Ltan, too_many_1, wrong_no_1},
  1989. {"tand", Ltand, too_many_1, wrong_no_1},
  1990. {"tanh", Ltanh, too_many_1, wrong_no_1},
  1991. #ifdef COMMON
  1992. {"cis", Lcis, too_many_1, wrong_no_1},
  1993. {"isqrt", Lisqrt, too_many_1, wrong_no_1},
  1994. {"phase", Lphase, too_many_1, wrong_no_1},
  1995. {"signum", Lsignum, too_many_1, wrong_no_1},
  1996. {"atan", Latan, Latan_2, wrong_no_1},
  1997. #else
  1998. {"atan", Latan, too_many_1, wrong_no_1},
  1999. {"logb", too_few_2, Llog_2, wrong_no_2},
  2000. #endif
  2001. {NULL, 0, 0, 0}
  2002. };
  2003. /* end of arith10.c */