CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

| Download

GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it

Path: gap4r8 / doc / ref / chap19.txt
Views: 418346
1
2
19 Floats
3
4
Starting with version 4.5, GAP has built-in support for floating-point
5
numbers in machine format, and allows package to implement
6
arbitrary-precision floating-point arithmetic in a uniform manner. For now,
7
one such package, Float exists, and is based on the arbitrary-precision
8
routines in mpfr.
9
10
A word of caution: GAP deals primarily with algebraic objects, which can be
11
represented exactly in a computer. Numerical imprecision means that
12
floating-point numbers do not form a ring in the strict GAP sense, because
13
addition is in general not associative ((1.0e-100+1.0)-1.0 is not the same
14
as 1.0e-100+(1.0-1.0), in the default precision setting).
15
16
Most algorithms in GAP which require ring elements will therefore not be
17
applicable to floating-point elements. In some cases, such a notion would
18
not even make any sense (what is the greatest common divisor of two
19
floating-point numbers?)
20
21
22
19.1 A sample run
23
24
Floating-point numbers can be input into GAP in the standard floating-point
25
notation:
26
27
 Example 
28
gap> 3.14;
29
3.14
30
gap> last^2/6;
31
1.64327
32
gap> h := 6.62606896e-34;
33
6.62607e-34
34
gap> pi := 4*Atan(1.0);
35
3.14159
36
gap> hbar := h/(2*pi);
37
1.05457e-34
38

39
40
Floating-point numbers can also be created using Float, from strings or
41
rational numbers; and can be converted back using String,Rat,Int.
42
43
GAP allows rational and floating-point numbers to be mixed in the elementary
44
operations +,-,*,/. However, floating-point numbers and rational numbers may
45
not be compared. Conversions are performed using the creator Float:
46
47
 Example 
48
gap> Float("3.1416");
49
3.1416
50
gap> Float(355/113);
51
3.14159
52
gap> Rat(last);
53
355/113
54
gap> Rat(0.33333);
55
1/3
56
gap> Int(1.e10);
57
10000000000
58
gap> Int(1.e20);
59
100000000000000000000
60
gap> Int(1.e30);
61
1000000000000000019884624838656
62

63
64
65
19.2 Methods
66
67
Floating-point numbers may be directly input, as in any usual mathematical
68
software or language; with the exception that every floating-point number
69
must contain a decimal digit. Therefore .1, .1e1, -.999 etc. are all valid
70
GAP inputs.
71
72
Floating-point numbers so entered in GAP are stored as strings. They are
73
converted to floating-point when they are first used. This means that, if
74
the floating-point precision is increased, the constants are reevaluated to
75
fit the new format.
76
77
Floating-point numbers may be followed by an underscore, as in 1._. This
78
means that they are to be immediately converted to the current
79
floating-point format. The underscore may be followed by a single letter,
80
which specifies which format/precision to use. By default, GAP has a single
81
floating-point handler, with fixed (53 bits) precision, and its format
82
specifier is 'l' as in 1._l. Higher-precision floating-point computations is
83
available via external packages; float for example.
84
85
A record, FLOAT (19.2-6), contains all relevant constants for the current
86
floating-point format; see its documentation for details. Typical fields are
87
FLOAT.MANT_DIG=53, the constant FLOAT.VIEW_DIG=6 specifying the number of
88
digits to view, and FLOAT.PI for the constant π. The constants have the same
89
name as their C counterparts, except for the missing initial DBL_ or M_.
90
91
Floating-point numbers may be created using the single function Float
92
(19.2-7), which accepts as arguments rational, string, or floating-point
93
numbers. Floating-point numbers may also be created, in any floating-point
94
representation, using NewFloat (19.2-7) as in
95
NewFloat(IsIEEE754FloatRep,355/113), by supplying the category filter of the
96
desired new floating-point number; or using MakeFloat (19.2-7) as in
97
NewFloat(1.0,355/113), by supplying a sample floating-point number.
98
99
Floating-point numbers may also be converted to other GAP formats using the
100
usual commands Int (14.2-3), Rat (17.2-6), String (27.7-6).
101
102
Exact conversion to and from floating-point format may be done using
103
external representations. The "external representation" of a floating-point
104
number x is a pair [m,e] of integers, such that
105
x=m*2^(-1+e-LogInt(AbsInt(m),2)). Conversion to and from external
106
representation is performed as usual using ExtRepOfObj (79.16-1) and
107
ObjByExtRep (79.16-1):
108
109
 Example 
110
gap> ExtRepOfObj(3.14);
111
[ 7070651414971679, 2 ]
112
gap> ObjByExtRep(IEEE754FloatsFamily,last);
113
3.14
114

115
116
Computations with floating-point numbers never raise any error. Division by
117
zero is allowed, and produces a signed infinity. Illegal operations, such as
118
0./0., produce NaN's (not-a-number); this is the only floating-point number
119
x such that not EqFloat(x+0.0,x).
120
121
The IEEE754 standard requires NaN to be non-equal to itself. On the other
122
hand, GAP requires every object to be equal to itself. To respect the
123
IEEE754 standard, the function EqFloat (19.2-2) should be used instead of =.
124
125
The category a floating-point belongs to can be checked using the filters
126
IsFinite (30.4-2), IsPInfinity (19.2-5), IsNInfinity (19.2-5), IsXInfinity
127
(19.2-5), IsNaN (19.2-5).
128
129
Comparisons between floating-point numbers and rationals are explicitly
130
forbidden. The rationale is that objects belonging to different families
131
should in general not be comparable in GAP. Floating-point numbers are also
132
approximations of real numbers, and don't follow the same rules; consider
133
for example, using the default GAP implementation of floating-point numbers,
134
135
 Example 
136
gap> 1.0/3.0 = Float(1/3);
137
true
138
gap> (1.0/3.0)^5 = Float((1/3)^5);
139
false
140

141
142
143
19.2-1 Mathematical operations
144
145
Cos( x )  attribute
146
Sin( x )  attribute
147
SinCos( x )  attribute
148
Tan( x )  attribute
149
Sec( x )  attribute
150
Csc( x )  attribute
151
Cot( x )  attribute
152
Asin( x )  attribute
153
Acos( x )  attribute
154
Atan( x )  attribute
155
Atan2( y, x )  operation
156
Cosh( x )  attribute
157
Sinh( x )  attribute
158
Tanh( x )  attribute
159
Sech( x )  attribute
160
Csch( x )  attribute
161
Coth( x )  attribute
162
Asinh( x )  attribute
163
Acosh( x )  attribute
164
Atanh( x )  attribute
165
Log( x )  operation
166
Log2( x )  attribute
167
Log10( x )  attribute
168
Log1p( x )  attribute
169
Exp( x )  attribute
170
Exp2( x )  attribute
171
Exp10( x )  attribute
172
Expm1( x )  attribute
173
CubeRoot( x )  attribute
174
Square( x )  attribute
175
Hypothenuse( x, y )  operation
176
Ceil( x )  attribute
177
Floor( x )  attribute
178
Round( x )  attribute
179
Trunc( x )  attribute
180
Frac( x )  attribute
181
SignFloat( x )  attribute
182
Argument( x )  attribute
183
Erf( x )  attribute
184
Zeta( x )  attribute
185
Gamma( x )  attribute
186
ComplexI( x )  attribute
187
188
Usual mathematical functions.
189
190
19.2-2 EqFloat
191
192
EqFloat( x, y )  operation
193
Returns: Whether the floateans x and y are equal
194
195
This function compares two floating-point numbers, and returns true if they
196
are equal, and false otherwise; with the exception that NaN is always
197
considered to be different from itself.
198
199
19.2-3 PrecisionFloat
200
201
PrecisionFloat( x )  attribute
202
Returns: The precision of x
203
204
This function returns the precision, counted in number of binary digits, of
205
the floating-point number x.
206
207
208
19.2-4 Interval operations
209
210
Sup( interval )  attribute
211
Inf( interval )  attribute
212
Mid( interval )  attribute
213
AbsoluteDiameter( interval )  attribute
214
RelativeDiameter( interval )  attribute
215
Overlaps( interval1, interval2 )  operation
216
IsDisjoint( interval1, interval2 )  operation
217
IncreaseInterval( interval, delta )  operation
218
BlowupInterval( interval, ratio )  operation
219
BisectInterval( interval )  operation
220
221
Most are self-explanatory. BlowupInterval returns an interval with same
222
midpoint but relative diameter increased by ratio; IncreaseInterval returns
223
an interval with same midpoint but absolute diameter increased by delta;
224
BisectInterval returns a list of two intervals whose union equals interval.
225
226
19.2-5 IsPInfinity
227
228
IsPInfinity( x )  property
229
IsNInfinity( x )  property
230
IsXInfinity( x )  property
231
IsFinite( x )  property
232
IsNaN( x )  property
233
234
Returns true if the floating-point number x is respectively +∞, -∞, ±∞,
235
finite, or `not a number', such as the result of 0.0/0.0.
236
237
19.2-6 FLOAT
238
239
FLOAT global variable
240
241
This record contains useful floating-point constants:
242
243
DECIMAL_DIG
244
Maximal number of useful digits;
245
246
DIG
247
Number of significant digits;
248
249
VIEW_DIG
250
Number of digits to print in short view;
251
252
EPSILON
253
Smallest number such that 1≠1+ϵ;
254
255
MANT_DIG
256
Number of bits in the mantissa;
257
258
MAX
259
Maximal representable number;
260
261
MAX_10_EXP
262
Maximal decimal exponent;
263
264
MAX_EXP
265
Maximal binary exponent;
266
267
MIN
268
Minimal positive representable number;
269
270
MIN_10_EXP
271
Minimal decimal exponent;
272
273
MIN_EXP
274
Minimal exponent;
275
276
INFINITY
277
Positive infinity;
278
279
NINFINITY
280
Negative infinity;
281
282
NAN
283
Not-a-number,
284
285
as well as mathematical constants E, LOG2E, LOG10E, LN2, LN10, PI, PI_2,
286
PI_4, 1_PI, 2_PI, 2_SQRTPI, SQRT2, SQRT1_2.
287
288
19.2-7 Float
289
290
Float( obj )  function
291
NewFloat( filter, obj )  operation
292
MakeFloat( sample, obj, obj )  operation
293
Returns: A new floating-point number, based on obj
294
295
This function creates a new floating-point number.
296
297
If obj is a rational number, the created number is created with sufficient
298
precision so that the number can (usually) be converted back to the original
299
number (see Rat (Reference: Rat) and Rat (17.2-6)). For an integer, the
300
precision, if unspecified, is chosen sufficient so that Int(Float(obj))=obj
301
always holds, but at least 64 bits.
302
303
obj may also be a string, which may be of the form "3.14e0" or ".314e1" or
304
".314@1" etc.
305
306
An option may be passed to specify, it bits, a desired precision. The format
307
is Float("3.14":PrecisionFloat:=1000) to create a 1000-bit approximation of
308
3.14.
309
310
In particular, if obj is already a floating-point number, then
311
Float(obj:PrecisionFloat:=prec) creates a copy of obj with a new precision.
312
prec
313
314
19.2-8 Rat
315
316
Rat( f )  attribute
317
Returns: A rational approximation to f
318
319
This command constructs a rational approximation to the floating-point
320
number f. Of course, it is not guaranteed to return the original rational
321
number f was created from, though it returns the most `reasonable' one given
322
the precision of f.
323
324
Two options control the precision of the rational approximation: In the form
325
Rat(f:maxdenom:=md,maxpartial:=mp), the rational returned is such that the
326
denominator is at most md and the partials in its continued fraction
327
expansion are at most mp. The default values are maxpartial:=10000 and
328
maxdenom:=2^(precision/2).
329
330
19.2-9 SetFloats
331
332
SetFloats( rec[, bits][, install] )  function
333
334
Installs a new interface to floating-point numbers in GAP, optionally with a
335
desired precision bits in binary digits. The last optional argument install
336
is a boolean value; if false, it only installs the eager handler and the
337
precision for the floateans, without making them the default.
338
339
340
19.3 High-precision-specific methods
341
342
GAP provides a mechanism for packages to implement new floating-point
343
numerical interfaces. The following describes that mechanism, actual
344
examples of packages are documented separately.
345
346
A package must create a record with fields (all optional)
347
348
creator
349
a function converting strings to floating-point;
350
351
eager
352
a character allowing immediate conversion to floating-point;
353
354
objbyextrep
355
a function creating a floating-point number out of a list
356
[mantissa,exponent];
357
358
filter
359
a filter for the new floating-point objects;
360
361
constants
362
a record containing numerical constants, such as MANT_DIG, MAX, MIN,
363
NAN.
364
365
The package must install methods Int, Rat, String for its objects, and
366
creators NewFloat(filter,IsRat), NewFloat(IsString).
367
368
It must then install methods for all arithmetic and numerical operations:
369
PLUS, Exp, ...
370
371
The user chooses that implementation by calling SetFloats (19.2-9) with the
372
record as argument, and with an optional second argument requesting a
373
precision in binary digits.
374
375
376
19.4 Complex arithmetic
377
378
Complex arithmetic may be implemented in packages, and is present in float.
379
Complex numbers are treated as usual numbers; they may be input with an
380
extra "i" as in -0.5+0.866i.
381
382
Methods should then be implemented for Norm, RealPart, ImaginaryPart,
383
ComplexConjugate, ...
384
385
386
19.5 Interval-specific methods
387
388
Interval arithmetic may also be implemented in packages. Intervals are in
389
fact efficient implementations of sets of real numbers. The only non-trivial
390
issue is how they should be compared. The standard EQ tests if the intervals
391
are equal; however, it is usually more useful to know if intervals overlap,
392
or are disjoint, or are contained in each other. The methods provided by the
393
package should include
394
Sup,Inf,Mid,DiameterOfInterval,Overlaps,IsSubset,IsDisjoint.
395
396
Note the usual convention that intervals are compared as in [a,b]le[c,d] if
397
and only if ale c and ble d.
398
399
400