Remove some compile time warnings about duplicate definitions.
[official-gcc.git] / gcc / ada / a-ngcoty.adb
blobdf0b73ac54b496495b2bd0b5deddbee5f478e444
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUNTIME COMPONENTS --
4 -- --
5 -- A D A . N U M E R I C S . G E N E R I C _ C O M P L E X _ T Y P E S --
6 -- --
7 -- B o d y --
8 -- --
9 -- $Revision: 1.16 $
10 -- --
11 -- Copyright (C) 1992-2001 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 ------------------------------------------------------------------------------
36 with Ada.Numerics.Aux; use Ada.Numerics.Aux;
37 package body Ada.Numerics.Generic_Complex_Types is
39 subtype R is Real'Base;
41 Two_Pi : constant R := R (2.0) * Pi;
42 Half_Pi : constant R := Pi / R (2.0);
44 ---------
45 -- "*" --
46 ---------
48 function "*" (Left, Right : Complex) return Complex is
49 X : R;
50 Y : R;
52 begin
53 X := Left.Re * Right.Re - Left.Im * Right.Im;
54 Y := Left.Re * Right.Im + Left.Im * Right.Re;
56 -- If either component overflows, try to scale.
58 if abs (X) > R'Last then
59 X := R' (4.0) * (R'(Left.Re / 2.0) * R'(Right.Re / 2.0)
60 - R'(Left.Im / 2.0) * R'(Right.Im / 2.0));
61 end if;
63 if abs (Y) > R'Last then
64 Y := R' (4.0) * (R'(Left.Re / 2.0) * R'(Right.Im / 2.0)
65 - R'(Left.Im / 2.0) * R'(Right.Re / 2.0));
66 end if;
68 return (X, Y);
69 end "*";
71 function "*" (Left, Right : Imaginary) return Real'Base is
72 begin
73 return -R (Left) * R (Right);
74 end "*";
76 function "*" (Left : Complex; Right : Real'Base) return Complex is
77 begin
78 return Complex'(Left.Re * Right, Left.Im * Right);
79 end "*";
81 function "*" (Left : Real'Base; Right : Complex) return Complex is
82 begin
83 return (Left * Right.Re, Left * Right.Im);
84 end "*";
86 function "*" (Left : Complex; Right : Imaginary) return Complex is
87 begin
88 return Complex'(-(Left.Im * R (Right)), Left.Re * R (Right));
89 end "*";
91 function "*" (Left : Imaginary; Right : Complex) return Complex is
92 begin
93 return Complex'(-(R (Left) * Right.Im), R (Left) * Right.Re);
94 end "*";
96 function "*" (Left : Imaginary; Right : Real'Base) return Imaginary is
97 begin
98 return Left * Imaginary (Right);
99 end "*";
101 function "*" (Left : Real'Base; Right : Imaginary) return Imaginary is
102 begin
103 return Imaginary (Left * R (Right));
104 end "*";
106 ----------
107 -- "**" --
108 ----------
110 function "**" (Left : Complex; Right : Integer) return Complex is
111 Result : Complex := (1.0, 0.0);
112 Factor : Complex := Left;
113 Exp : Integer := Right;
115 begin
116 -- We use the standard logarithmic approach, Exp gets shifted right
117 -- testing successive low order bits and Factor is the value of the
118 -- base raised to the next power of 2. For positive exponents we
119 -- multiply the result by this factor, for negative exponents, we
120 -- divide by this factor.
122 if Exp >= 0 then
124 -- For a positive exponent, if we get a constraint error during
125 -- this loop, it is an overflow, and the constraint error will
126 -- simply be passed on to the caller.
128 while Exp /= 0 loop
129 if Exp rem 2 /= 0 then
130 Result := Result * Factor;
131 end if;
133 Factor := Factor * Factor;
134 Exp := Exp / 2;
135 end loop;
137 return Result;
139 else -- Exp < 0 then
141 -- For the negative exponent case, a constraint error during this
142 -- calculation happens if Factor gets too large, and the proper
143 -- response is to return 0.0, since what we essentially have is
144 -- 1.0 / infinity, and the closest model number will be zero.
146 begin
148 while Exp /= 0 loop
149 if Exp rem 2 /= 0 then
150 Result := Result * Factor;
151 end if;
153 Factor := Factor * Factor;
154 Exp := Exp / 2;
155 end loop;
157 return R ' (1.0) / Result;
159 exception
161 when Constraint_Error =>
162 return (0.0, 0.0);
163 end;
164 end if;
165 end "**";
167 function "**" (Left : Imaginary; Right : Integer) return Complex is
168 M : R := R (Left) ** Right;
169 begin
170 case Right mod 4 is
171 when 0 => return (M, 0.0);
172 when 1 => return (0.0, M);
173 when 2 => return (-M, 0.0);
174 when 3 => return (0.0, -M);
175 when others => raise Program_Error;
176 end case;
177 end "**";
179 ---------
180 -- "+" --
181 ---------
183 function "+" (Right : Complex) return Complex is
184 begin
185 return Right;
186 end "+";
188 function "+" (Left, Right : Complex) return Complex is
189 begin
190 return Complex'(Left.Re + Right.Re, Left.Im + Right.Im);
191 end "+";
193 function "+" (Right : Imaginary) return Imaginary is
194 begin
195 return Right;
196 end "+";
198 function "+" (Left, Right : Imaginary) return Imaginary is
199 begin
200 return Imaginary (R (Left) + R (Right));
201 end "+";
203 function "+" (Left : Complex; Right : Real'Base) return Complex is
204 begin
205 return Complex'(Left.Re + Right, Left.Im);
206 end "+";
208 function "+" (Left : Real'Base; Right : Complex) return Complex is
209 begin
210 return Complex'(Left + Right.Re, Right.Im);
211 end "+";
213 function "+" (Left : Complex; Right : Imaginary) return Complex is
214 begin
215 return Complex'(Left.Re, Left.Im + R (Right));
216 end "+";
218 function "+" (Left : Imaginary; Right : Complex) return Complex is
219 begin
220 return Complex'(Right.Re, R (Left) + Right.Im);
221 end "+";
223 function "+" (Left : Imaginary; Right : Real'Base) return Complex is
224 begin
225 return Complex'(Right, R (Left));
226 end "+";
228 function "+" (Left : Real'Base; Right : Imaginary) return Complex is
229 begin
230 return Complex'(Left, R (Right));
231 end "+";
233 ---------
234 -- "-" --
235 ---------
237 function "-" (Right : Complex) return Complex is
238 begin
239 return (-Right.Re, -Right.Im);
240 end "-";
242 function "-" (Left, Right : Complex) return Complex is
243 begin
244 return (Left.Re - Right.Re, Left.Im - Right.Im);
245 end "-";
247 function "-" (Right : Imaginary) return Imaginary is
248 begin
249 return Imaginary (-R (Right));
250 end "-";
252 function "-" (Left, Right : Imaginary) return Imaginary is
253 begin
254 return Imaginary (R (Left) - R (Right));
255 end "-";
257 function "-" (Left : Complex; Right : Real'Base) return Complex is
258 begin
259 return Complex'(Left.Re - Right, Left.Im);
260 end "-";
262 function "-" (Left : Real'Base; Right : Complex) return Complex is
263 begin
264 return Complex'(Left - Right.Re, -Right.Im);
265 end "-";
267 function "-" (Left : Complex; Right : Imaginary) return Complex is
268 begin
269 return Complex'(Left.Re, Left.Im - R (Right));
270 end "-";
272 function "-" (Left : Imaginary; Right : Complex) return Complex is
273 begin
274 return Complex'(-Right.Re, R (Left) - Right.Im);
275 end "-";
277 function "-" (Left : Imaginary; Right : Real'Base) return Complex is
278 begin
279 return Complex'(-Right, R (Left));
280 end "-";
282 function "-" (Left : Real'Base; Right : Imaginary) return Complex is
283 begin
284 return Complex'(Left, -R (Right));
285 end "-";
287 ---------
288 -- "/" --
289 ---------
291 function "/" (Left, Right : Complex) return Complex is
292 a : constant R := Left.Re;
293 b : constant R := Left.Im;
294 c : constant R := Right.Re;
295 d : constant R := Right.Im;
297 begin
298 if c = 0.0 and then d = 0.0 then
299 raise Constraint_Error;
300 else
301 return Complex'(Re => ((a * c) + (b * d)) / (c ** 2 + d ** 2),
302 Im => ((b * c) - (a * d)) / (c ** 2 + d ** 2));
303 end if;
304 end "/";
306 function "/" (Left, Right : Imaginary) return Real'Base is
307 begin
308 return R (Left) / R (Right);
309 end "/";
311 function "/" (Left : Complex; Right : Real'Base) return Complex is
312 begin
313 return Complex'(Left.Re / Right, Left.Im / Right);
314 end "/";
316 function "/" (Left : Real'Base; Right : Complex) return Complex is
317 a : constant R := Left;
318 c : constant R := Right.Re;
319 d : constant R := Right.Im;
320 begin
321 return Complex'(Re => (a * c) / (c ** 2 + d ** 2),
322 Im => -(a * d) / (c ** 2 + d ** 2));
323 end "/";
325 function "/" (Left : Complex; Right : Imaginary) return Complex is
326 a : constant R := Left.Re;
327 b : constant R := Left.Im;
328 d : constant R := R (Right);
330 begin
331 return (b / d, -a / d);
332 end "/";
334 function "/" (Left : Imaginary; Right : Complex) return Complex is
335 b : constant R := R (Left);
336 c : constant R := Right.Re;
337 d : constant R := Right.Im;
339 begin
340 return (Re => b * d / (c ** 2 + d ** 2),
341 Im => b * c / (c ** 2 + d ** 2));
342 end "/";
344 function "/" (Left : Imaginary; Right : Real'Base) return Imaginary is
345 begin
346 return Imaginary (R (Left) / Right);
347 end "/";
349 function "/" (Left : Real'Base; Right : Imaginary) return Imaginary is
350 begin
351 return Imaginary (-Left / R (Right));
352 end "/";
354 ---------
355 -- "<" --
356 ---------
358 function "<" (Left, Right : Imaginary) return Boolean is
359 begin
360 return R (Left) < R (Right);
361 end "<";
363 ----------
364 -- "<=" --
365 ----------
367 function "<=" (Left, Right : Imaginary) return Boolean is
368 begin
369 return R (Left) <= R (Right);
370 end "<=";
372 ---------
373 -- ">" --
374 ---------
376 function ">" (Left, Right : Imaginary) return Boolean is
377 begin
378 return R (Left) > R (Right);
379 end ">";
381 ----------
382 -- ">=" --
383 ----------
385 function ">=" (Left, Right : Imaginary) return Boolean is
386 begin
387 return R (Left) >= R (Right);
388 end ">=";
390 -----------
391 -- "abs" --
392 -----------
394 function "abs" (Right : Imaginary) return Real'Base is
395 begin
396 return abs R (Right);
397 end "abs";
399 --------------
400 -- Argument --
401 --------------
403 function Argument (X : Complex) return Real'Base is
404 a : constant R := X.Re;
405 b : constant R := X.Im;
406 arg : R;
408 begin
409 if b = 0.0 then
411 if a >= 0.0 then
412 return 0.0;
413 else
414 return R'Copy_Sign (Pi, b);
415 end if;
417 elsif a = 0.0 then
419 if b >= 0.0 then
420 return Half_Pi;
421 else
422 return -Half_Pi;
423 end if;
425 else
426 arg := R (Atan (Double (abs (b / a))));
428 if a > 0.0 then
429 if b > 0.0 then
430 return arg;
431 else -- b < 0.0
432 return -arg;
433 end if;
435 else -- a < 0.0
436 if b >= 0.0 then
437 return Pi - arg;
438 else -- b < 0.0
439 return -(Pi - arg);
440 end if;
441 end if;
442 end if;
444 exception
445 when Constraint_Error =>
446 if b > 0.0 then
447 return Half_Pi;
448 else
449 return -Half_Pi;
450 end if;
451 end Argument;
453 function Argument (X : Complex; Cycle : Real'Base) return Real'Base is
454 begin
455 if Cycle > 0.0 then
456 return Argument (X) * Cycle / Two_Pi;
457 else
458 raise Argument_Error;
459 end if;
460 end Argument;
462 ----------------------------
463 -- Compose_From_Cartesian --
464 ----------------------------
466 function Compose_From_Cartesian (Re, Im : Real'Base) return Complex is
467 begin
468 return (Re, Im);
469 end Compose_From_Cartesian;
471 function Compose_From_Cartesian (Re : Real'Base) return Complex is
472 begin
473 return (Re, 0.0);
474 end Compose_From_Cartesian;
476 function Compose_From_Cartesian (Im : Imaginary) return Complex is
477 begin
478 return (0.0, R (Im));
479 end Compose_From_Cartesian;
481 ------------------------
482 -- Compose_From_Polar --
483 ------------------------
485 function Compose_From_Polar (
486 Modulus, Argument : Real'Base)
487 return Complex
489 begin
490 if Modulus = 0.0 then
491 return (0.0, 0.0);
492 else
493 return (Modulus * R (Cos (Double (Argument))),
494 Modulus * R (Sin (Double (Argument))));
495 end if;
496 end Compose_From_Polar;
498 function Compose_From_Polar (
499 Modulus, Argument, Cycle : Real'Base)
500 return Complex
502 Arg : Real'Base;
504 begin
505 if Modulus = 0.0 then
506 return (0.0, 0.0);
508 elsif Cycle > 0.0 then
509 if Argument = 0.0 then
510 return (Modulus, 0.0);
512 elsif Argument = Cycle / 4.0 then
513 return (0.0, Modulus);
515 elsif Argument = Cycle / 2.0 then
516 return (-Modulus, 0.0);
518 elsif Argument = 3.0 * Cycle / R (4.0) then
519 return (0.0, -Modulus);
520 else
521 Arg := Two_Pi * Argument / Cycle;
522 return (Modulus * R (Cos (Double (Arg))),
523 Modulus * R (Sin (Double (Arg))));
524 end if;
525 else
526 raise Argument_Error;
527 end if;
528 end Compose_From_Polar;
530 ---------------
531 -- Conjugate --
532 ---------------
534 function Conjugate (X : Complex) return Complex is
535 begin
536 return Complex'(X.Re, -X.Im);
537 end Conjugate;
539 --------
540 -- Im --
541 --------
543 function Im (X : Complex) return Real'Base is
544 begin
545 return X.Im;
546 end Im;
548 function Im (X : Imaginary) return Real'Base is
549 begin
550 return R (X);
551 end Im;
553 -------------
554 -- Modulus --
555 -------------
557 function Modulus (X : Complex) return Real'Base is
558 Re2, Im2 : R;
560 begin
562 begin
563 Re2 := X.Re ** 2;
565 -- To compute (a**2 + b**2) ** (0.5) when a**2 may be out of bounds,
566 -- compute a * (1 + (b/a) **2) ** (0.5). On a machine where the
567 -- squaring does not raise constraint_error but generates infinity,
568 -- we can use an explicit comparison to determine whether to use
569 -- the scaling expression.
571 if Re2 > R'Last then
572 raise Constraint_Error;
573 end if;
575 exception
576 when Constraint_Error =>
577 return abs (X.Re)
578 * R (Sqrt (Double (R (1.0) + (X.Im / X.Re) ** 2)));
579 end;
581 begin
582 Im2 := X.Im ** 2;
584 if Im2 > R'Last then
585 raise Constraint_Error;
586 end if;
588 exception
589 when Constraint_Error =>
590 return abs (X.Im)
591 * R (Sqrt (Double (R (1.0) + (X.Re / X.Im) ** 2)));
592 end;
594 -- Now deal with cases of underflow. If only one of the squares
595 -- underflows, return the modulus of the other component. If both
596 -- squares underflow, use scaling as above.
598 if Re2 = 0.0 then
600 if X.Re = 0.0 then
601 return abs (X.Im);
603 elsif Im2 = 0.0 then
605 if X.Im = 0.0 then
606 return abs (X.Re);
608 else
609 if abs (X.Re) > abs (X.Im) then
610 return
611 abs (X.Re)
612 * R (Sqrt (Double (R (1.0) + (X.Im / X.Re) ** 2)));
613 else
614 return
615 abs (X.Im)
616 * R (Sqrt (Double (R (1.0) + (X.Re / X.Im) ** 2)));
617 end if;
618 end if;
620 else
621 return abs (X.Im);
622 end if;
625 elsif Im2 = 0.0 then
626 return abs (X.Re);
628 -- in all other cases, the naive computation will do.
630 else
631 return R (Sqrt (Double (Re2 + Im2)));
632 end if;
633 end Modulus;
635 --------
636 -- Re --
637 --------
639 function Re (X : Complex) return Real'Base is
640 begin
641 return X.Re;
642 end Re;
644 ------------
645 -- Set_Im --
646 ------------
648 procedure Set_Im (X : in out Complex; Im : in Real'Base) is
649 begin
650 X.Im := Im;
651 end Set_Im;
653 procedure Set_Im (X : out Imaginary; Im : in Real'Base) is
654 begin
655 X := Imaginary (Im);
656 end Set_Im;
658 ------------
659 -- Set_Re --
660 ------------
662 procedure Set_Re (X : in out Complex; Re : in Real'Base) is
663 begin
664 X.Re := Re;
665 end Set_Re;
667 end Ada.Numerics.Generic_Complex_Types;