/* -*-C-*- tasin.c */

#include "elefunt.h"

/*
#     program to test asin/acos
#
#     data required
#
#        none
#
#     subprograms required from this package
#
#        machar - an environmental inquiry program providing
#                 information on the floating-point arithmetic
#                 system.  note that the call to machar can
#                 be deleted provided the following four
#                 parameters are assigned the values indicated
#
#                 ibeta  - the radix of the floating-point system
#                 it     - the number of base-ibeta digits in the
#                          significand of a floating-point number
#                 irnd   - 0 if floating-point addition chops,
#                          1 if floating-point addition rounds
#                 minexp - the largest in magnitude negative
#                          integer such that float(ibeta)**minexp
#                          is a positive floating-point number
#
#        ran(k) - a function subprogram returning random real
#                 numbers uniformly distributed over (0,1)
#
#
#     standard fortran subprograms required
#
#         abs, acos, alog, alog10, amax1, asin, float, int, sqrt
#
#
#     latest revision - december 6, 1979
#
#     author - w. j. cody
#              argonne national laboratory
#
#*/

void
tasin(VOID_ARG)
{
    int i,
        ibeta,
        iexp,
        irnd,
        it,
        j,
        k,
        k1,
        k2,
        k3,
        l,
        m,
        machep,
        maxexp,
        minexp,
        n,
        negep,
        ngrd;

    qp_t
        eps,
        epsneg,
        xmax,
        xmin;

    volatile qp_t 
	a,
        ait,
        albeta,
        b,
        beta,
        betap,
        c1,
        c2,
        del,
        half,
        r6,
        r7,
        s,
        sum,
        w,
        x,
        xl,
        xm,
        xn,
        x1,
        y,
        ysq,
        z,
        zz;

    /*******************************************************************/

    (void)qranset(initseed());
    macharl(&ibeta, &it, &irnd, &ngrd, &machep, &negep, &iexp, &minexp,
        &maxexp, &eps, &epsneg, &xmin, &xmax);
    beta = TO_FP_T(ibeta);
    albeta = ALOG(beta);
    half = 0.5e+00L;
    ait = TO_FP_T(it);
    k = (int) (ALOG10(IPOW(beta, it))) + 1;
    if (ibeta != 10)
    {
	c1 = 201.0e+00L / 128.0e+00L;
	c2 = 4.8382679489661923132169163975144209858469968755291049e-04L;
    }
    else
    {
	c1 = 1.57e+00L;
	c2 = 7.9632679489661923132169163975144209858469968755291049e-04L;
    }
    a = -0.125e+00L;
    b = -a;
    n = maxtest();
    xn = TO_FP_T(n);
    l = -1;
    z = ZERO;

    /* random argument accuracy tests */

    for (j = 1; j <= 5; ++j)
    {
	k1 = 0;
	k3 = 0;
	l = -l;
	x1 = ZERO;
	r6 = ZERO;
	r7 = ZERO;
	del = (b - a) / xn;
	xl = a;

	for (i = 1; i <= n; ++i)
	{
	    x = del * RAN() + xl;
	    if (j <= 2)
	    {
		y = x;
		ysq = y * y;
	    }
	    else
	    {
		ysq = half - half * ABS(x);
		x = (half - (ysq + ysq)) + half;
		if (j == 5)
		    x = -x;
		y = SQRT(ysq);
		y = y + y;
	    }
	    sum = ZERO;
	    xm = TO_FP_T(k + k + 1);
	    if (l > 0)
		z = ASIN(x);
	    if (l < 0)
		z = ACOS(x);

	    for (m = 1; m <= k; ++m)
	    {
		sum = ysq * (sum + 1.0e+00L / xm);
		xm = xm - 2.0e+00L;
		sum = sum * (xm / (xm + 1.0e+00L));
	    }

	    sum = sum * y;
	    if (!((j != 1) && (j != 4)))
	    {
		zz = y + sum;
		sum = (y - zz) + sum;
		if (irnd != 1)
		    zz = zz + (sum + sum);
	    }
	    else
	    {
		s = c1 + c2;
		sum = ((c1 - s) + c2) - sum;
		zz = s + sum;
		sum = ((s - zz) + sum) - y;
		s = zz;
		zz = s + sum;
		sum = (s - zz) + sum;
		if (irnd != 1)
		    zz = zz + (sum + sum);
	    }
	    w = 1.0e+00L;
	    if (z != ZERO)
		w = (z - zz) / z;
	    if (w > ZERO)
		k1 = k1 + 1;
	    if (w < ZERO)
		k3 = k3 + 1;
	    w = ABS(w);
	    if (w > r6)
	    {
		r6 = w;
		x1 = x;
	    }
	    r7 = r7 + w * w;
	    xl = a + TO_FP_T(i) * del;	/* OLD: xl = xl + del (bad for large n) */
	}

	k2 = n - k3 - k1;
	r7 = SQRT(r7 / xn);
	if (l < 0)
	{
	    (void)printf("1TEST OF ACOS(X) VS TAYLOR SERIES\n\n");
	    (void)printf("%7d RANDOM ARGUMENTS WERE TESTED FROM THE INTERVAL\n", n);
	    (void)printf("      (%15.4Le,%15.4Le)\n\n\n", a, b);
	    (void)printf(" ACOS(X) WAS LARGER%6d TIMES,\n", k1);
	    (void)printf("             AGREED%6d TIMES, AND\n", k2);
	    (void)printf("        WAS SMALLER%6d TIMES.\n\n", k3);
	}
	else
	{
	    (void)printf("1TEST OF ASIN(X) VS TAYLOR SERIES\n\n");
	    (void)printf("%7d RANDOM ARGUMENTS WERE TESTED FROM THE INTERVAL\n", n);
	    (void)printf("      (%15.4Le,%15.4Le)\n\n\n", a, b);
	    (void)printf(" ASIN(X) WAS LARGER%6d TIMES,\n", k1);
	    (void)printf("             AGREED%6d TIMES, AND\n", k2);
	    (void)printf("        WAS SMALLER%6d TIMES.\n\n", k3);
	}
	(void)printf(" THERE ARE%4d BASE%4d SIGNIFICANT DIGITS IN A FLOATING-POINT NUMBER\n\n",
	    it, ibeta);
	w = -999.0e+00L;
	if (r6 != ZERO)
	    w = ALOG(ABS(r6)) / albeta;
	(void)printf(" THE MAXIMUM RELATIVE ERROR OF%15.4Le = %4d **%7.2Lf\n",
	    r6, ibeta, w);
	(void)printf("    OCCURRED FOR X =%17.6Le\n", x1);
	w = AMAX1(ait + w, ZERO);
	(void)printf(
	    " THE ESTIMATED LOSS OF BASE%4d SIGNIFICANT DIGITS IS%7.2Lf\n\n\n",
	    ibeta, w);
	w = -999.0e+00L;
	if (r7 != ZERO)
	    w = ALOG(ABS(r7)) / albeta;
	(void)printf(" THE ROOT MEAN SQUARE RELATIVE ERROR WAS%15.4Le = %4d **%7.2Lf\n",
	    r7, ibeta, w);
	w = AMAX1(ait + w, ZERO);
	(void)printf(
	    " THE ESTIMATED LOSS OF BASE%4d SIGNIFICANT DIGITS IS%7.2Lf\n\n\n",
	    ibeta, w);
	if (j == 2)
	{
	    a = 0.75e+00L;
	    b = 1.0e+00L;
	}
	if (j == 4)
	{
	    b = -a;
	    a = -1.0e+00L;
	    c1 = c1 + c1;
	    c2 = c2 + c2;
	    l = -l;
	}
    }

    /* special tests */

    (void)printf("1SPECIAL TESTS\n\n");
    (void)printf(" THE IDENTITY  ASIN(-X) = -ASIN(X)  WILL BE TESTED.\n\n");
    (void)printf("        X         F(X) + F(-X)\n");

    for (i = 1; i <= 5; ++i)
    {
	x = RAN() * a;
	z = ASIN(x) + ASIN(-x);
	(void)printf("%15.7Le%15.7Le\n\n", x, z);
    }

    (void)printf(" THE IDENTITY ASIN(X) = X , X SMALL, WILL BE TESTED.\n\n");
    (void)printf("        X         X - F(X)\n");
    betap = IPOW(beta, it);
    x = RAN() / betap;

    for (i = 1; i <= 5; ++i)
    {
	z = x - ASIN(x);
	(void)printf("%15.7Le%15.7Le\n\n", x, z);
	x = x / beta;
    }

    (void)printf(" TEST OF UNDERFLOW FOR VERY SMALL ARGUMENT.\n\n");
    x = POW(beta, TO_FP_T(minexp) * 0.75e+00L);
    y = ASIN(x);
    (void)printf("       ASIN(%13.6Le) =%13.6Le\n\n", x, y);

    /* test of error returns */

    (void)printf("1TEST OF ERROR RETURNS\n\n\n");

    x = 1.2e+00L;
    (void)printf(" ASIN WILL BE CALLED WITH THE ARGUMENT%15.4Le\n",x);
    (void)printf(" THIS SHOULD TRIGGER AN ERROR MESSAGE\n\n\n");
    fflush(stdout);
    errno = 0;
    y = ASIN(x);
    if (errno)
	perror("asin()");
    (void)printf(" ASIN RETURNED THE VALUE%15.4Le\n\n\n", y);

    (void)printf(" THIS CONCLUDES THE TESTS\n");
}
