1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- ADA.NUMERICS.BIG_NUMBERS.BIG_REALS --
9 -- This specification is derived from the Ada Reference Manual for use with --
10 -- GNAT. In accordance with the copyright of that document, you can freely --
11 -- copy and modify this specification, provided that if you redistribute a --
12 -- modified version, any changes that you have made are clearly indicated. --
14 ------------------------------------------------------------------------------
16 with Ada
.Numerics
.Big_Numbers
.Big_Integers
;
18 with Ada
.Strings
.Text_Buffers
; use Ada
.Strings
.Text_Buffers
;
20 package Ada
.Numerics
.Big_Numbers
.Big_Reals
with
25 type Big_Real
is private with
26 Real_Literal
=> From_Universal_Image
,
27 Put_Image
=> Put_Image
;
29 function Is_Valid
(Arg
: Big_Real
) return Boolean
31 Convention
=> Intrinsic
,
34 subtype Valid_Big_Real
is Big_Real
35 with Dynamic_Predicate
=> Is_Valid
(Valid_Big_Real
),
36 Predicate_Failure
=> raise Program_Error
;
39 (Num
, Den
: Big_Integers
.Valid_Big_Integer
) return Valid_Big_Real
41 -- with Pre => (Big_Integers."/=" (Den, Big_Integers.To_Big_Integer (0))
42 -- or else Constraint_Error);
45 (Arg
: Valid_Big_Real
) return Big_Integers
.Valid_Big_Integer
48 function Denominator
(Arg
: Valid_Big_Real
) return Big_Integers
.Big_Positive
52 then Big_Integers
."=" (Denominator
'Result,
53 Big_Integers
.To_Big_Integer
(1))
55 (Big_Integers
.Greatest_Common_Divisor
56 (Numerator
(Arg
), Denominator
'Result),
57 Big_Integers
.To_Big_Integer
(1))),
61 (Arg
: Big_Integers
.Big_Integer
)
62 return Valid_Big_Real
is (Arg
/ Big_Integers
.To_Big_Integer
(1))
65 function To_Real
(Arg
: Integer) return Valid_Big_Real
is
66 (Big_Integers
.To_Big_Integer
(Arg
) / Big_Integers
.To_Big_Integer
(1))
69 function "=" (L
, R
: Valid_Big_Real
) return Boolean with Global
=> null;
71 function "<" (L
, R
: Valid_Big_Real
) return Boolean with Global
=> null;
73 function "<=" (L
, R
: Valid_Big_Real
) return Boolean with Global
=> null;
75 function ">" (L
, R
: Valid_Big_Real
) return Boolean with Global
=> null;
77 function ">=" (L
, R
: Valid_Big_Real
) return Boolean with Global
=> null;
79 function In_Range
(Arg
, Low
, High
: Big_Real
) return Boolean is
80 (Low
<= Arg
and then Arg
<= High
)
84 type Num
is digits <>;
85 package Float_Conversions
is
87 function To_Big_Real
(Arg
: Num
) return Valid_Big_Real
90 function From_Big_Real
(Arg
: Big_Real
) return Num
93 Low
=> To_Big_Real
(Num
'First),
94 High
=> To_Big_Real
(Num
'Last))
95 or else (raise Constraint_Error
),
98 end Float_Conversions
;
101 type Num
is delta <>;
102 package Fixed_Conversions
is
104 function To_Big_Real
(Arg
: Num
) return Valid_Big_Real
107 function From_Big_Real
(Arg
: Big_Real
) return Num
109 Pre
=> In_Range
(Arg
,
110 Low
=> To_Big_Real
(Num
'First),
111 High
=> To_Big_Real
(Num
'Last))
112 or else (raise Constraint_Error
),
115 end Fixed_Conversions
;
117 function To_String
(Arg
: Valid_Big_Real
;
120 Exp
: Field
:= 0) return String
122 Post
=> To_String
'Result'First = 1,
125 function From_String (Arg : String) return Valid_Big_Real
128 function From_Universal_Image (Arg : String) return Valid_Big_Real
130 function From_Universal_Image (Num, Den : String) return Valid_Big_Real is
131 (Big_Integers.From_Universal_Image (Num) /
132 Big_Integers.From_Universal_Image (Den))
135 function To_Quotient_String (Arg : Big_Real) return String is
136 (Big_Integers.To_String (Numerator (Arg)) & " / "
137 & Big_Integers.To_String (Denominator (Arg)))
140 function From_Quotient_String (Arg : String) return Valid_Big_Real
143 procedure Put_Image (S : in out Root_Buffer_Type'Class; V : Big_Real);
145 function "+" (L : Valid_Big_Real) return Valid_Big_Real
148 function "-" (L : Valid_Big_Real) return Valid_Big_Real
151 function "abs" (L : Valid_Big_Real) return Valid_Big_Real
154 function "+" (L, R : Valid_Big_Real) return Valid_Big_Real
157 function "-" (L, R : Valid_Big_Real) return Valid_Big_Real
160 function "*" (L, R : Valid_Big_Real) return Valid_Big_Real
163 function "/" (L, R : Valid_Big_Real) return Valid_Big_Real
166 function "**" (L : Valid_Big_Real; R : Integer) return Valid_Big_Real
169 function Min (L, R : Valid_Big_Real) return Valid_Big_Real
172 function Max (L, R : Valid_Big_Real) return Valid_Big_Real
177 type Big_Real is record
178 Num, Den : Big_Integers.Big_Integer;
181 end Ada.Numerics.Big_Numbers.Big_Reals;