------------------------------------------------------------------------------ -- -- -- G N U . M U L T I P L E _ P R E C I S I O N . Q -- -- -- -- S p e c -- -- -- -- $Revision: 1.1 $ -- -- -- -- Copyright (C) 1999 Michael Roe -- -- -- -- This 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 2, 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. See the GNU General Public License -- -- for more details. You should have received a copy of the GNU General -- -- Public License distributed with GNAT; see file COPYING. If not, write -- -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, -- -- MA 02111-1307, USA. -- -- -- -- As a special exception, if other files instantiate generics from this -- -- unit, or you link this unit with other files to produce an executable, -- -- this unit does not by itself cause the resulting executable to be -- -- covered by the GNU General Public License. This exception does not -- -- however invalidate any other reasons why the executable file might be -- -- covered by the GNU Public License. -- -- -- ------------------------------------------------------------------------------ with Ada.Streams; with Interfaces.C; with GNU_Multiple_Precision.Z; package GNU_Multiple_Precision.Q is pragma Remote_Types (GNU_Multiple_Precision.Q); -- We want to be able to pass Big_Rationals between a client and a server -- so this package is declared Remote_Types type Big_Rational is limited record Numerator : GNU_Multiple_Precision.Z.Big_Integer; Denominator: GNU_Multiple_Precision.Z.Big_Integer; end record; -- Big_Rational is NOT declared as a private type, because many legitimate -- algorithms need to access the numerator and denominator separately ------------------------------------------------------------------------------ -- Initialization and assignment -- ------------------------------------------------------------------------------ procedure Init (Rop : out Big_Rational); pragma Import (C, Init, "mpq_init"); procedure Clear (Rop : in out Big_Rational); pragma Import (C, Clear, "mpq_clear"); -- The assignent functions do not canonicalize the assigned variable. -- It is the responsiblity of the user to call Canonicalize explicitly procedure Canonicalize (Rop : in out Big_Rational); pragma Import (C, Canonicalize, "mpq_canonlicalize"); --------- -- Set -- --------- procedure Set (Rop : in out Big_Rational; Op : in Big_Rational); pragma Import (C, Set, "mpq_set"); procedure Set_Unsigned (Rop : in out Big_Rational; Numerator : in Interfaces.C.unsigned; Denominator : in Interfaces.C.unsigned); pragma Import (C, Set_Unsigned, "mpq_set_ui"); procedure Set_Integer (Rop : in out Big_Rational; Numerator : in Interfaces.C.int; Denominator : in Interfaces.C.int); pragma Import (C, Set_Integer, "mpq_set_si"); ------------------------------------------------------------------------------ -- Arithmetic -- ------------------------------------------------------------------------------ procedure Add (Rop : in out Big_Rational; Op1, Op2 : in Big_Rational); pragma Import (C, Add, "mpq_add"); procedure Subtract (Rop : in out Big_Rational; Op1, Op2 : in Big_Rational); pragma Import (C, Subtract, "mpq_sub"); procedure Multiply (Rop : in out Big_Rational; Op1, Op2 : in Big_Rational); pragma Import (C, Multiply, "mpq_mul"); procedure Divide (Rop : in out Big_Rational; Op1, Op2 : in Big_Rational); pragma Import (C, Divide, "mpq_div"); procedure Negate (Rop : in out Big_Rational; Op : in Big_Rational); pragma Import (C, Negate, "mpq_neg"); -- Rop <- -Op procedure Inverse (Rop : in out Big_Rational; Op : in Big_Rational); pragma Import (C, Inverse, "mpq_inv"); -- Rop <- 1/Op ------------------------------------------------------------------------------ -- Comparison -- ------------------------------------------------------------------------------ function Compare (Op1, Op2 : Big_Rational) return Interfaces.C.int; pragma Import (C, Compare, "mpq_cmp"); function Compare_Unsigned (Op1 : Big_Rational; Numerator, Denominator : Interfaces.C.unsigned) return Interfaces.C.int; pragma Import (C, Compare_Unsigned, "mpq_cmp_ui"); function Sgn (Op : Big_Rational) return Interfaces.C.int; pragma Import (C, Sgn, "mpq_sgn"); function Equal_Integer_Result (Op1, Op2 : Big_Rational) return Interfaces.C.int; pragma Import (C, Equal_Integer_Result, "mpq_equal"); -- Returns non-zero if Op1 and Op2 and equal, zero if they are not equal -- This routine is faster than Compare -- Note that the result is not an Ada Boolean type - this is why -- Integer_Result is in its name private procedure Read (Stream : access Ada.Streams.Root_Stream_Type'Class; Item : out Big_Rational); for Big_Rational'Read use Read; procedure Write (Stream : access Ada.Streams.Root_Stream_Type'Class; Item : in Big_Rational); for Big_Rational'Write use Write; end GNU_Multiple_Precision.Q;