--
--	Inmos Libraries
--	Copyright (C) 1988, 1989, 1990, 1991, 1993  Inmos Limited
--
--	This library is free software; you can redistribute it and/or
--	modify it under the terms of the GNU Lesser General Public
--	License as published by the Free Software Foundation; either
--	version 2 of the License, or (at your option) any later version.
--
--	This library is distributed in the hope that it will be useful,
--	but WITHOUT ANY WARRANTY; without even the implied warranty of
--	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
--	Lesser General Public License for more details.
--
--	You should have received a copy of the GNU Lesser General Public
--	License along with this library; if not, write to the Free Software
--	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
--

--** @module maths

#OPTION "V"  -- no vector space
#COMMENT "Floating point maths library, V1.1 27/03/90"
#COMMENT "(c) Copyright INMOS Limited 1988"
#INCLUDE "mathhdr.inc"

--* Double-precision exponent.
--
-- @param X Input value
-- @return e raised to the power X
REAL64 FUNCTION DEXP (VAL REAL64 X)

  --{{{  history
  -- V1.0 1-Jul-88
  -- Added OPTION "V"; V1.1 27-Mar-90 SRH
  --}}}

  REAL64 RWork :
  VALOF
    IF
      DNOTFINITE (X)
        --{{{  deal with infs and NaNs
        INT order :
        SEQ
          order := DIEEECOMPARE (X, DZero)
          CASE order
            less.than
              RWork := DZero -- underflow
            greater.than
              RWork := DRInf -- overflow
            is.nan
              RWork := X     -- transmit NaN
        --}}}
      --{{{  definitions and variables
      -- XMax = 709.7827129999999442588887177407741546630859375
      VAL REAL64 XMax RETYPES #40862E42FF08893B(INT64) :
      VAL XMin IS 745.5(REAL64) : --   -ln(smallest real)
      VAL REAL64 DLn2     RETYPES #3FE62E42FEFA39EF(INT64) :
      VAL REAL64 DLn2.err RETYPES #3C7ABC9E3B39803F(INT64) :
      --}}}
      IF
        X > XMax
          RWork := DRInf
        DABS(X) > XMin -- if true, X must be negative
          --{{{  alternative responses
          -- RWork := MinDenorm    -- alternative response
          -- RWork := Dinexact.NaN -- alternative response
          --}}}
          RWork := DZero -- underflow
        TRUE  -- general case
          --{{{  rational approximation
          REAL64 XWork :
          INT32 N :
          SEQ
            BOOL ok :  -- it will be, because the range of X has been limited.
            ok, N, XWork := DARGUMENT.REDUCE (X, DLn2, DLn2.err)
            --{{{  coefficients
            VAL P2 IS 3.1555192765684646356E-5(REAL64) :
            VAL P1 IS 7.5753180159422776666E-3(REAL64) :
            VAL P0 IS 0.25(REAL64) :
            VAL Q3 IS 7.5104028399870046114E-7(REAL64) :
            VAL Q2 IS 6.3121894374398503557E-4(REAL64) :
            VAL Q1 IS 5.6817302698551221787E-2(REAL64) :
            VAL Q0 IS 0.5(REAL64) :
            --}}}
            VAL Z IS XWork * XWork :
            VAL GPZ IS ((((P2 * Z) + P1) * Z) + P0) * XWork :
            RWork := DSCALEB (Q0 +
                             (GPZ /(((((((Q3 * Z) + Q2) * Z) + Q1) * Z) + Q0) - GPZ)),
                              (INT N) + 1) -- N guarenteed small
          --}}}
    RESULT RWork
:
