/**************************************************************************/
/*                                                                        */
/*                                 Sources                                */
/*                                from  the                               */
/*                           XSC SOFTWARE PACKAGE                         */
/*                                                                        */
/*                Copyright (c) 1997 University of Karlsruhe              */
/*                     Institute for Appl. Mathematics                    */
/*                                                                        */
/* This program is free software for non-commercial use. For details, see */
/* the file LICENSE.XSC, distributed with this software package.          */
/*                                                                        */
/* This program is distributed WITHOUT ANY WARRANTY. For details, see the */
/* file LICENSE.XSC, distributed with this software package.              */
/*                                                                        */
/**************************************************************************/

/* RCS $Id: i_tan.c,v 1.2 1997/09/25 17:31:31 ae59 Exp $ */
/*
$Log: i_tan.c,v $
 * Revision 1.2  1997/09/25  17:31:31  ae59
 * Copyright-message
 *
 * Revision 1.1  1994/01/27  13:23:06  ae59
 * Put under RCS control
 *
 */
/****************************************************************/
/*                                                              */
/*      Filename        : i_tan.c                               */
/*                                                              */
/*      Entries         : a_intv i_tan(a)                       */
/*                        a_intv a;                             */
/*                                                              */
/*      Arguments       : a = interval argument of function     */
/*                                                              */
/*      Description     : Interval tan function                 */
/*                                                              */
/*      Date            : 1993-08-23                            */
/*      1993-08-23 : cpi as a_btyp, use B_TYpINI         =b=    */
/*      1992-07-22 : cast to unsigned in jub, jlb        =b=    */
/*      1992-03-05 : chg unsigned to a_btyp in LhI decl. =b=    */
/*      1991-11-14 : save and restore flags                     */
/*      1991-02-21 : extern L...() declaration removed          */
/*      1990-10-30 : long constants                             */
/*      1990-10-18 : real operations                            */
/****************************************************************/

#ifndef ALL_IN_ONE
#ifdef AIX
#include "/u/p88c/runtime/o_defs.h"
#else
#include "o_defs.h"
#endif
#define local
#endif

extern a_btyp LhI;    /* entier(x/(pi/4)) mod 16 after call tan(x) */

#ifdef LINT_ARGS
local a_intv i_tan( a_intv a )
#else
local a_intv i_tan(a)
a_intv a;
#endif

{
        /* --- Pi.l --- */
        static a_btyp cpi[] = B_TYPINI( 0x400921fbL, 0x54442d18L);
     /* static unsigned long cpi[2] = {0x182d4454L, 0xfb210940L}; */
        a_real            *pil= (a_real*)&cpi[0];

        a_btyp        rc;
        a_intv          res;
        a_real            dummy;
        unsigned        jub, jlb, d;

        E_SPUSH("i_tan")

        /* --- point argument --- */
        if (i_point(a)) {
            /* printf("point argument!\n"); */
            rc = i_invp(Ltan, a.INF, &res.INF, &res.SUP);
            /* printf("entier part Lhi: %d\n", LhI); */
        }

        /* --- interval argument --- */
        else if (i_iv(a)) {
            /* printf("interval argument!\n");   */

            if (r_lt(r_addd(a.INF,*pil),a.SUP))
                 rc = 1; /* error singularity */
            else {

            rc =  i_invp(Ltan, a.INF, &res.INF, &dummy);
            jlb = (unsigned) LhI;        /* entier(lb/(pi/4)) mod 4 */

            /* printf("entier part Lhi lb mod 16: %u\n", LhI); */

            rc += i_invp(Ltan, a.SUP, &dummy, &res.SUP);
            jub = (unsigned) LhI;        /* entier(lb/(pi/4)) mod 4 */

            /* printf("entier part Lhi ub mod 16: %u\n", LhI); */


            /* --- d --- */
            if(jub<jlb)
                d = (jub+16)-jlb;       /* d in {0,..,15} */
            else
                d = jub - jlb;          /* d in {0,..,15} */
            if (d>=4) rc = 1;           /* error singularity */
            jlb%=4;
            jub%=4;

            if(jub<jlb)
                d = (jub+4)-jlb;        /* d in {0,..,3} */
            else
                d = jub - jlb;          /* d in {0,..,3} */

            if (jlb <= 1 && jub >= 2)   /* singularity */
                rc = 1;

            /* printf("mod 4: d = jub - jlb: %u\n", d);
            printf("jlb: %u jub: %u\n", jlb, jub); */
        }} /* interval argument */
        else
            /* --- no correct interval --- */
            rc = 1;

        /* --- error --- */
        if (rc)
            e_trap(INV_ARG,4,E_TDBL+E_TEXT(5),&a.INF,
                             E_TDBL+E_TEXT(6),&a.SUP);

        E_SPOPP("i_tan")
        return(res);
}
