# Transcendental Function Evaluation

The following are notes from my program Extra Precision Floating-Point Calculator XPCalc:

Transcendental Function Evaluation -

All transcendental functions, Sin(x), Cos(x), Tan(x), Cot(x), Sec(x), Csc(x), ASin(x), ACos(x), ATan(x), ATan2(y, x), ACot(x), ASec(x), ACsc(x), Exp(x), ExpL(x), Pow(x, y), Ln(x), LnL(x), LogB(b, x), Log(x), Sinh(x), Cosh(x), Tanh(x), Coth(x), Sech(x), Csch(x), ASinh(x), ACosh(x), ATanh(x), ACoth(x), ASech(x), and ACsch(x), when they are evaluated, ends up using one of the four basic transcendental functions, Sin(x), ATan(x), ExpL(x), and LnL(x). The methods used by these four functions are quite similar: 1) For F(x), reduce the given argument x to a related argument f. 2) Further reduce f, NN times in a recursive loop to produce an argument g much smaller than f. 3) Evaluate the Taylor series for the argument g. 4) Reconstruct F(f) from F(g) by a recursive process executed NN times. 5) Reconstruct the desired function value F(x) from F(f).

The number NN in steps 2) and 4) is computed by a heuristic equation of the form NN = a + b * SqRt(M) where a and b are constants and M is the current max decimal digits in a mantissa. The best value of NN is the value that produces the smallest total execution time. After step 4) a best value of NN is computed and output by estimating a value of NN that would have made the running time of step 3) equal the sum of the running time of steps 2) and 4). Best NN = NN * SqRt(T3 / (T2 + T4)). Where Tn is the time to execute step n). This equation is based on T2 and T4 being proportional to NN and T3 being inversely proportional to NN.

If the operator wants to control the value on NN, he can enter a value on the list for item MSinNN, MATanNN, MExpLNN, MLnLNN to control the value used for NN in the Sin(x), ATan(x), ExpL(x), and LnL(x) functions respectively.

The recursive method used to reduce the argument for Sin(x) is based on the equation: Sin(x) = Sin(x/3) * (3 - 4 * Sq(Sin(x/3))). In step 2) f is divided by 3, NN times to produce g. In step 4) the recursion: S = S * (3 - 4 * Sq(S)), is performed NN times, where S is initially the value of Sin(g) produced in step 3) and the final value is Sin(f).

The recursive method used to reduce the argument for ATan(x) is based on the equation: Tan(x/2) = Tan(x) / (1 + SqRt(1+Sq(Tan(x))). In step 2) the recursion: T = T / (1 + SqRt(1 + Sq(T))), is performed NN times, where T is initially the value of f from step 1) and the final value of T is the value of g for step 3). In step 4) the angle value, A = ATan(g), produced in step 3) is multiplied by 2, NN times to produce ATan(f).

The recursive method used to reduce the argument for ExpL(x) is based on the equation: Exp(x) = Sq(Exp(x/2). In step 2) f is divided by 2, NN times to produce g. In step 4) the recursion: a = a * (2 + a), is performed NN times, where a is initially the value of ExpL(g) produced in step 3) and the final value is ExpL(f). The recursion a = a * (2 + a) is equivalent to, but more accurate than, the recursion e = Sq(e), where e = a + 1; The recursive method used to reduce the argument for LnL(y) is based on the equation: Ln(x) = 2 * Ln(SqRt(x)). In step 2) the recursion: y = y / (1 + SqRt(1 + y)), is performed NN times, where y is initially the value of f from step 1) and the final value of y is the value of g for step 3). In step 4) the log value L = LnL(g) produced in step 3) is multiplied by 2, NN times to produce LnL(f). The recursion y = y / (1 + SqRt(1 + y)) is equivalent to, but more accurate than, the recursion x = SqRt(x), where y = x - 1;

If the diagnostic mode is on, the values computed for NN in the four subroutines MSin, MATan, MExpL, and MLnL are displayed, for example, as:

MExpL: NN = 22.299
MExpL: NN = 21
Best NN = 21.935 +/- 1.633

In this example the MExpL subroutine estimated NN to be 22.299. A value of NN = 21 was actually used (this is not 22 because the number being worked on was less than 3, the base number used to generate the heuristic equation). Based on the actual timing of the run, the best value for NN is computed to be 21.935. Due to the uncertainty of the timing, the Best NN could be off by + or - 1.633.

The Taylor series used for Sin(x) is:

Sin(x) = x - x^3 / 3! + x^5 / 5! ...

The Taylor series used for ATan(x) is:

ATan(x) = x - x^3 / 3 + x^5 / 5 ...

The Taylor series used for ExpL(x) is:

ExpL(x) = x + x^2 / 2! + x^3 / 3! ...

The Taylor series used for LnL(y) is:

LnL(y) = Ln(1+y) = Ln((1+z)/(1-z)) = 2 * (z + z^3/3 + z^5/5 ...)
Where x = 1+y = (1+z) / (1-z),
y = x-1 = 2 * z / (1-z),
z = (x-1) / (x+1) = y / (2+z).

Other equations used to produce the transcendental functions:

Cos(x) = Sin(x + Pi/2).

Tan(x) = Sin(x) / SqRt(1 - Sq(Sin(x)), and change sign of Tan(x) if in
2nd or 3rd quadrant, but error if x is equivalent to plus or minus 90
degrees.

ASin(S) = ATan2(S, SqRt(1 - Sq(S)), but error if |S| > 1.

ACos(C) = ATan2(SqRt(1 - Sq(C), C), but error if |C| > 1.

Log(x) = Ln(x) / Ln(10), but error if x <= 0.

For x >= 0.1, Sinh(x) = (y - 1/y) / 2, where y = Exp(x),
for x < 0.1, Sinh(x) = y / (2 * SqRt(y+1)),
here y = ExpL(2*x), and Sinh(-x) = -Sinh(x).

Cosh(x) = (y + 1/y) / 2, where y = Exp(|x|).

Tanh(x) = y / (y + 2), where y = ExpL(2 * x),
and Tanh(-x) = -Tanh(x).

For x >= 0.1, ASinh(x) = Ln(x + SqRt(1 + Sq(x))),
for x < 0.1, ASinh(x) = LnL(x + Sq(x) / SqRt(1 + Sq(x))),
and ASinh(-x) = -ASinh(x).

ACosh(x) = Ln(x + SqRt(Sq(x) - 1)), but error if x < 1.

ATanh(x) = LnL(2 * x / (1 - x)), but error if |x| >= 1,
and ATanh(-x) = -ATanh(x).