2 * qtrans - transcendental functions for real numbers
4 * Copyright (C) 1999-2007 David I. Bell and Ernest Bowen
6 * Primary author: David I. Bell
8 * Calc is open software; you can redistribute it and/or modify it under
9 * the terms of the version 2.1 of the GNU Lesser General Public License
10 * as published by the Free Software Foundation.
12 * Calc is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
15 * Public License for more details.
17 * A copy of version 2.1 of the GNU Lesser General Public License is
18 * distributed with calc under the filename COPYING-LGPL. You should have
19 * received a copy with calc; if not, write to Free Software Foundation, Inc.
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * @(#) $Revision: 30.2 $
23 * @(#) $Id: qtrans.c,v 30.2 2008/10/24 07:09:41 chongo Exp $
24 * @(#) $Source: /usr/local/src/bin/calc/RCS/qtrans.c,v $
26 * Under source code control: 1990/02/15 01:48:22
27 * File existed as early as: before 1990
29 * Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/
33 * Transcendental functions for real numbers.
34 * These are sin, cos, exp, ln, power, cosh, sinh.
40 HALF _qlgenum_
[] = { 36744 };
41 HALF _qlgeden_
[] = { 25469 };
42 NUMBER _qlge_
= { { _qlgenum_
, 1, 0 }, { _qlgeden_
, 1, 0 }, 1, NULL
};
45 * cache the natural logarithm of 10
47 STATIC NUMBER
*ln_10
= NULL
;
48 STATIC NUMBER
*ln_10_epsilon
= NULL
;
50 STATIC NUMBER
*pivalue
[2];
51 STATIC NUMBER
*qexprel(NUMBER
*q
, long bitnum
);
54 * Evaluate and store in specified locations the sin and cos of a given
55 * number to accuracy corresponding to a specified number of binary digits.
58 qsincos(NUMBER
*q
, long bitnum
, NUMBER
**vs
, NUMBER
**vc
)
60 long n
, m
, k
, h
, s
, t
, d
;
61 NUMBER
*qtmp1
, *qtmp2
;
62 ZVALUE X
, cossum
, sinsum
, mul
, ztmp1
, ztmp2
, ztmp3
;
69 *vs
= qlink(&_qzero_
);
80 } /* s is int(sqrt(k)) */
84 n
= h
+ s
; /* n is number of squarings that will be required */
86 while (s
> 0) { /* increasing m by ilog2(s) */
89 } /* m is working number of bits */
90 qtmp1
= qscale(q
, m
- n
);
91 zquo(qtmp1
->num
, qtmp1
->den
, &X
, 24);
95 *vs
= qlink(&_qzero_
);
99 zbitvalue(m
, &cossum
);
105 zmul(X
, mul
, &ztmp1
);
107 zshift(ztmp1
, -m
, &ztmp2
);
109 zdivi(ztmp2
, ++d
, &X
);
113 zadd(cossum
, X
, &ztmp1
);
116 zmul(X
, mul
, &ztmp1
);
118 zshift(ztmp1
, -m
, &ztmp2
);
120 zdivi(ztmp2
, ++d
, &X
);
124 zadd(sinsum
, X
, &ztmp1
);
131 zsquare(cossum
, &ztmp1
);
132 zsquare(sinsum
, &ztmp2
);
133 zsub(ztmp1
, ztmp2
, &ztmp3
);
136 zmul(cossum
, sinsum
, &ztmp1
);
139 zshift(ztmp3
, -m
, &cossum
);
141 zshift(ztmp1
, 1 - m
, &sinsum
);
147 zshift(cossum
, -h
, &qtmp1
->num
);
148 zbitvalue(m
- h
, &qtmp1
->den
);
150 zshift(cossum
, - m
, &qtmp1
->num
);
157 zshift(sinsum
, -h
, &qtmp2
->num
);
158 zbitvalue(m
- h
, &qtmp2
->den
);
160 zshift(sinsum
, -m
, &qtmp2
->num
);
168 * Calculate the cosine of a number to a near multiple of epsilon.
169 * This calls qsincos() and discards the value of sin.
172 qcos(NUMBER
*q
, NUMBER
*epsilon
)
174 NUMBER
*sin
, *cos
, *res
;
177 if (qiszero(epsilon
)) {
178 math_error("Zero epsilon value for cosine");
182 return qlink(&_qone_
);
183 n
= -qilog2(epsilon
);
185 return qlink(&_qzero_
);
186 qsincos(q
, n
+ 2, &sin
, &cos
);
188 res
= qmappr(cos
, epsilon
, 24);
196 * This calls qsincos() and discards the value of cos.
199 qsin(NUMBER
*q
, NUMBER
*epsilon
)
201 NUMBER
*sin
, *cos
, *res
;
204 if (qiszero(epsilon
)) {
205 math_error("Zero epsilon value for sine");
208 n
= -qilog2(epsilon
);
209 if (qiszero(q
) || n
< 0)
210 return qlink(&_qzero_
);
211 qsincos(q
, n
+ 2, &sin
, &cos
);
213 res
= qmappr(sin
, epsilon
, 24);
220 * Calculate the tangent function.
223 qtan(NUMBER
*q
, NUMBER
*epsilon
)
225 NUMBER
*sin
, *cos
, *tan
, *res
;
228 if (qiszero(epsilon
)) {
229 math_error("Zero epsilon value for tangent");
235 k
= (n
> 0) ? 4 + n
/2 : 4;
237 qsincos(q
, 2 * k
- n
, &sin
, &cos
);
251 tan
= qqdiv(sin
, cos
);
254 res
= qmappr(tan
, epsilon
, 24);
261 * Calculate the cotangent function.
264 qcot(NUMBER
*q
, NUMBER
*epsilon
)
266 NUMBER
*sin
, *cos
, *cot
, *res
;
269 if (qiszero(epsilon
)) {
270 math_error("Zero epsilon value for cotangent");
274 math_error("Zero argument for cotangent");
280 k
= (n
> 0) ? n
/2 : 0;
283 qsincos(q
, 2 * k
- n
, &sin
, &cos
);
297 cot
= qqdiv(cos
, sin
);
300 res
= qmappr(cot
, epsilon
, 24);
307 * Calculate the secant function.
310 qsec(NUMBER
*q
, NUMBER
*epsilon
)
312 NUMBER
*sin
, *cos
, *sec
, *res
;
315 if (qiszero(epsilon
)) {
316 math_error("Zero epsilon value for secant");
320 return qlink(&_qone_
);
322 k
= (n
> 0) ? 4 + n
/2 : 4;
324 qsincos(q
, 2 * k
- n
, &sin
, &cos
);
339 res
= qmappr(sec
, epsilon
, 24);
346 * Calculate the cosecant function.
349 qcsc(NUMBER
*q
, NUMBER
*epsilon
)
351 NUMBER
*sin
, *cos
, *csc
, *res
;
354 if (qiszero(epsilon
)) {
355 math_error("Zero epsilon value for cosecant");
359 math_error("Zero argument for cosecant");
365 k
= (n
> 0) ? n
/2 : 0;
368 qsincos(q
, 2 * k
- n
, &sin
, &cos
);
383 res
= qmappr(csc
, epsilon
, 24);
388 * Calculate the arcsine function.
389 * The result is in the range -pi/2 to pi/2.
392 qasin(NUMBER
*q
, NUMBER
*epsilon
)
394 NUMBER
*qtmp1
, *qtmp2
, *epsilon1
;
399 if (qiszero(epsilon
)) {
400 math_error("Zero epsilon value for asin");
404 return qlink(&_qzero_
);
408 r
= zrel(ztmp
, q
->den
);
412 epsilon1
= qscale(epsilon
, 1L);
413 qtmp2
= qpi(epsilon1
);
414 qtmp1
= qscale(qtmp2
, -1L);
416 epsilon1
= qscale(epsilon
, -2L);
418 zsquare(q
->num
, &qtmp1
->num
);
419 zsquare(q
->den
, &ztmp
);
420 zsub(ztmp
, qtmp1
->num
, &qtmp1
->den
);
422 qtmp2
= qsqrt(qtmp1
, epsilon1
, 24);
424 qtmp1
= qatan(qtmp2
, epsilon
);
439 * Calculate the acos function.
440 * The result is in the range 0 to pi.
443 qacos(NUMBER
*q
, NUMBER
*epsilon
)
445 NUMBER
*q1
, *q2
, *epsilon1
;
448 if (qiszero(epsilon
)) {
449 math_error("Zero epsilon value for acos");
453 return qlink(&_qzero_
);
459 if (zrel(z
, q
->den
) > 0)
461 epsilon1
= qscale(epsilon
, -3L); /* ??? */
463 zsub(q
->den
, q
->num
, &q1
->num
);
464 zadd(q
->den
, q
->num
, &q1
->den
);
465 q2
= qsqrt(q1
, epsilon1
, 24L);
468 epsilon1
= qscale(epsilon
, -1L);
469 q1
= qatan(q2
, epsilon1
);
479 * Calculate the arctangent function to the nearest or next to nearest
480 * multiple of epsilon. Algorithm uses
481 * atan(x) = 2 * atan(x/(1 + sqrt(1+x^2)))
482 * to reduce x to a small value and then
483 * atan(x) = x - x^3/3 + ...
486 qatan(NUMBER
*q
, NUMBER
*epsilon
)
490 ZVALUE X
, D
, DD
, sum
, mul
, term
, ztmp1
, ztmp2
;
494 if (qiszero(epsilon
)) {
495 math_error("Zero epsilon value for arctangent");
499 return qlink(&_qzero_
);
500 m
= 12 - qilog2(epsilon
);
501 /* 4 bits for 4 doublings; 8 for rounding */
503 m
= 8; /* m is number of working binary digits */
505 zquo(qtmp
->num
, qtmp
->den
, &X
, 24);
507 zbitvalue(m
, &D
); /* q has become X/D */
509 i
= 4; /* maybe this should be larger */
510 while (i
-- > 0 && !ziszero(X
)) {
512 zadd(ztmp1
, DD
, &ztmp2
);
514 zsqrt(ztmp2
, &ztmp1
, 24L);
516 zadd(ztmp1
, D
, &ztmp2
);
518 zshift(X
, m
, &ztmp1
);
520 zquo(ztmp1
, ztmp2
, &X
, 24L);
528 return qlink(&_qzero_
);
532 zshift(ztmp1
, -m
, &mul
);
538 math_error("Too many terms required for atan");
541 zmul(X
, mul
, &ztmp1
);
543 zshift(ztmp1
, -m
, &X
); /* X now (original X)^d */
551 zadd(sum
, term
, &ztmp1
);
563 zshift(sum
, -k
, &qtmp
->num
);
568 zbitvalue(m
- 4 - k
, &qtmp
->den
);
569 res
= qmappr(qtmp
, epsilon
, 24L);
575 * Inverse secant function
578 qasec(NUMBER
*q
, NUMBER
*epsilon
)
583 res
= qacos(tmp
, epsilon
);
590 * Inverse cosecant function
593 qacsc(NUMBER
*q
, NUMBER
*epsilon
)
598 res
= qasin(tmp
, epsilon
);
605 * Inverse cotangent function
608 qacot(NUMBER
*q
, NUMBER
*epsilon
)
610 NUMBER
*tmp1
, *tmp2
, *tmp3
, *epsilon1
;
612 if (qiszero(epsilon
)) {
613 math_error("Zero epsilon for acot");
617 epsilon1
= qscale(epsilon
, 1L);
618 tmp1
= qpi(epsilon1
);
620 tmp2
= qscale(tmp1
, -1L);
626 tmp2
= qatan(tmp1
, epsilon
);
630 epsilon1
= qscale(epsilon
, -2L);
631 tmp2
= qatan(tmp1
, epsilon1
);
633 tmp1
= qpi(epsilon1
);
635 tmp3
= qqadd(tmp1
, tmp2
);
638 tmp1
= qmappr(tmp3
, epsilon
, 24L);
645 * Calculate the angle which is determined by the point (x,y).
646 * This is the same as atan(y/x) for positive x, but is continuous
647 * except for y = 0, x <= 0. By convention, y is the first argument.
648 * For all x, y, -pi < atan2 <= pi. For example, qatan2(1, -1) = 3/4 * pi.
651 qatan2(NUMBER
*qy
, NUMBER
*qx
, NUMBER
*epsilon
)
653 NUMBER
*tmp1
, *tmp2
, *tmp3
, *epsilon2
;
655 if (qiszero(epsilon
)) {
656 math_error("Zero epsilon value for atan2");
659 if (qiszero(qy
) && qiszero(qx
)) {
660 /* conform to 4.3BSD ANSI/IEEE 754-1985 math lib */
661 return qlink(&_qzero_
);
664 * If the point is on the negative real axis, then the answer is pi.
666 if (qiszero(qy
) && qisneg(qx
))
669 * If the point is in the right half plane, then use the normal atan.
671 if (!qisneg(qx
) && !qiszero(qx
)) {
673 return qlink(&_qzero_
);
674 tmp1
= qqdiv(qy
, qx
);
675 tmp2
= qatan(tmp1
, epsilon
);
680 * The point is in the left half plane (x <= 0) with nonzero y.
681 * Calculate the angle by using the formula:
682 * atan2(y,x) = 2 * atan(sgn(y) * sqrt((x/y)^2 + 1) - x/y).
684 epsilon2
= qscale(epsilon
, -4L);
685 tmp1
= qqdiv(qx
, qy
);
686 tmp2
= qsquare(tmp1
);
687 tmp3
= qqadd(tmp2
, &_qone_
);
689 tmp2
= qsqrt(tmp3
, epsilon2
, 24L | (qy
->num
.sign
* 64));
691 tmp3
= qsub(tmp2
, tmp1
);
695 epsilon2
= qscale(epsilon
, -1L);
696 tmp1
= qatan(tmp3
, epsilon2
);
699 tmp2
= qscale(tmp1
, 1L);
706 * Calculate the value of pi to within the required epsilon.
707 * This uses the following formula which only needs integer calculations
708 * except for the final operation:
709 * pi = 1 / SUMOF(comb(2 * N, N) ^ 3 * (42 * N + 5) / 2 ^ (12 * N + 4)),
710 * where the summation runs from N=0. This formula gives about 6 bits of
711 * accuracy per term. Since the denominator for each term is a power of two,
712 * we can simply use shifts to sum the terms. The combinatorial numbers
713 * in the formula are calculated recursively using the formula:
714 * comb(2*(N+1), N+1) = 2 * comb(2 * N, N) * (2 * N + 1) / N.
719 ZVALUE comb
; /* current combinatorial value */
720 ZVALUE sum
; /* current sum */
722 NUMBER
*r
, *t1
, qtmp
;
723 long shift
; /* current shift of result */
724 long N
; /* current term number */
725 long bits
; /* needed number of bits of precision */
728 if (qiszero(epsilon
)) {
729 math_error("zero epsilon value for pi");
732 if (epsilon
== pivalue
[0])
733 return qlink(pivalue
[1]);
738 bits
= -qilog2(epsilon
) + 4;
747 (void) zdivi(comb
, N
/ (3 - t
), &tmp1
);
749 zmuli(tmp1
, t
* (2 * N
- 1), &comb
);
751 zsquare(comb
, &tmp1
);
752 zmul(comb
, tmp1
, &tmp2
);
754 zmuli(tmp2
, 42 * N
+ 5, &tmp1
);
756 zshift(sum
, 12L, &tmp2
);
758 zadd(tmp1
, tmp2
, &sum
);
763 } while ((shift
- t
) < bits
);
767 t1
= qscale(&qtmp
, shift
);
769 r
= qmappr(t1
, epsilon
, 24L);
771 pivalue
[0] = qlink(epsilon
);
772 pivalue
[1] = qlink(r
);
777 * Calculate the exponential function to the nearest or next to nearest
778 * multiple of the positive number epsilon.
781 qexp(NUMBER
*q
, NUMBER
*epsilon
)
786 if (qiszero(epsilon
)) {
787 math_error("Zero epsilon value for exp");
791 return qlink(&_qone_
);
792 tmp1
= qmul(q
, &_qlge_
);
793 m
= qtoi(tmp1
); /* exp(q) < 2^(m+1) or m == MAXLONG */
799 n
= qilog2(epsilon
); /* 2^n <= epsilon < 2^(n+1) */
801 return qlink(&_qzero_
);
803 tmp2
= qexprel(tmp1
, m
- n
+ 1);
812 tmp1
= qmappr(tmp2
, epsilon
, 24L);
819 * Calculate the exponential function with relative error corresponding
820 * to a specified number of significant bits
821 * Requires *q >= 0, bitnum >= 0.
822 * This returns NULL if more than 2^30 working bits would be required.
825 qexprel(NUMBER
*q
, long bitnum
)
827 long n
, m
, k
, h
, s
, t
, d
;
829 ZVALUE X
, B
, sum
, term
, ztmp1
, ztmp2
;
834 return qlink(&_qone_
);
842 } /* s is int(sqrt(k)) */
846 n
= h
+ s
; /* n is number of squarings that will be required */
850 while (s
> 0) { /* increasing m by ilog2(s) */
853 } /* m is working number of bits */
854 qtmp1
= qscale(q
, m
- n
);
855 zquo(qtmp1
->num
, qtmp1
->den
, &X
, 24);
859 return qlink(&_qone_
);
865 zadd(sum
, term
, &ztmp1
);
868 zmul(term
, X
, &ztmp1
);
870 zshift(ztmp1
, -m
, &ztmp2
);
872 zdivi(ztmp2
, ++d
, &term
);
875 while (!ziszero(term
));
879 zbitvalue(2 * m
+ 1, &B
);
882 zsquare(sum
, &ztmp1
);
884 if (zrel(ztmp1
, B
) >= 0) {
885 zshift(ztmp1
, -m
- 1, &sum
);
888 zshift(ztmp1
, -m
, &sum
);
896 zshift(sum
, -h
, &qtmp1
->num
);
897 zbitvalue(m
- h
- k
, &qtmp1
->den
);
900 zshift(sum
, k
- m
, &qtmp1
->num
);
907 * Calculate the natural logarithm of a number accurate to the specified
911 qln(NUMBER
*q
, NUMBER
*epsilon
)
914 ZVALUE term
, sum
, mul
, pow
, X
, D
, B
, ztmp
;
918 if (qiszero(q
) || qiszero(epsilon
)) {
919 math_error("logarithm of 0");
923 return qlink(&_qzero_
);
924 q
= qqabs(q
); /* Ignore sign of q */
925 neg
= (zrel(q
->num
, q
->den
) < 0);
932 m
= -qilog2(epsilon
); /* m will be number of working bits */
938 m
++; /* Add 1 for each sqrt until X < 2 */
940 m
+= 18; /* 8 more sqrts, 8 for rounding, 2 for epsilon/4 */
941 qtmp
= qscale(q
, m
- k
);
942 zquo(qtmp
->num
, qtmp
->den
, &X
, 24L);
946 zbitvalue(m
, &D
); /* Now "q" = X/D */
947 zbitvalue(m
- 8, &ztmp
);
948 zadd(D
, ztmp
, &B
); /* Will take sqrts until X <= B */
951 n
= 1; /* n is to count 1 + number of sqrts */
953 while (k
> 0 || zrel(X
, B
) > 0) {
955 zshift(X
, m
+ (k
& 1), &ztmp
);
962 zsub(X
, D
, &pow
); /* pow, mul used as tmps */
966 zshift(pow
, m
, &ztmp
);
968 zquo(ztmp
, mul
, &pow
, 24); /* pow now (X - D)/(X + D) */
972 zcopy(pow
, &sum
); /* pow is first term of sum */
974 zshift(ztmp
, -m
, &mul
); /* mul is now multiplier for powers */
979 zmul(pow
, mul
, &ztmp
);
981 zshift(ztmp
, -m
, &pow
);
984 zdivi(pow
, d
, &term
); /* Round down div should be round off */
989 zadd(sum
, term
, &ztmp
);
996 qtmp
= qalloc(); /* qtmp is to be 2^n * sum / 2^m */
1000 zshift(sum
, n
- m
, &qtmp
->num
);
1003 zshift(sum
, -k
, &qtmp
->num
);
1008 zbitvalue(m
- k
- n
, &qtmp
->den
);
1010 res
= qmappr(qtmp
, epsilon
, 24L);
1017 * Calculate the base 10 logarithm
1019 * log(q) = ln(q) / ln(10)
1022 qlog(NUMBER
*q
, NUMBER
*epsilon
)
1024 int need_new_ln_10
= TRUE
; /* FALSE => use cached ln_10 value */
1025 NUMBER
*ln_q
; /* ln(x) */
1026 NUMBER
*ret
; /* base 10 logarithm of x */
1029 if (qiszero(q
) || qiszero(epsilon
)) {
1030 math_error("logarithm of 0");
1035 * shortcut for small integer powers of 10
1037 if (qisint(q
) && qispos(q
) && !zge8192b(q
->num
) && ziseven(q
->num
)) {
1038 BOOL is_10_power
; /* TRUE ==> q is a power of 10 */
1039 long ilog_10
; /* integer log base 10 */
1041 /* try for a quick small power of 10 log */
1042 ilog_10
= zlog10(q
->num
, &is_10_power
);
1043 if (is_10_power
== TRUE
) {
1044 /* is small power of 10, return log */
1045 return itoq(ilog_10
);
1047 /* q is an even integer that is not a power of 10 */
1051 * compute ln(c) first
1053 ln_q
= qln(q
, epsilon
);
1055 if (qiszero(ln_q
)) {
1060 * save epsilon for ln(10) if needed
1062 if (ln_10_epsilon
== NULL
) {
1063 /* first time call */
1064 ln_10_epsilon
= qcopy(epsilon
);
1065 } else if (qcmp(ln_10_epsilon
, epsilon
) == TRUE
) {
1066 /* replaced cacheed value with epsilon arg */
1067 qfree(ln_10_epsilon
);
1068 ln_10_epsilon
= qcopy(epsilon
);
1069 } else if (ln_10
!= NULL
) {
1070 /* the previously computed ln(2) is OK to use */
1071 need_new_ln_10
= FALSE
;
1075 * compute ln(10) if needed
1077 if (need_new_ln_10
== TRUE
) {
1078 if (ln_10
!= NULL
) {
1081 ln_10
= qln(&_qten_
, ln_10_epsilon
);
1085 * return ln(q) / ln(10)
1087 ret
= qqdiv(ln_q
, ln_10
);
1094 * Calculate the result of raising one number to the power of another.
1095 * The result is calculated to the nearest or next to nearest multiple of
1099 qpower(NUMBER
*q1
, NUMBER
*q2
, NUMBER
*epsilon
)
1101 NUMBER
*tmp1
, *tmp2
, *epsilon2
;
1102 NUMBER
*q1tmp
, *q2tmp
;
1105 if (qiszero(epsilon
)) {
1106 math_error("Zero epsilon for power");
1109 if (qiszero(q1
) && qisneg(q2
)) {
1110 math_error("Negative power of zero");
1113 if (qiszero(q2
) || qisone(q1
))
1114 return qlink(&_qone_
);
1116 return qlink(&_qzero_
);
1118 math_error("Negative base for qpower");
1122 return qmappr(q1
, epsilon
, 24);
1123 if (zrel(q1
->num
, q1
->den
) < 0) {
1130 if (qisone(q2tmp
)) {
1132 q2tmp
= qmappr(q1tmp
, epsilon
, 24);
1137 n
= qilog2(epsilon
);
1138 if (qisneg(q2tmp
)) {
1141 tmp2
= qmul(tmp1
, q2tmp
);
1145 tmp2
= qqdiv(tmp1
, q1tmp
);
1147 tmp1
= qmul(tmp2
, q2tmp
);
1149 tmp2
= qmul(tmp1
, &_qlge_
);
1155 tmp2
= qmul(tmp1
, q2tmp
);
1159 tmp2
= qmul(tmp1
, q2tmp
);
1161 tmp1
= qmul(tmp2
, &_qlge_
);
1167 if (m
> (1 << 30)) {
1176 return qlink(&_qzero_
);
1178 tmp1
= qqdiv(epsilon
, q2tmp
);
1179 tmp2
= qscale(tmp1
, -m
- 4);
1180 epsilon2
= qqabs(tmp2
);
1183 tmp1
= qln(q1tmp
, epsilon2
);
1185 tmp2
= qmul(tmp1
, q2tmp
);
1192 tmp2
= qexprel(tmp1
, m
- n
+ 3);
1200 tmp1
= qexprel(tmp2
, m
- n
+ 3) ;
1207 tmp2
= qmappr(tmp1
, epsilon
, 24L);
1214 * Calculate the Kth root of a number to within the specified accuracy.
1217 qroot(NUMBER
*q1
, NUMBER
*q2
, NUMBER
*epsilon
)
1219 NUMBER
*tmp1
, *tmp2
;
1222 if (qiszero(epsilon
)) {
1223 math_error("Zero epsilon for root");
1226 if (qisneg(q2
) || qiszero(q2
) || qisfrac(q2
)) {
1227 math_error("Taking bad root of number");
1230 if (qiszero(q1
) || qisone(q1
) || qisone(q2
))
1233 return qsqrt(q1
, epsilon
, 24L);
1236 if (ziseven(q2
->num
)) {
1237 math_error("Taking even root of negative number");
1243 tmp1
= qpower(q1
, tmp2
, epsilon
);
1256 /* Calculate the hyperbolic cosine function to the nearest or next to
1257 * nearest multiple of epsilon.
1258 * This is calculated using cosh(x) = (exp(x) + 1/exp(x))/2;
1261 qcosh(NUMBER
*q
, NUMBER
*epsilon
)
1263 NUMBER
*tmp1
, *tmp2
, *tmp3
, *epsilon1
;
1265 epsilon1
= qscale(epsilon
, -2);
1267 tmp2
= qexp(tmp1
, epsilon1
);
1273 tmp3
= qqadd(tmp1
, tmp2
);
1276 tmp1
= qscale(tmp3
, -1);
1278 tmp2
= qmappr(tmp1
, epsilon
, 24L);
1285 * Calculate the hyperbolic sine to the nearest or next to nearest
1286 * multiple of epsilon.
1287 * This is calculated using sinh(x) = (exp(x) - 1/exp(x))/2.
1290 qsinh(NUMBER
*q
, NUMBER
*epsilon
)
1292 NUMBER
*tmp1
, *tmp2
, *tmp3
, *epsilon1
;
1295 return qlink(&_qzero_
);
1296 epsilon1
= qscale(epsilon
, -3);
1298 tmp2
= qexp(tmp1
, epsilon1
);
1304 tmp3
= qispos(q
) ? qsub(tmp2
, tmp1
) : qsub(tmp1
, tmp2
);
1307 tmp1
= qscale(tmp3
, -1);
1309 tmp2
= qmappr(tmp1
, epsilon
, 24L);
1316 * Calculate the hyperbolic tangent to the nearest or next to nearest
1317 * multiple of epsilon.
1318 * This is calculated using the formulae:
1319 * tanh(x) = 1 or -1 for very large abs(x)
1320 * tanh(x) = (+ or -)(1 - 2 * exp(2 * x)) for large abx(x)
1321 * tanh(x) = (exp(2*x) - 1)/(exp(2*x) + 1) otherwise
1324 qtanh(NUMBER
*q
, NUMBER
*epsilon
)
1326 NUMBER
*tmp1
, *tmp2
, *tmp3
;
1329 n
= qilog2(epsilon
);
1330 if (n
> 0 || qiszero(q
))
1331 return qlink(&_qzero_
);
1334 tmp2
= qmul(tmp1
, &_qlge_
);
1335 m
= qtoi(tmp2
); /* exp(|q|) < 2^(m+1) or m == MAXLONG */
1339 return q
->num
.sign
? qlink(&_qnegone_
) : qlink(&_qone_
);
1341 tmp2
= qscale(tmp1
, 1);
1343 tmp1
= qexprel(tmp2
, 2 + n
);
1346 tmp2
= qqdiv(&_qtwo_
, tmp1
);
1348 tmp1
= qsub(&_qone_
, tmp2
);
1354 tmp1
= qqdiv(tmp2
, tmp3
);
1358 tmp2
= qmappr(tmp1
, epsilon
, 24L);
1370 * Hyperbolic cotangent.
1371 * Calculated using coth(x) = 1 + 2/(exp(2*x) - 1)
1374 qcoth(NUMBER
*q
, NUMBER
*epsilon
)
1376 NUMBER
*tmp1
, *tmp2
, *res
;
1379 if (qiszero(epsilon
)) {
1380 math_error("Zero epsilon value for coth");
1384 math_error("Zero argument for coth");
1387 tmp1
= qscale(q
, 1);
1391 n
= qilog2(epsilon
);
1393 tmp1
= qmul(&_qlge_
, tmp2
);
1402 tmp1
= qexprel(tmp2
, k
);
1406 if (qiszero(tmp2
)) {
1407 math_error("This should not happen ????");
1412 tmp2
= qscale(tmp1
, 1);
1421 res
= qmappr(tmp1
, epsilon
, 24L);
1428 qsech(NUMBER
*q
, NUMBER
*epsilon
)
1430 NUMBER
*tmp1
, *tmp2
, *tmp3
, *res
;
1433 if (qiszero(epsilon
)) {
1434 math_error("Zero epsilon value for sech");
1438 return qlink(&_qone_
);
1442 if (zrel(tmp1
->num
, tmp1
->den
) >= 0) {
1443 tmp2
= qmul(&_qlge_
, tmp1
);
1447 n
= qilog2(epsilon
);
1450 return qlink(&_qzero_
);
1452 tmp2
= qexprel(tmp1
, 4 - k
- n
);
1455 tmp3
= qqadd(tmp1
, tmp2
);
1460 tmp2
= qscale(tmp1
, 1);
1462 res
= qmappr(tmp2
, epsilon
, 24L);
1469 qcsch(NUMBER
*q
, NUMBER
*epsilon
)
1471 NUMBER
*tmp1
, *tmp2
, *tmp3
, *res
;
1474 if (qiszero(epsilon
)) {
1475 math_error("Zero epsilon value for csch");
1479 math_error("Zero argument for csch");
1483 n
= qilog2(epsilon
);
1485 if (zrel(tmp1
->num
, tmp1
->den
) >= 0) {
1486 tmp2
= qmul(&_qlge_
, tmp1
);
1490 k
= 2 * qilog2(tmp1
);
1494 return qlink(&_qzero_
);
1496 tmp2
= qexprel(tmp1
, 4 - k
- n
);
1500 tmp3
= qsub(tmp1
, tmp2
);
1502 tmp3
= qsub(tmp2
, tmp1
);
1507 tmp2
= qscale(tmp1
, 1);
1509 res
= qmappr(tmp2
, epsilon
, 24L);
1516 * Compute the hyperbolic arccosine within the specified accuracy.
1517 * This is calculated using the formula:
1518 * acosh(x) = ln(x + sqrt(x^2 - 1)).
1521 qacosh(NUMBER
*q
, NUMBER
*epsilon
)
1523 NUMBER
*tmp1
, *tmp2
, *epsilon1
;
1526 if (qiszero(epsilon
)) {
1527 math_error("Zero epsilon value for acosh");
1531 return qlink(&_qzero_
);
1532 if (zrel(q
->num
, q
->den
) < 0)
1534 n
= qilog2(epsilon
);
1535 epsilon1
= qbitvalue(n
- 3);
1539 tmp1
= qsqrt(tmp2
, epsilon1
, 24L);
1541 tmp2
= qqadd(tmp1
, q
);
1543 tmp1
= qln(tmp2
, epsilon1
);
1546 tmp2
= qmappr(tmp1
, epsilon
, 24L);
1553 * Compute the hyperbolic arcsine within the specified accuracy.
1554 * This is calculated using the formula:
1555 * asinh(x) = ln(x + sqrt(x^2 + 1)).
1558 qasinh(NUMBER
*q
, NUMBER
*epsilon
)
1560 NUMBER
*tmp1
, *tmp2
, *epsilon1
;
1564 if (qiszero(epsilon
)) {
1565 math_error("Zero epsilon value for asinh");
1569 return qlink(&_qzero_
);
1572 n
= qilog2(epsilon
);
1573 epsilon1
= qbitvalue(n
- 3);
1577 tmp1
= qsqrt(tmp2
, epsilon1
, 24L);
1579 tmp2
= qqadd(tmp1
, q
);
1581 tmp1
= qln(tmp2
, epsilon1
);
1585 tmp2
= qmappr(tmp1
, epsilon
, 24L);
1596 * Compute the hyperbolic arctangent within the specified accuracy.
1597 * This is calculated using the formula:
1598 * atanh(x) = ln((1 + x) / (1 - x)) / 2.
1601 qatanh(NUMBER
*q
, NUMBER
*epsilon
)
1603 NUMBER
*tmp1
, *tmp2
, *tmp3
, *epsilon1
;
1606 if (qiszero(epsilon
)) {
1607 math_error("Zero epsilon value for atanh");
1611 return qlink(&_qzero_
);
1614 if (zrel(z
, q
->den
) >= 0)
1617 tmp2
= qsub(&_qone_
, q
);
1618 tmp3
= qqdiv(tmp1
, tmp2
);
1621 epsilon1
= qscale(epsilon
, 1L);
1622 tmp1
= qln(tmp3
, epsilon1
);
1624 tmp2
= qscale(tmp1
, -1L);
1632 * Inverse hyperbolic secant function
1635 qasech(NUMBER
*q
, NUMBER
*epsilon
)
1640 res
= qacosh(tmp
, epsilon
);
1647 * Inverse hyperbolic cosecant function
1650 qacsch(NUMBER
*q
, NUMBER
*epsilon
)
1655 res
= qasinh(tmp
, epsilon
);
1662 * Inverse hyperbolic cotangent function
1665 qacoth(NUMBER
*q
, NUMBER
*epsilon
)
1670 res
= qatanh(tmp
, epsilon
);