1 ------------------------------------------------------------------------------
3 -- GNAT RUNTIME COMPONENTS --
5 -- ADA.NUMERICS.GENERIC_ELEMENTARY_FUNCTIONS --
11 -- Copyright (C) 1992-2000, Free Software Foundation, Inc. --
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. --
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. --
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). --
34 ------------------------------------------------------------------------------
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.
42 -- Uses functions sqrt, exp, log, pow, sin, asin, cos, acos, tan, atan,
43 -- sinh, cosh, tanh from C library via math.h
45 with Ada.Numerics.Aux;
47 package body Ada.Numerics.Generic_Elementary_Functions is
49 use type Ada.Numerics.Aux.Double;
51 Sqrt_Two : constant := 1.41421_35623_73095_04880_16887_24209_69807_85696;
52 Log_Two : constant := 0.69314_71805_59945_30941_72321_21458_17656_80755;
53 Half_Log_Two : constant := Log_Two / 2;
56 subtype T is Float_Type'Base;
57 subtype Double is Aux.Double;
60 Two_Pi : constant T := 2.0 * Pi;
61 Half_Pi : constant T := Pi / 2.0;
62 Fourth_Pi : constant T := Pi / 4.0;
64 Epsilon : constant T := 2.0 ** (1 - T'Model_Mantissa);
65 IEpsilon : constant T := 2.0 ** (T'Model_Mantissa - 1);
66 Log_Epsilon : constant T := T (1 - T'Model_Mantissa) * Log_Two;
67 Half_Log_Epsilon : constant T := T (1 - T'Model_Mantissa) * Half_Log_Two;
68 Log_Inverse_Epsilon : constant T := T (T'Model_Mantissa - 1) * Log_Two;
69 Sqrt_Epsilon : constant T := Sqrt_Two ** (1 - T'Model_Mantissa);
72 DEpsilon : constant Double := Double (Epsilon);
73 DIEpsilon : constant Double := Double (IEpsilon);
75 -----------------------
76 -- Local Subprograms --
77 -----------------------
79 function Exp_Strict (X : Float_Type'Base) return Float_Type'Base;
80 -- Cody/Waite routine, supposedly more precise than the library
81 -- version. Currently only needed for Sinh/Cosh on X86 with the largest
86 X : Float_Type'Base := 1.0)
87 return Float_Type'Base;
88 -- Common code for arc tangent after cyele reduction
94 function "**" (Left, Right : Float_Type'Base) return Float_Type'Base is
95 A_Right : Float_Type'Base;
97 Result : Float_Type'Base;
99 Rest : Float_Type'Base;
105 raise Argument_Error;
107 elsif Left < 0.0 then
108 raise Argument_Error;
110 elsif Right = 0.0 then
113 elsif Left = 0.0 then
115 raise Constraint_Error;
120 elsif Left = 1.0 then
123 elsif Right = 1.0 then
131 elsif Right = 0.5 then
135 A_Right := abs (Right);
137 -- If exponent is larger than one, compute integer exponen-
138 -- tiation if possible, and evaluate fractional part with
139 -- more precision. The relative error is now proportional
140 -- to the fractional part of the exponent only.
143 and then A_Right < Float_Type'Base (Integer'Last)
145 Int_Part := Integer (Float_Type'Base'Truncation (A_Right));
146 Result := Left ** Int_Part;
147 Rest := A_Right - Float_Type'Base (Int_Part);
149 -- Compute with two leading bits of the mantissa using
150 -- square roots. Bound to be better than logarithms, and
151 -- easily extended to greater precision.
155 Result := Result * R1;
159 Result := Result * Sqrt (R1);
163 elsif Rest >= 0.25 then
164 Result := Result * Sqrt (Sqrt (Left));
169 Float_Type'Base (Aux.Pow (Double (Left), Double (Rest)));
174 return (1.0 / Result);
178 Float_Type'Base (Aux.Pow (Double (Left), Double (Right)));
184 raise Constraint_Error;
195 function Arccos (X : Float_Type'Base) return Float_Type'Base is
196 Temp : Float_Type'Base;
200 raise Argument_Error;
202 elsif abs X < Sqrt_Epsilon then
212 Temp := Float_Type'Base (Aux.Acos (Double (X)));
223 function Arccos (X, Cycle : Float_Type'Base) return Float_Type'Base is
224 Temp : Float_Type'Base;
228 raise Argument_Error;
230 elsif abs X > 1.0 then
231 raise Argument_Error;
233 elsif abs X < Sqrt_Epsilon then
243 Temp := Arctan (Sqrt ((1.0 - X) * (1.0 + X)) / X, 1.0, Cycle);
246 Temp := Cycle / 2.0 + Temp;
256 function Arccosh (X : Float_Type'Base) return Float_Type'Base is
258 -- Return positive branch of Log (X - Sqrt (X * X - 1.0)), or
259 -- the proper approximation for X close to 1 or >> 1.
262 raise Argument_Error;
264 elsif X < 1.0 + Sqrt_Epsilon then
265 return Sqrt (2.0 * (X - 1.0));
267 elsif X > 1.0 / Sqrt_Epsilon then
268 return Log (X) + Log_Two;
271 return Log (X + Sqrt ((X - 1.0) * (X + 1.0)));
282 (X : Float_Type'Base;
283 Y : Float_Type'Base := 1.0)
284 return Float_Type'Base
287 -- Just reverse arguments
289 return Arctan (Y, X);
295 (X : Float_Type'Base;
296 Y : Float_Type'Base := 1.0;
297 Cycle : Float_Type'Base)
298 return Float_Type'Base
301 -- Just reverse arguments
303 return Arctan (Y, X, Cycle);
310 function Arccoth (X : Float_Type'Base) return Float_Type'Base is
313 return Arctanh (1.0 / X);
315 elsif abs X = 1.0 then
316 raise Constraint_Error;
318 elsif abs X < 1.0 then
319 raise Argument_Error;
322 -- 1.0 < abs X <= 2.0. One of X + 1.0 and X - 1.0 is exact, the
323 -- other has error 0 or Epsilon.
325 return 0.5 * (Log (abs (X + 1.0)) - Log (abs (X - 1.0)));
335 function Arcsin (X : Float_Type'Base) return Float_Type'Base is
338 raise Argument_Error;
340 elsif abs X < Sqrt_Epsilon then
350 return Float_Type'Base (Aux.Asin (Double (X)));
355 function Arcsin (X, Cycle : Float_Type'Base) return Float_Type'Base is
358 raise Argument_Error;
360 elsif abs X > 1.0 then
361 raise Argument_Error;
373 return Arctan (X / Sqrt ((1.0 - X) * (1.0 + X)), 1.0, Cycle);
380 function Arcsinh (X : Float_Type'Base) return Float_Type'Base is
382 if abs X < Sqrt_Epsilon then
385 elsif X > 1.0 / Sqrt_Epsilon then
386 return Log (X) + Log_Two;
388 elsif X < -1.0 / Sqrt_Epsilon then
389 return -(Log (-X) + Log_Two);
392 return -Log (abs X + Sqrt (X * X + 1.0));
395 return Log (X + Sqrt (X * X + 1.0));
406 (Y : Float_Type'Base;
407 X : Float_Type'Base := 1.0)
408 return Float_Type'Base
414 raise Argument_Error;
420 return Pi * Float_Type'Copy_Sign (1.0, Y);
431 return Local_Atan (Y, X);
438 (Y : Float_Type'Base;
439 X : Float_Type'Base := 1.0;
440 Cycle : Float_Type'Base)
441 return Float_Type'Base
445 raise Argument_Error;
450 raise Argument_Error;
456 return Cycle / 2.0 * Float_Type'Copy_Sign (1.0, Y);
467 return Local_Atan (Y, X) * Cycle / Two_Pi;
475 function Arctanh (X : Float_Type'Base) return Float_Type'Base is
476 A, B, D, A_Plus_1, A_From_1 : Float_Type'Base;
477 Mantissa : constant Integer := Float_Type'Base'Machine_Mantissa;
480 -- The naive formula:
482 -- Arctanh (X) := (1/2) * Log (1 + X) / (1 - X)
484 -- is not well-behaved numerically when X < 0.5 and when X is close
485 -- to one. The following is accurate but probably not optimal.
488 raise Constraint_Error;
490 elsif abs X >= 1.0 - 2.0 ** (-Mantissa) then
493 raise Argument_Error;
496 -- The one case that overflows if put through the method below:
497 -- abs X = 1.0 - Epsilon. In this case (1/2) log (2/Epsilon) is
498 -- accurate. This simplifies to:
500 return Float_Type'Copy_Sign (
501 Half_Log_Two * Float_Type'Base (Mantissa + 1), X);
504 -- elsif abs X <= 0.5 then
505 -- why is above line commented out ???
508 -- Use several piecewise linear approximations.
509 -- A is close to X, chosen so 1.0 + A, 1.0 - A, and X - A are exact.
510 -- The two scalings remove the low-order bits of X.
512 A := Float_Type'Base'Scaling (
513 Float_Type'Base (Long_Long_Integer
514 (Float_Type'Base'Scaling (X, Mantissa - 1))), 1 - Mantissa);
516 B := X - A; -- This is exact; abs B <= 2**(-Mantissa).
517 A_Plus_1 := 1.0 + A; -- This is exact.
518 A_From_1 := 1.0 - A; -- Ditto.
519 D := A_Plus_1 * A_From_1; -- 1 - A*A.
521 -- use one term of the series expansion:
522 -- f (x + e) = f(x) + e * f'(x) + ..
524 -- The derivative of Arctanh at A is 1/(1-A*A). Next term is
525 -- A*(B/D)**2 (if a quadratic approximation is ever needed).
527 return 0.5 * (Log (A_Plus_1) - Log (A_From_1)) + B / D;
530 -- return 0.5 * Log ((X + 1.0) / (1.0 - X));
531 -- why are above lines commented out ???
541 function Cos (X : Float_Type'Base) return Float_Type'Base is
546 elsif abs X < Sqrt_Epsilon then
551 return Float_Type'Base (Aux.Cos (Double (X)));
556 function Cos (X, Cycle : Float_Type'Base) return Float_Type'Base is
558 -- Just reuse the code for Sin. The potential small
559 -- loss of speed is negligible with proper (front-end) inlining.
561 -- ??? Add pragma Inline_Always in spec when this is supported
562 return -Sin (abs X - Cycle * 0.25, Cycle);
569 function Cosh (X : Float_Type'Base) return Float_Type'Base is
570 Lnv : constant Float_Type'Base := 8#0.542714#;
571 V2minus1 : constant Float_Type'Base := 0.13830_27787_96019_02638E-4;
572 Y : Float_Type'Base := abs X;
576 if Y < Sqrt_Epsilon then
579 elsif Y > Log_Inverse_Epsilon then
580 Z := Exp_Strict (Y - Lnv);
581 return (Z + V2minus1 * Z);
585 return 0.5 * (Z + 1.0 / Z);
596 function Cot (X : Float_Type'Base) return Float_Type'Base is
599 raise Constraint_Error;
601 elsif abs X < Sqrt_Epsilon then
605 return 1.0 / Float_Type'Base (Aux.Tan (Double (X)));
610 function Cot (X, Cycle : Float_Type'Base) return Float_Type'Base is
615 raise Argument_Error;
618 T := Float_Type'Base'Remainder (X, Cycle);
620 if T = 0.0 or abs T = 0.5 * Cycle then
621 raise Constraint_Error;
623 elsif abs T < Sqrt_Epsilon then
626 elsif abs T = 0.25 * Cycle then
630 T := T / Cycle * Two_Pi;
631 return Cos (T) / Sin (T);
639 function Coth (X : Float_Type'Base) return Float_Type'Base is
642 raise Constraint_Error;
644 elsif X < Half_Log_Epsilon then
647 elsif X > -Half_Log_Epsilon then
650 elsif abs X < Sqrt_Epsilon then
654 return 1.0 / Float_Type'Base (Aux.Tanh (Double (X)));
661 function Exp (X : Float_Type'Base) return Float_Type'Base is
662 Result : Float_Type'Base;
669 Result := Float_Type'Base (Aux.Exp (Double (X)));
671 -- Deal with case of Exp returning IEEE infinity. If Machine_Overflows
672 -- is False, then we can just leave it as an infinity (and indeed we
673 -- prefer to do so). But if Machine_Overflows is True, then we have
674 -- to raise a Constraint_Error exception as required by the RM.
676 if Float_Type'Machine_Overflows and then not Result'Valid then
677 raise Constraint_Error;
687 function Exp_Strict (X : Float_Type'Base) return Float_Type'Base is
691 P0 : constant := 0.25000_00000_00000_00000;
692 P1 : constant := 0.75753_18015_94227_76666E-2;
693 P2 : constant := 0.31555_19276_56846_46356E-4;
695 Q0 : constant := 0.5;
696 Q1 : constant := 0.56817_30269_85512_21787E-1;
697 Q2 : constant := 0.63121_89437_43985_02557E-3;
698 Q3 : constant := 0.75104_02839_98700_46114E-6;
700 C1 : constant := 8#0.543#;
701 C2 : constant := -2.1219_44400_54690_58277E-4;
702 Le : constant := 1.4426_95040_88896_34074;
704 XN : Float_Type'Base;
705 P, Q, R : Float_Type'Base;
712 XN := Float_Type'Base'Rounding (X * Le);
713 G := (X - XN * C1) - XN * C2;
715 P := G * ((P2 * Z + P1) * Z + P0);
716 Q := ((Q3 * Z + Q2) * Z + Q1) * Z + Q0;
717 R := 0.5 + P / (Q - P);
720 R := Float_Type'Base'Scaling (R, Integer (XN) + 1);
722 -- Deal with case of Exp returning IEEE infinity. If Machine_Overflows
723 -- is False, then we can just leave it as an infinity (and indeed we
724 -- prefer to do so). But if Machine_Overflows is True, then we have
725 -- to raise a Constraint_Error exception as required by the RM.
727 if Float_Type'Machine_Overflows and then not R'Valid then
728 raise Constraint_Error;
741 (Y : Float_Type'Base;
742 X : Float_Type'Base := 1.0)
743 return Float_Type'Base
746 Raw_Atan : Float_Type'Base;
749 if abs Y > abs X then
755 if Z < Sqrt_Epsilon then
759 Raw_Atan := Pi / 4.0;
762 Raw_Atan := Float_Type'Base (Aux.Atan (Double (Z)));
765 if abs Y > abs X then
766 Raw_Atan := Half_Pi - Raw_Atan;
778 return Pi - Raw_Atan;
780 return -(Pi - Raw_Atan);
791 function Log (X : Float_Type'Base) return Float_Type'Base is
794 raise Argument_Error;
797 raise Constraint_Error;
803 return Float_Type'Base (Aux.Log (Double (X)));
808 function Log (X, Base : Float_Type'Base) return Float_Type'Base is
811 raise Argument_Error;
813 elsif Base <= 0.0 or else Base = 1.0 then
814 raise Argument_Error;
817 raise Constraint_Error;
823 return Float_Type'Base (Aux.Log (Double (X)) / Aux.Log (Double (Base)));
832 function Sin (X : Float_Type'Base) return Float_Type'Base is
834 if abs X < Sqrt_Epsilon then
838 return Float_Type'Base (Aux.Sin (Double (X)));
843 function Sin (X, Cycle : Float_Type'Base) return Float_Type'Base is
848 raise Argument_Error;
851 -- Is this test really needed on any machine ???
855 T := Float_Type'Base'Remainder (X, Cycle);
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.
864 if abs T > 0.25 * Cycle then
865 T := 0.5 * Float_Type'Copy_Sign (Cycle, T) - T;
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.
872 return Float_Type'Base (Aux.Sin (Double (T / Cycle * Two_Pi)));
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;
886 Float_Digits_1_6 : constant Boolean := Float_Type'Digits < 7;
889 if Y < Sqrt_Epsilon then
892 elsif Y > Log_Inverse_Epsilon then
893 Z := Exp_Strict (Y - Lnv);
894 Z := Z + V2minus1 * Z;
898 if Float_Digits_1_6 then
900 -- Use expansion provided by Cody and Waite, p. 226. Note that
901 -- leading term of the polynomial in Q is exactly 1.0.
904 P0 : constant := -0.71379_3159E+1;
905 P1 : constant := -0.19033_3399E+0;
906 Q0 : constant := -0.42827_7109E+2;
909 Z := Y + Y * F * (P1 * F + P0) / (F + Q0);
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;
923 Z := Y + Y * F * (((P3 * F + P2) * F + P1) * F + P0)
924 / (((F + Q2) * F + Q1) * F + Q0);
930 Z := 0.5 * (Z - 1.0 / Z);
944 function Sqrt (X : Float_Type'Base) return Float_Type'Base is
947 raise Argument_Error;
949 -- Special case Sqrt (0.0) to preserve possible minus sign per IEEE
956 return Float_Type'Base (Aux.Sqrt (Double (X)));
965 function Tan (X : Float_Type'Base) return Float_Type'Base is
967 if abs X < Sqrt_Epsilon then
970 elsif abs X = Pi / 2.0 then
971 raise Constraint_Error;
974 return Float_Type'Base (Aux.Tan (Double (X)));
979 function Tan (X, Cycle : Float_Type'Base) return Float_Type'Base is
984 raise Argument_Error;
990 T := Float_Type'Base'Remainder (X, Cycle);
992 if abs T = 0.25 * Cycle then
993 raise Constraint_Error;
995 elsif abs T = 0.5 * Cycle then
999 T := T / Cycle * Two_Pi;
1000 return Sin (T) / Cos (T);
1009 function Tanh (X : Float_Type'Base) return Float_Type'Base is
1010 P0 : constant Float_Type'Base := -0.16134_11902E4;
1011 P1 : constant Float_Type'Base := -0.99225_92967E2;
1012 P2 : constant Float_Type'Base := -0.96437_49299E0;
1014 Q0 : constant Float_Type'Base := 0.48402_35707E4;
1015 Q1 : constant Float_Type'Base := 0.22337_72071E4;
1016 Q2 : constant Float_Type'Base := 0.11274_47438E3;
1017 Q3 : constant Float_Type'Base := 0.10000000000E1;
1019 Half_Ln3 : constant Float_Type'Base := 0.54930_61443;
1021 P, Q, R : Float_Type'Base;
1022 Y : Float_Type'Base := abs X;
1023 G : Float_Type'Base := Y * Y;
1025 Float_Type_Digits_15_Or_More : constant Boolean :=
1026 Float_Type'Digits > 14;
1029 if X < Half_Log_Epsilon then
1032 elsif X > -Half_Log_Epsilon then
1035 elsif Y < Sqrt_Epsilon then
1039 and then Float_Type_Digits_15_Or_More
1041 P := (P2 * G + P1) * G + P0;
1042 Q := ((Q3 * G + Q2) * G + Q1) * G + Q0;
1047 return Float_Type'Base (Aux.Tanh (Double (X)));
1051 end Ada.Numerics.Generic_Elementary_Functions;