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