--
--	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"

--* Single-precision arccosine.
--
-- @param X Input value in the range [@text [-1.0, 1.0]]
-- @return Arccosine of X in radians
REAL32 FUNCTION ACOS (VAL REAL32 X)

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

  REAL32 Result :
  VALOF
    IF
      NOTFINITE (X)
        --{{{  NaNs and Infs
        IF
          ISNAN (X)
            Result := X
          TRUE
            Result := undefined.NaN
        --}}}
      --{{{  definitions and variables
      VAL P2 IS -5.04400557E-1(REAL32) :
      VAL P1 IS  9.33935835E-1(REAL32) :
      VAL Q1 IS -5.54846723(REAL32) :
      VAL Q0 IS  5.60363004(REAL32) :
      VAL XWork IS ABS (X) :
      --}}}
      IF
        XWork > One
          Result := undefined.NaN
        XWork = One
          --{{{  result depends on sign
          IF
            X < Zero
              Result := Pi
            TRUE
              Result := Zero
          --}}}
        XWork > Half
          --{{{  reduce argument & compute result
          REAL32 Y, G, RWork :
          SEQ
            G := DIVBY2 (One - XWork)
            Y := MULBY2 (SQRT (G))
            RWork := Y + (Y * ((((P2 * G) + P1) * G)/(((G + Q1) * G) + Q0)))
            IF
              X < Zero
                Result := Pi - RWork
              TRUE
                Result := RWork
          --}}}
        TRUE
          --{{{  remaining case
          VAL G IS X * X :
          Result := PiBy2 - (X + (X * ((((P2 * G) + P1) * G)/(((G + Q1) * G) + Q0))))
          --}}}
    RESULT Result
:
