arith10.c 59 KB

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