/**************************************************************************/
/*                                                                        */
/*                                 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: t_icsh.c,v 1.2 1997/09/25 18:16:36 ae59 Exp $ */
/*
$Log: t_icsh.c,v $
 * Revision 1.2  1997/09/25  18:16:36  ae59
 * Copyright-message
 *
 * Revision 1.1  1994/01/27  13:25:30  ae59
 * Put under RCS control
 *
 */
/****************************************************************/
/*                                                              */
/*      Date            : 1991-11-14                            */
/*      1991-11-14 : save and restore flags                     */
/*      1991-09-24 : macro call StdFctInterval replaced         */
/*      1991-09-17 : remove semicolon after makro               */
/****************************************************************/

#ifdef AIX
#include "/u/p88c/runtime/tbyte/t_defs.h"
#else
#include "t_defs.h"
#endif

/* StdFctInterval(t_icsh,icoshee) */
#ifdef LINT_ARGS
a_intv t_icsh(a_intv ai)
#else
a_intv t_icsh(ai)

a_intv ai;
#endif
        {

        a_intv   res;
        int      rnd, rc;
        IExtReal  a, r;

        E_SPUSH("t_icsh")

        rnd = getrndmode();
        longreal_to_extreal((LongReal *)&ai.INF, &a.l);
        longreal_to_extreal((LongReal *)&ai.SUP, &a.u);

        if ((rc = icoshee(&a, &r))!=0)
            ieee_aborti1(rc, &ai);

        setrndmode(DOWN);
        if ((rc = extreal_to_longreal(&r.l, (LongReal *)&res.INF))!=0)
             ieee_aborti1(rc, &ai);
        setrndmode(UP);
        if ((rc = extreal_to_longreal(&r.u, (LongReal *)&res.SUP))!=0)
            ieee_aborti1(rc, &ai);
        setrndmode(rnd);

        E_SPOPP("t_icsh")
        return res;
        }

/* ------------------------------------------------------------ */

/*--------------------------------------------------------------*
 | coshee                                                       |
 *--------------------------------------------------------------*/
#ifdef ANSI_C
#ifdef LINT_ARGS
int icoshee (const IExtReal *arg, IExtReal *res)
#else
int icoshee (arg, res)
const IExtReal   *arg;
      IExtReal   *res;
#endif /* LINT_ARGS */
#else  /* NOT ANSI_C */
#ifdef LINT_ARGS
int icoshee (IExtReal *arg, IExtReal *res)
#else
int icoshee (arg, res)
IExtReal   *arg;
IExtReal   *res;
#endif /* LINT_ARGS */
#endif /* ANSI_C */
{
   IExtReal    r;          /* Ergebnis vor Rundung              */
   int         retu;       /* Rueckgabe                         */
   int         retl;       /* Rueckgabe                         */
   int         check;      /* Rueckgabe von Makro ArgCheck      */

   /* --- pruefe Argument, dann Pruefung aus --- */
   ArgCheckI1(ICosh, arg, res);
   arg_check = Off;

   /* --- cosh --- */
   if(-1==cmpee(&(arg->u), &Zero)) { /* --- u<0 --- */
      retu = coshee(&(arg->l), &r.u);
      retl = coshee(&(arg->u), &r.l);
   } else
   if(1==cmpee(&(arg->l), &Zero)) { /* --- l>0 --- */
      retu = coshee(&(arg->u), &r.u);
      retl = coshee(&(arg->l), &r.l);
   } else {
      copyee(&One, &(res->l));             /* --- 0 in argument --- */
      retl = LB_Exact;
      if(!cmpee(&arg->u,&arg->l))          /* --- u = l = 0 --- */
      {
         copyee(&One, &(res->u));
         retu = UB_Exact;
      }
      else
      {
         retu = (-1!=cmpabsee(&(arg->u), &(arg->l)) ?
            coshee(&(arg->u), &r.u) :      /* --- |u|>=|l| --- */
            coshee(&(arg->l), &r.u));      /* --- |u|< |l| --- */
      }
   }

   /* --- Abbruch bei Fehler --- */
   if((retu&Except)!=NoErr || (retl&Except)!=NoErr) {
      icopyee(&r, res);
      arg_check = On;
      return max((retu&Except), (retl&Except));
   }

   /* --- Rundungs-Fehler --- */
   if(!(retu&UB_Exact))
      round_rel(UP, &r.u, &EpsCosh, &(res->u));

   if(!(retl&LB_Exact))
   {
      if(cmpee(&r.l,&One))
         round_rel(DOWN, &r.l, &EpsCosh, &(res->l));
      else
         copyee(&One, &(res->l));
   }

   /* --- ArgPruefung wieder an --- */
   arg_check = On;

   return NoErr;
} /* icoshee() */
