home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / adav313.zip / gnat-3_13p-os2-bin-20010916.zip / emx / gnatlib / a-ngelfu.adb < prev    next >
Text File  |  2000-07-19  |  28KB  |  1,050 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                         GNAT RUNTIME COMPONENTS                          --
  4. --                                                                          --
  5. --                ADA.NUMERICS.GENERIC_ELEMENTARY_FUNCTIONS                 --
  6. --                                                                          --
  7. --                                 B o d y                                  --
  8. --                                                                          --
  9. --                            $Revision: 1.40-3.13a $
  10. --                                                                          --
  11. --          Copyright (C) 1992-1999, Free Software Foundation, Inc.         --
  12. --                                                                          --
  13. -- GNAT is free software;  you can  redistribute it  and/or modify it under --
  14. -- terms of the  GNU General Public License as published  by the Free Soft- --
  15. -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
  16. -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
  17. -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
  18. -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
  19. -- for  more details.  You should have  received  a copy of the GNU General --
  20. -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
  21. -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
  22. -- MA 02111-1307, USA.                                                      --
  23. --                                                                          --
  24. -- As a special exception,  if other files  instantiate  generics from this --
  25. -- unit, or you link  this unit with other files  to produce an executable, --
  26. -- this  unit  does not  by itself cause  the resulting  executable  to  be --
  27. -- covered  by the  GNU  General  Public  License.  This exception does not --
  28. -- however invalidate  any other reasons why  the executable file  might be --
  29. -- covered by the  GNU Public License.                                      --
  30. --                                                                          --
  31. -- GNAT was originally developed  by the GNAT team at  New York University. --
  32. -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
  33. --                                                                          --
  34. ------------------------------------------------------------------------------
  35.  
  36. --  This body is specifically for using an Ada interface to C math.h to get
  37. --  the computation engine. Many special cases are handled locally to avoid
  38. --  unnecessary calls. This is not a "strict" implementation, but takes full
  39. --  advantage of the C functions, e.g. in providing interface to hardware
  40. --  provided versions of the elementary functions.
  41.  
  42. --  A known weakness is that on the x86, all computation is done in Double,
  43. --  which means that a lot of accuracy is lost for the Long_Long_Float case.
  44.  
  45. --  Uses functions sqrt, exp, log, pow, sin, asin, cos, acos, tan, atan,
  46. --  sinh, cosh, tanh from C library via math.h
  47.  
  48. with Ada.Numerics.Aux;
  49.  
  50. package body Ada.Numerics.Generic_Elementary_Functions is
  51.  
  52.    use type Ada.Numerics.Aux.Double;
  53.  
  54.    Sqrt_Two : constant := 1.41421_35623_73095_04880_16887_24209_69807_85696;
  55.    Log_Two  : constant := 0.69314_71805_59945_30941_72321_21458_17656_80755;
  56.    Half_Log_Two : constant := Log_Two / 2;
  57.  
  58.  
  59.    subtype T is Float_Type'Base;
  60.    subtype Double is Aux.Double;
  61.  
  62.  
  63.    Two_Pi     : constant T := 2.0 * Pi;
  64.    Half_Pi    : constant T := Pi / 2.0;
  65.    Fourth_Pi  : constant T := Pi / 4.0;
  66.  
  67.    Epsilon             : constant T := 2.0 ** (1 - T'Model_Mantissa);
  68.    IEpsilon            : constant T := 2.0 ** (T'Model_Mantissa - 1);
  69.    Log_Epsilon         : constant T := T (1 - T'Model_Mantissa) * Log_Two;
  70.    Half_Log_Epsilon    : constant T := T (1 - T'Model_Mantissa) * Half_Log_Two;
  71.    Log_Inverse_Epsilon : constant T := T (T'Model_Mantissa - 1) * Log_Two;
  72.    Sqrt_Epsilon        : constant T := Sqrt_Two ** (1 - T'Model_Mantissa);
  73.  
  74.  
  75.    DEpsilon    : constant Double := Double (Epsilon);
  76.    DIEpsilon   : constant Double := Double (IEpsilon);
  77.  
  78.    -----------------------
  79.    -- Local Subprograms --
  80.    -----------------------
  81.  
  82.    function Exp_Strict (X : Float_Type'Base) return Float_Type'Base;
  83.    --  Cody/Waite routine, supposedly more precise than the library
  84.    --  version. Currently only needed for Sinh/Cosh on X86 with the largest
  85.    --  FP type.
  86.  
  87.    function Local_Atan
  88.      (Y    : Float_Type'Base;
  89.       X    : Float_Type'Base := 1.0)
  90.       return Float_Type'Base;
  91.    --  Common code for arc tangent after cyele reduction
  92.  
  93.    ----------
  94.    -- "**" --
  95.    ----------
  96.  
  97.    function "**" (Left, Right : Float_Type'Base) return Float_Type'Base is
  98.       A_Right  : Float_Type'Base;
  99.       Int_Part : Integer;
  100.       Result   : Float_Type'Base;
  101.       R1       : Float_Type'Base;
  102.       Rest     : Float_Type'Base;
  103.  
  104.    begin
  105.       if Left = 0.0
  106.         and then Right = 0.0
  107.       then
  108.          raise Argument_Error;
  109.  
  110.       elsif Left < 0.0 then
  111.          raise Argument_Error;
  112.  
  113.       elsif Right = 0.0 then
  114.          return 1.0;
  115.  
  116.       elsif Left = 0.0 then
  117.          if Right < 0.0 then
  118.             raise Constraint_Error;
  119.          else
  120.             return 0.0;
  121.          end if;
  122.  
  123.       elsif Left = 1.0 then
  124.          return 1.0;
  125.  
  126.       elsif Right = 1.0 then
  127.          return Left;
  128.  
  129.       else
  130.          begin
  131.             if Right = 2.0 then
  132.                return Left * Left;
  133.  
  134.             elsif Right = 0.5 then
  135.                return Sqrt (Left);
  136.  
  137.             else
  138.                A_Right := abs (Right);
  139.  
  140.                --  If exponent is larger than one, compute integer exponen-
  141.                --  tiation if possible, and evaluate fractional part with
  142.                --  more precision. The relative error is now proportional
  143.                --  to the fractional part of the exponent only.
  144.  
  145.                if A_Right > 1.0
  146.                  and then A_Right < Float_Type (Integer'Last)
  147.                then
  148.                   Int_Part := Integer (Float_Type'Truncation (A_Right));
  149.                   Result := Left ** Int_Part;
  150.                   Rest :=  A_Right - Float_Type'Base (Int_Part);
  151.  
  152.                   --  Compute with two leading bits of the mantissa using
  153.                   --  square roots. Bound  to be better than logarithms, and
  154.                   --  easily extended to greater precision.
  155.  
  156.                   if Rest >= 0.5 then
  157.                      R1 := Sqrt (Left);
  158.                      Result := Result * R1;
  159.                      Rest := Rest - 0.5;
  160.  
  161.                      if Rest >= 0.25 then
  162.                         Result := Result * Sqrt (R1);
  163.                         Rest := Rest - 0.25;
  164.                      end if;
  165.  
  166.                   elsif Rest >= 0.25 then
  167.                      Result := Result * Sqrt (Sqrt (Left));
  168.                      Rest := Rest - 0.25;
  169.                   end if;
  170.  
  171.                   Result :=  Result *
  172.                     Float_Type'Base (Aux.Pow (Double (Left), Double (Rest)));
  173.  
  174.                   if Right >= 0.0 then
  175.                      return Result;
  176.                   else
  177.                      return (1.0 / Result);
  178.                   end if;
  179.                else
  180.                   return
  181.                     Float_Type'Base (Aux.Pow (Double (Left), Double (Right)));
  182.                end if;
  183.             end if;
  184.  
  185.          exception
  186.             when others =>
  187.                raise Constraint_Error;
  188.          end;
  189.       end if;
  190.    end "**";
  191.  
  192.    ------------
  193.    -- Arccos --
  194.    ------------
  195.  
  196.    --  Natural cycle
  197.  
  198.    function Arccos (X : Float_Type'Base) return Float_Type'Base is
  199.       Temp : Float_Type'Base;
  200.  
  201.    begin
  202.       if abs X > 1.0 then
  203.          raise Argument_Error;
  204.  
  205.       elsif abs X < Sqrt_Epsilon then
  206.          return Pi / 2.0 - X;
  207.  
  208.       elsif X = 1.0 then
  209.          return 0.0;
  210.  
  211.       elsif X = -1.0 then
  212.          return Pi;
  213.       end if;
  214.  
  215.       Temp := Float_Type'Base (Aux.Acos (Double (X)));
  216.  
  217.       if Temp < 0.0 then
  218.          Temp := Pi + Temp;
  219.       end if;
  220.  
  221.       return Temp;
  222.    end Arccos;
  223.  
  224.    --  Arbitrary cycle
  225.  
  226.    function Arccos (X, Cycle : Float_Type'Base) return Float_Type'Base is
  227.       Temp : Float_Type'Base;
  228.  
  229.    begin
  230.       if Cycle <= 0.0 then
  231.          raise Argument_Error;
  232.  
  233.       elsif abs X > 1.0 then
  234.          raise Argument_Error;
  235.  
  236.       elsif abs X < Sqrt_Epsilon then
  237.          return Cycle / 4.0;
  238.  
  239.       elsif X = 1.0 then
  240.          return 0.0;
  241.  
  242.       elsif X = -1.0 then
  243.          return Cycle / 2.0;
  244.       end if;
  245.  
  246.       Temp := Arctan (Sqrt ((1.0 - X) * (1.0 + X)) / X, 1.0, Cycle);
  247.  
  248.       if Temp < 0.0 then
  249.          Temp := Cycle / 2.0 + Temp;
  250.       end if;
  251.  
  252.       return Temp;
  253.    end Arccos;
  254.  
  255.    -------------
  256.    -- Arccosh --
  257.    -------------
  258.  
  259.    function Arccosh (X : Float_Type'Base) return Float_Type'Base is
  260.    begin
  261.  
  262.       --  Return positive branch of Log (X - Sqrt (X * X - 1.0)), or
  263.       --  the proper approximation for X close to 1 or >> 1.
  264.  
  265.       if X < 1.0 then
  266.          raise Argument_Error;
  267.  
  268.       elsif X < 1.0 + Sqrt_Epsilon then
  269.          return Sqrt (2.0 * (X - 1.0));
  270.  
  271.       elsif  X > 1.0 / Sqrt_Epsilon then
  272.          return Log (X) + Log_Two;
  273.  
  274.       else
  275.          return Log (X + Sqrt ((X - 1.0) * (X + 1.0)));
  276.       end if;
  277.    end Arccosh;
  278.  
  279.    ------------
  280.    -- Arccot --
  281.    ------------
  282.  
  283.    --  Natural cycle
  284.  
  285.    function Arccot
  286.      (X    : Float_Type'Base;
  287.       Y    : Float_Type'Base := 1.0)
  288.       return Float_Type'Base
  289.    is
  290.    begin
  291.       --  Just reverse arguments
  292.  
  293.       return Arctan (Y, X);
  294.    end Arccot;
  295.  
  296.    --  Arbitrary cycle
  297.  
  298.    function Arccot
  299.      (X     : Float_Type'Base;
  300.       Y     : Float_Type'Base := 1.0;
  301.       Cycle : Float_Type'Base)
  302.       return  Float_Type'Base
  303.    is
  304.    begin
  305.       --  Just reverse arguments
  306.  
  307.       return Arctan (Y, X, Cycle);
  308.    end Arccot;
  309.  
  310.    -------------
  311.    -- Arccoth --
  312.    -------------
  313.  
  314.    function Arccoth (X : Float_Type'Base) return Float_Type'Base is
  315.    begin
  316.  
  317.       if abs X > 2.0 then
  318.          return Arctanh (1.0 / X);
  319.  
  320.       elsif abs X = 1.0 then
  321.          raise Constraint_Error;
  322.  
  323.       elsif abs X < 1.0 then
  324.          raise Argument_Error;
  325.  
  326.       else
  327.  
  328.          --  1.0 < abs X <= 2.0.  One of X + 1.0 and X - 1.0 is exact, the
  329.          --  other has error 0 or Epsilon.
  330.  
  331.          return 0.5 * (Log (abs (X + 1.0)) - Log (abs (X - 1.0)));
  332.       end if;
  333.    end Arccoth;
  334.  
  335.    ------------
  336.    -- Arcsin --
  337.    ------------
  338.  
  339.    --  Natural cycle
  340.  
  341.    function Arcsin (X : Float_Type'Base) return Float_Type'Base is
  342.    begin
  343.       if abs X > 1.0 then
  344.          raise Argument_Error;
  345.  
  346.       elsif abs X < Sqrt_Epsilon then
  347.          return X;
  348.  
  349.       elsif X = 1.0 then
  350.          return Pi / 2.0;
  351.  
  352.       elsif X = -1.0 then
  353.          return -Pi / 2.0;
  354.       end if;
  355.  
  356.       return Float_Type'Base (Aux.Asin (Double (X)));
  357.    end Arcsin;
  358.  
  359.    --  Arbitrary cycle
  360.  
  361.    function Arcsin (X, Cycle : Float_Type'Base) return Float_Type'Base is
  362.    begin
  363.       if Cycle <= 0.0 then
  364.          raise Argument_Error;
  365.  
  366.       elsif abs X > 1.0 then
  367.          raise Argument_Error;
  368.  
  369.       elsif X = 0.0 then
  370.          return X;
  371.  
  372.       elsif X = 1.0 then
  373.          return Cycle / 4.0;
  374.  
  375.       elsif X = -1.0 then
  376.          return -Cycle / 4.0;
  377.       end if;
  378.  
  379.       return Arctan (X / Sqrt ((1.0 - X) * (1.0 + X)), 1.0, Cycle);
  380.    end Arcsin;
  381.  
  382.    -------------
  383.    -- Arcsinh --
  384.    -------------
  385.  
  386.    function Arcsinh (X : Float_Type'Base) return Float_Type'Base is
  387.    begin
  388.       if abs X < Sqrt_Epsilon then
  389.          return X;
  390.  
  391.       elsif X > 1.0 / Sqrt_Epsilon then
  392.          return Log (X) + Log_Two;
  393.  
  394.       elsif X < -1.0 / Sqrt_Epsilon then
  395.          return -(Log (-X) + Log_Two);
  396.  
  397.       elsif X < 0.0 then
  398.          return -Log (abs X + Sqrt (X * X + 1.0));
  399.  
  400.       else
  401.          return Log (X + Sqrt (X * X + 1.0));
  402.       end if;
  403.    end Arcsinh;
  404.  
  405.    ------------
  406.    -- Arctan --
  407.    ------------
  408.  
  409.    --  Natural cycle
  410.  
  411.    function Arctan
  412.      (Y    : Float_Type'Base;
  413.       X    : Float_Type'Base := 1.0)
  414.       return Float_Type'Base
  415.    is
  416.    begin
  417.       if X = 0.0
  418.         and then Y = 0.0
  419.       then
  420.          raise Argument_Error;
  421.  
  422.       elsif Y = 0.0 then
  423.          if X > 0.0 then
  424.             return 0.0;
  425.          else -- X < 0.0
  426.             return Pi * Float_Type'Copy_Sign (1.0, Y);
  427.          end if;
  428.  
  429.       elsif X = 0.0 then
  430.          if Y > 0.0 then
  431.             return Half_Pi;
  432.          else -- Y < 0.0
  433.             return -Half_Pi;
  434.          end if;
  435.  
  436.       else
  437.          return Local_Atan (Y, X);
  438.       end if;
  439.    end Arctan;
  440.  
  441.    --  Arbitrary cycle
  442.  
  443.    function Arctan
  444.      (Y     : Float_Type'Base;
  445.       X     : Float_Type'Base := 1.0;
  446.       Cycle : Float_Type'Base)
  447.       return  Float_Type'Base
  448.    is
  449.    begin
  450.       if Cycle <= 0.0 then
  451.          raise Argument_Error;
  452.  
  453.       elsif X = 0.0
  454.         and then Y = 0.0
  455.       then
  456.          raise Argument_Error;
  457.  
  458.       elsif Y = 0.0 then
  459.          if X > 0.0 then
  460.             return 0.0;
  461.          else -- X < 0.0
  462.             return Cycle / 2.0 * Float_Type'Copy_Sign (1.0, Y);
  463.          end if;
  464.  
  465.       elsif X = 0.0 then
  466.          if Y > 0.0 then
  467.             return Cycle / 4.0;
  468.          else -- Y < 0.0
  469.             return -Cycle / 4.0;
  470.          end if;
  471.  
  472.       else
  473.          return Local_Atan (Y, X) *  Cycle / Two_Pi;
  474.       end if;
  475.    end Arctan;
  476.  
  477.    -------------
  478.    -- Arctanh --
  479.    -------------
  480.  
  481.    function Arctanh (X : Float_Type'Base) return Float_Type'Base is
  482.       A, B, D, A_Plus_1, A_From_1 : Float_Type'Base;
  483.       Mantissa : constant Integer := Float_Type'Machine_Mantissa;
  484.    begin
  485.  
  486.       --  The naive formula:
  487.       --   Arctanh (X) := (1/2) * Log  (1 + X) / (1 - X)
  488.       --   is not well-behaved numerically when X < 0.5 and when X is close
  489.       --   to one. The following is accurate but probably not optimal.
  490.  
  491.       if abs X = 1.0 then
  492.          raise Constraint_Error;
  493.  
  494.       elsif abs X >= 1.0 - 2.0 ** (-Mantissa) then
  495.  
  496.          if abs X >= 1.0 then
  497.             raise Argument_Error;
  498.          else
  499.  
  500.             --  The one case that overflows if put through the method below:
  501.             --  abs X = 1.0 - Epsilon.  In this case (1/2) log (2/Epsilon) is
  502.             --  accurate. This simplifies to:
  503.  
  504.             return Float_Type'Base'Copy_Sign (
  505.                Half_Log_Two * Float_Type'Base (Mantissa + 1), X);
  506.          end if;
  507.  
  508.       --  elsif abs X <= 0.5 then
  509.       else
  510.  
  511.          --  Use several piecewise linear approximations.
  512.          --  A is close to X, chosen so 1.0 + A, 1.0 - A, and X - A are exact.
  513.          --  The two scalings remove the low-order bits of X.
  514.  
  515.          A := Float_Type'Base'Scaling (
  516.              Float_Type'Base (Long_Long_Integer
  517.                (Float_Type'Base'Scaling (X, Mantissa - 1))), 1 - Mantissa);
  518.  
  519.          B := X - A;                --  This is exact; abs B <= 2**(-Mantissa).
  520.          A_Plus_1 := 1.0 + A;       --  This is exact.
  521.          A_From_1 := 1.0 - A;       --  Ditto.
  522.          D := A_Plus_1 * A_From_1;  --  1 - A*A.
  523.  
  524.          --  use one term of the series expansion:
  525.          --  f (x + e) = f(x) + e * f'(x) + ..
  526.  
  527.          --  The derivative of Arctanh at A is 1/(1-A*A). Next term is
  528.          --  A*(B/D)**2 (if a quadratic approximation is ever needed).
  529.  
  530.          return 0.5 * (Log (A_Plus_1) - Log (A_From_1)) + B / D;
  531.  
  532.          --  else
  533.          --  return 0.5 * Log ((X + 1.0) / (1.0 - X));
  534.       end if;
  535.    end Arctanh;
  536.  
  537.    ---------
  538.    -- Cos --
  539.    ---------
  540.  
  541.    --  Natural cycle
  542.  
  543.    function Cos (X : Float_Type'Base) return Float_Type'Base is
  544.    begin
  545.       if X = 0.0 then
  546.          return 1.0;
  547.  
  548.       elsif abs X < Sqrt_Epsilon then
  549.          return 1.0;
  550.  
  551.       end if;
  552.  
  553.       return Float_Type'Base (Aux.Cos (Double (X)));
  554.    end Cos;
  555.  
  556.    --  Arbitrary cycle
  557.  
  558.    function Cos (X, Cycle : Float_Type'Base) return Float_Type'Base is
  559.    begin
  560.       --  Just reuse the code for Sin. The potential small
  561.       --  loss of speed is negligible with proper (front-end) inlining.
  562.  
  563.       --  ??? Add pragma Inline_Always in spec when this is supported
  564.       return -Sin (abs X - Cycle * 0.25, Cycle);
  565.    end Cos;
  566.  
  567.    ----------
  568.    -- Cosh --
  569.    ----------
  570.  
  571.    function Cosh (X : Float_Type'Base) return Float_Type'Base is
  572.       Lnv      : constant Float_Type'Base := 8#0.542714#;
  573.       V2minus1 : constant Float_Type'Base := 0.13830_27787_96019_02638E-4;
  574.       Y        : Float_Type'Base := abs X;
  575.       Z        : Float_Type'Base;
  576.  
  577.    begin
  578.       if Y < Sqrt_Epsilon then
  579.          return 1.0;
  580.  
  581.       elsif  Y > Log_Inverse_Epsilon then
  582.          Z := Exp_Strict (Y - Lnv);
  583.          return (Z + V2minus1 * Z);
  584.  
  585.       else
  586.          Z := Exp_Strict (Y);
  587.          return 0.5 * (Z + 1.0 / Z);
  588.       end if;
  589.  
  590.    end Cosh;
  591.  
  592.    ---------
  593.    -- Cot --
  594.    ---------
  595.  
  596.    --  Natural cycle
  597.  
  598.    function Cot (X : Float_Type'Base) return Float_Type'Base is
  599.    begin
  600.       if X = 0.0 then
  601.          raise Constraint_Error;
  602.  
  603.       elsif abs X < Sqrt_Epsilon then
  604.          return 1.0 / X;
  605.       end if;
  606.  
  607.       return 1.0 / Float_Type'Base (Aux.Tan (Double (X)));
  608.    end Cot;
  609.  
  610.    --  Arbitrary cycle
  611.  
  612.    function Cot (X, Cycle : Float_Type'Base) return Float_Type'Base is
  613.       T : Float_Type'Base;
  614.    begin
  615.       if Cycle <= 0.0 then
  616.          raise Argument_Error;
  617.       end if;
  618.  
  619.       T := Float_Type'Base'Remainder (X, Cycle);
  620.  
  621.       if T = 0.0 or abs T = 0.5 * Cycle then
  622.          raise Constraint_Error;
  623.  
  624.       elsif abs T < Sqrt_Epsilon then
  625.          return 1.0 / T;
  626.  
  627.       elsif abs T = 0.25 * Cycle then
  628.          return 0.0;
  629.  
  630.       else
  631.          T := T / Cycle * Two_Pi;
  632.          return  Cos (T) / Sin (T);
  633.       end if;
  634.    end Cot;
  635.  
  636.    ----------
  637.    -- Coth --
  638.    ----------
  639.  
  640.    function Coth (X : Float_Type'Base) return Float_Type'Base is
  641.    begin
  642.       if X = 0.0 then
  643.          raise Constraint_Error;
  644.  
  645.       elsif X < Half_Log_Epsilon then
  646.          return -1.0;
  647.  
  648.       elsif X > -Half_Log_Epsilon then
  649.          return 1.0;
  650.  
  651.       elsif abs X < Sqrt_Epsilon then
  652.          return 1.0 / X;
  653.       end if;
  654.  
  655.       return 1.0 / Float_Type'Base (Aux.Tanh (Double (X)));
  656.    end Coth;
  657.  
  658.    ---------
  659.    -- Exp --
  660.    ---------
  661.  
  662.    function Exp (X : Float_Type'Base) return Float_Type'Base is
  663.       Result : Float_Type'Base;
  664.  
  665.    begin
  666.       if X = 0.0 then
  667.          return 1.0;
  668.       end if;
  669.  
  670.       Result := Float_Type'Base (Aux.Exp (Double (X)));
  671.  
  672.       --  Deal with case of Exp returning IEEE infinity. If Machine_Overflows
  673.       --  is False, then we can just leave it as an infinity (and indeed we
  674.       --  prefer to do so). But if Machine_Overflows is True, then we have
  675.       --  to raise a Constraint_Error exception as required by the RM.
  676.  
  677.       if Float_Type'Machine_Overflows and then not Result'Valid then
  678.          raise Constraint_Error;
  679.       end if;
  680.  
  681.       return Result;
  682.    end Exp;
  683.  
  684.    ----------------
  685.    -- Exp_Strict --
  686.    ----------------
  687.  
  688.    function Exp_Strict (X : Float_Type'Base) return Float_Type'Base is
  689.       G : Float_Type'Base;
  690.       Z : Float_Type'Base;
  691.  
  692.       P0 : constant := 0.25000_00000_00000_00000;
  693.       P1 : constant := 0.75753_18015_94227_76666E-2;
  694.       P2 : constant := 0.31555_19276_56846_46356E-4;
  695.  
  696.       Q0 : constant := 0.5;
  697.       Q1 : constant := 0.56817_30269_85512_21787E-1;
  698.       Q2 : constant := 0.63121_89437_43985_02557E-3;
  699.       Q3 : constant := 0.75104_02839_98700_46114E-6;
  700.  
  701.       C1 : constant := 8#0.543#;
  702.       C2 : constant := -2.1219_44400_54690_58277E-4;
  703.       Le : constant := 1.4426_95040_88896_34074;
  704.  
  705.       XN : Float_Type'Base;
  706.       P, Q, R : Float_Type'Base;
  707.  
  708.    begin
  709.       if X = 0.0 then
  710.          return 1.0;
  711.       end if;
  712.  
  713.       XN := Float_Type'Base'Rounding (X * Le);
  714.       G := (X - XN * C1) - XN * C2;
  715.       Z := G * G;
  716.       P := G * ((P2 * Z + P1) * Z + P0);
  717.       Q := ((Q3 * Z + Q2) * Z + Q1) * Z + Q0;
  718.       R := 0.5 + P / (Q - P);
  719.  
  720.  
  721.       R := Float_Type'Base'Scaling (R, Integer (XN) + 1);
  722.  
  723.       --  Deal with case of Exp returning IEEE infinity. If Machine_Overflows
  724.       --  is False, then we can just leave it as an infinity (and indeed we
  725.       --  prefer to do so). But if Machine_Overflows is True, then we have
  726.       --  to raise a Constraint_Error exception as required by the RM.
  727.  
  728.       if Float_Type'Machine_Overflows and then not R'Valid then
  729.          raise Constraint_Error;
  730.       else
  731.          return R;
  732.       end if;
  733.  
  734.    end Exp_Strict;
  735.  
  736.  
  737.    ----------------
  738.    -- Local_Atan --
  739.    ----------------
  740.  
  741.    function Local_Atan
  742.      (Y    : Float_Type'Base;
  743.       X    : Float_Type'Base := 1.0)
  744.       return Float_Type'Base
  745.    is
  746.       Z        : Float_Type'Base;
  747.       Raw_Atan : Float_Type'Base;
  748.  
  749.    begin
  750.       if abs Y > abs X then
  751.          Z := abs (X / Y);
  752.       else
  753.          Z := abs (Y / X);
  754.       end if;
  755.  
  756.       if Z < Sqrt_Epsilon then
  757.          Raw_Atan := Z;
  758.  
  759.       elsif Z = 1.0 then
  760.          Raw_Atan := Pi / 4.0;
  761.  
  762.       else
  763.          Raw_Atan := Float_Type'Base (Aux.Atan (Double (Z)));
  764.       end if;
  765.  
  766.       if abs Y > abs X then
  767.          Raw_Atan := Half_Pi - Raw_Atan;
  768.       end if;
  769.  
  770.       if X > 0.0 then
  771.          if Y > 0.0 then
  772.             return Raw_Atan;
  773.          else                 --  Y < 0.0
  774.             return -Raw_Atan;
  775.          end if;
  776.  
  777.       else                    --  X < 0.0
  778.          if Y > 0.0 then
  779.             return Pi - Raw_Atan;
  780.          else                  --  Y < 0.0
  781.             return -(Pi - Raw_Atan);
  782.          end if;
  783.       end if;
  784.    end Local_Atan;
  785.  
  786.    ---------
  787.    -- Log --
  788.    ---------
  789.  
  790.    --  Natural base
  791.  
  792.    function Log (X : Float_Type'Base) return Float_Type'Base is
  793.    begin
  794.       if X < 0.0 then
  795.          raise Argument_Error;
  796.  
  797.       elsif X = 0.0 then
  798.          raise Constraint_Error;
  799.  
  800.       elsif X = 1.0 then
  801.          return 0.0;
  802.       end if;
  803.  
  804.       return Float_Type'Base (Aux.Log (Double (X)));
  805.    end Log;
  806.  
  807.    --  Arbitrary base
  808.  
  809.    function Log (X, Base : Float_Type'Base) return Float_Type'Base is
  810.    begin
  811.       if X < 0.0 then
  812.          raise Argument_Error;
  813.  
  814.       elsif Base <= 0.0 or else Base = 1.0 then
  815.          raise Argument_Error;
  816.  
  817.       elsif X = 0.0 then
  818.          raise Constraint_Error;
  819.  
  820.       elsif X = 1.0 then
  821.          return 0.0;
  822.       end if;
  823.  
  824.       return Float_Type'Base (Aux.Log (Double (X)) / Aux.Log (Double (Base)));
  825.    end Log;
  826.  
  827.    ---------
  828.    -- Sin --
  829.    ---------
  830.  
  831.    --  Natural cycle
  832.  
  833.    function Sin (X : Float_Type'Base) return Float_Type'Base is
  834.    begin
  835.       if abs X < Sqrt_Epsilon then
  836.          return X;
  837.       end if;
  838.  
  839.       return Float_Type'Base (Aux.Sin (Double (X)));
  840.    end Sin;
  841.  
  842.    --  Arbitrary cycle
  843.  
  844.    function Sin (X, Cycle : Float_Type'Base) return Float_Type'Base is
  845.       T            : Float_Type'Base;
  846.    begin
  847.       if Cycle <= 0.0 then
  848.          raise Argument_Error;
  849.  
  850.       elsif X = 0.0 then
  851.          --  Is this test really needed on any machine ???
  852.          return X;
  853.       end if;
  854.  
  855.       T := Float_Type'Base'Remainder (X, Cycle);
  856.  
  857.       --  The following two reductions reduce the argument
  858.       --  to the interval [-0.25 * Cycle, 0.25 * Cycle].
  859.       --  This reduction is exact and is needed to prevent
  860.       --  inaccuracy that may result if the sinus function
  861.       --  a different (more accurate) value of Pi in its
  862.       --  reduction than is used in the multiplication with Two_Pi.
  863.  
  864.       if abs T > 0.25 * Cycle then
  865.          T := 0.5 * Float_Type'Base'Copy_Sign (Cycle, T) - T;
  866.       end if;
  867.  
  868.       --  Could test for 12.0 * abs T = Cycle, and return
  869.       --  an exact value in those cases. It is not clear that
  870.       --  this is worth the extra test though.
  871.  
  872.       return  Float_Type'Base (Aux.Sin (Double (T / Cycle * Two_Pi)));
  873.    end Sin;
  874.  
  875.    ----------
  876.    -- Sinh --
  877.    ----------
  878.  
  879.    function Sinh (X : Float_Type'Base) return Float_Type'Base is
  880.       Lnv      : constant Float_Type'Base := 8#0.542714#;
  881.       V2minus1 : constant Float_Type'Base := 0.13830_27787_96019_02638E-4;
  882.       Y        : Float_Type'Base := abs X;
  883.       F        : constant Float_Type'Base := Y * Y;
  884.       Z        : Float_Type'Base;
  885.  
  886.       Float_Digits_1_6 : constant Boolean := Float_Type'Digits < 7;
  887.  
  888.    begin
  889.       if Y < Sqrt_Epsilon then
  890.          return X;
  891.  
  892.       elsif  Y > Log_Inverse_Epsilon then
  893.          Z := Exp_Strict (Y - Lnv);
  894.          Z := Z + V2minus1 * Z;
  895.  
  896.       elsif Y < 1.0 then
  897.  
  898.          if Float_Digits_1_6 then
  899.  
  900.             --  Use expansion provided by Cody and Waite, p. 226. Note that
  901.             --  leading term of the polynomial in Q is exactly 1.0.
  902.  
  903.             declare
  904.                P0 : constant := -0.71379_3159E+1;
  905.                P1 : constant := -0.19033_3399E+0;
  906.                Q0 : constant := -0.42827_7109E+2;
  907.  
  908.             begin
  909.                Z := Y + Y * F * (P1 * F + P0) / (F + Q0);
  910.             end;
  911.  
  912.          else
  913.             declare
  914.                P0 : constant := -0.35181_28343_01771_17881E+6;
  915.                P1 : constant := -0.11563_52119_68517_68270E+5;
  916.                P2 : constant := -0.16375_79820_26307_51372E+3;
  917.                P3 : constant := -0.78966_12741_73570_99479E+0;
  918.                Q0 : constant := -0.21108_77005_81062_71242E+7;
  919.                Q1 : constant :=  0.36162_72310_94218_36460E+5;
  920.                Q2 : constant := -0.27773_52311_96507_01667E+3;
  921.  
  922.             begin
  923.                Z := Y + Y * F * (((P3 * F + P2) * F + P1) * F + P0)
  924.                               / (((F + Q2) * F + Q1) * F + Q0);
  925.             end;
  926.          end if;
  927.       else
  928.          Z := Exp_Strict (Y);
  929.          Z := 0.5 * (Z - 1.0 / Z);
  930.       end if;
  931.  
  932.       if X > 0.0 then
  933.          return Z;
  934.       else
  935.          return -Z;
  936.       end if;
  937.    end Sinh;
  938.  
  939.    ----------
  940.    -- Sqrt --
  941.    ----------
  942.  
  943.    function Sqrt (X : Float_Type'Base) return Float_Type'Base is
  944.    begin
  945.       if X < 0.0 then
  946.          raise Argument_Error;
  947.  
  948.       --  Special case Sqrt (0.0) to preserve possible minus sign per IEEE
  949.  
  950.       elsif X = 0.0 then
  951.          return X;
  952.  
  953.       end if;
  954.  
  955.       return Float_Type'Base (Aux.Sqrt (Double (X)));
  956.    end Sqrt;
  957.  
  958.    ---------
  959.    -- Tan --
  960.    ---------
  961.  
  962.    --  Natural cycle
  963.  
  964.    function Tan (X : Float_Type'Base) return Float_Type'Base is
  965.    begin
  966.       if abs X < Sqrt_Epsilon then
  967.          return X;
  968.  
  969.       elsif abs X = Pi / 2.0 then
  970.          raise Constraint_Error;
  971.       end if;
  972.  
  973.       return Float_Type'Base (Aux.Tan (Double (X)));
  974.    end Tan;
  975.  
  976.    --  Arbitrary cycle
  977.  
  978.    function Tan (X, Cycle : Float_Type'Base) return Float_Type'Base is
  979.       T : Float_Type'Base;
  980.    begin
  981.       if Cycle <= 0.0 then
  982.          raise Argument_Error;
  983.  
  984.       elsif X = 0.0 then
  985.          return X;
  986.       end if;
  987.  
  988.       T := Float_Type'Base'Remainder (X, Cycle);
  989.  
  990.       if abs T = 0.25 * Cycle then
  991.          raise Constraint_Error;
  992.  
  993.       elsif abs T = 0.5 * Cycle then
  994.          return 0.0;
  995.  
  996.       else
  997.          T := T / Cycle * Two_Pi;
  998.          return Sin (T) / Cos (T);
  999.       end if;
  1000.  
  1001.    end Tan;
  1002.  
  1003.    ----------
  1004.    -- Tanh --
  1005.    ----------
  1006.  
  1007.    function Tanh (X : Float_Type'Base) return Float_Type'Base is
  1008.       P0 : constant Float_Type'Base := -0.16134_11902E4;
  1009.       P1 : constant Float_Type'Base := -0.99225_92967E2;
  1010.       P2 : constant Float_Type'Base := -0.96437_49299E0;
  1011.  
  1012.       Q0 : constant Float_Type'Base :=  0.48402_35707E4;
  1013.       Q1 : constant Float_Type'Base :=  0.22337_72071E4;
  1014.       Q2 : constant Float_Type'Base :=  0.11274_47438E3;
  1015.       Q3 : constant Float_Type'Base :=  0.10000000000E1;
  1016.  
  1017.       Half_Ln3 : constant Float_Type'Base := 0.54930_61443;
  1018.  
  1019.       P, Q, R : Float_Type'Base;
  1020.       Y : Float_Type'Base := abs X;
  1021.       G : Float_Type'Base := Y * Y;
  1022.  
  1023.       Float_Type_Digits_15_Or_More : constant Boolean :=
  1024.                                        Float_Type'Digits > 14;
  1025.  
  1026.    begin
  1027.       if X < Half_Log_Epsilon then
  1028.          return -1.0;
  1029.  
  1030.       elsif X > -Half_Log_Epsilon then
  1031.          return 1.0;
  1032.  
  1033.       elsif Y < Sqrt_Epsilon then
  1034.          return X;
  1035.  
  1036.       elsif Y < Half_Ln3
  1037.         and then Float_Type_Digits_15_Or_More
  1038.       then
  1039.          P := (P2 * G + P1) * G + P0;
  1040.          Q := ((Q3 * G + Q2) * G + Q1) * G + Q0;
  1041.          R := G * (P / Q);
  1042.          return X + X * R;
  1043.  
  1044.       else
  1045.          return Float_Type'Base (Aux.Tanh (Double (X)));
  1046.       end if;
  1047.    end Tanh;
  1048.  
  1049. end Ada.Numerics.Generic_Elementary_Functions;
  1050.