File: float.tst

package info (click to toggle)
gap 4r10p0-7
  • links: PTS
  • area: main
  • in suites: buster
  • size: 47,392 kB
  • sloc: ansic: 118,475; xml: 54,089; sh: 4,112; perl: 1,654; makefile: 274
file content (414 lines) | stat: -rw-r--r-- 7,439 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#############################################################################
##
#W  float.tst                   GAP Tests                         Stefan Kohl
##
##
gap> START_TEST("float.tst");

# make sure we are testing the built-in machine floats
gap> SetFloats(IEEE754FLOAT);

# some special values we will use again later on
gap> posinf := 1.0/0.0;
inf
gap> neginf := -1.0/0.0;
-inf
gap> nan := 0.0/0.0;
nan

#
# Convert things to floats
#
gap> Float(3);
3.
gap> Float(-4);
-4.
gap> Float(2/3);
0.666667
gap> Float("-4");
-4.
gap> Float("4.1");
4.1
gap> Float("4.1e-1");
0.41
gap> Float(infinity);
inf
gap> Float(-infinity);
-inf

#
# input floats directly
#
gap> 0.6;
0.6
gap> -0.7;
-0.7

#
# some arithmetic
#
gap> 355.0/113.0;
3.14159
gap> last = 355.0/113;
false
gap> 355.0/113.0-355/113;
0.
gap> 355.0/113.0 = 355.0/113;
false
gap> 355.0/113.0 < 355.0/113;
false
gap> 355.0/113.0 > 355.0/113;
true
gap> 355.0/113;
3.14159
gap> 355.0/113.0 - 355.0/113;
4.44089e-16
gap> 355/113.0 = 355.0/113.0;
false
gap> 355/113.0 - 355.0/113.0;
-4.44089e-16
gap> 355/113.0 - 355.0/113;
0.
gap> 355/113.0 = 355.0/113;
true

#
gap> 2.0^2;
4.
gap> 2.0^-2;
0.25
gap> 2.0^2.;
4.
gap> 2.0^-2.;
0.25

#
gap> LeftQuotient(1.0, 2.0);
2.
gap> LeftQuotient(2.0, 1.0);
0.5

#
# convert floats to ints
#
gap> Int(1.0);
1
gap> Int(1.5);
1
gap> Int(-1.0);
-1
gap> Int(-1.5);
-1
gap> Int(1.e22);
10000000000000000000000
gap> Int(-1.e22);
-10000000000000000000000

#
# convert floats to rationals
#
gap> Rat(355.0/113.0);
355/113
gap> Rat(0.5);
1/2
gap> Rat(0.0);
0

#
# Print / View / Display for floats
#
gap> l := [ 0.0, -0.0, 1.0, Sqrt(2.0), posinf, neginf, nan ];
[ 0., -0., 1., 1.41421, inf, -inf, nan ]
gap> ViewObj(l); Print("\n");
[ 0., -0., 1., 1.41421, inf, -inf, nan ]
gap> PrintObj(l); Print("\n");
[ 0, -0, 1, 1.414213562373095, inf, -inf, nan ]
gap> Display(l);
[ 0, -0, 1, 1.414213562373095, inf, -inf, nan ]

#
#
#
gap> Sqrt(2.0);
1.41421
gap> MinimalPolynomial(Rationals,last);
-2*x_1^2+1
gap> r:=Rat("2.7182818");; r:=Rat(Float(String(NumeratorRat(r)))/Float(String(DenominatorRat(r))));
2683788193/987310511
gap> Float(String(NumeratorRat(r)))/Float(String(DenominatorRat(r)));
2.71828
gap> AbsoluteValue(Float("1")/Float("2"));
0.5
gap> AbsoluteValue(Float("-1")/Float("2"));
0.5
gap> AbsoluteValue(-Float("1")/Float("2"));
0.5
gap> AbsoluteValue(-Float("0"));
0.
gap> Float(List([1..100],n->1/Factorial(n)));
[ 1., 0.5, 0.166667, 0.0416667, 0.00833333, 0.00138889, 0.000198413, 
  2.48016e-05, 2.75573e-06, 2.75573e-07, 2.50521e-08, 2.08768e-09, 
  1.6059e-10, 1.14707e-11, 7.64716e-13, 4.77948e-14, 2.81146e-15, 
  1.56192e-16, 8.22064e-18, 4.11032e-19, 1.95729e-20, 8.89679e-22, 
  3.86817e-23, 1.61174e-24, 6.44695e-26, 2.4796e-27, 9.18369e-29, 
  3.27989e-30, 1.131e-31, 3.76999e-33, 1.21613e-34, 3.80039e-36, 1.15163e-37, 
  3.38716e-39, 9.67759e-41, 2.68822e-42, 7.26546e-44, 1.91196e-45, 
  4.90247e-47, 1.22562e-48, 2.98931e-50, 7.11741e-52, 1.65521e-53, 
  3.76184e-55, 8.35965e-57, 1.81732e-58, 3.86663e-60, 8.05548e-62, 
  1.64397e-63, 3.28795e-65, 6.44696e-67, 1.2398e-68, 2.33925e-70, 
  4.33194e-72, 7.87625e-74, 1.40647e-75, 2.4675e-77, 4.2543e-79, 7.21068e-81, 
  1.20178e-82, 1.97013e-84, 3.17763e-86, 5.04386e-88, 7.88103e-90, 
  1.21247e-91, 1.83707e-93, 2.7419e-95, 4.0322e-97, 5.84377e-99, 
  8.34824e-101, 1.17581e-102, 1.63307e-104, 2.23708e-106, 3.02308e-108, 
  4.03077e-110, 5.30365e-112, 6.88785e-114, 8.83058e-116, 1.1178e-117, 
  1.39724e-119, 1.72499e-121, 2.10365e-123, 2.53452e-125, 3.01728e-127, 
  3.54974e-129, 4.12761e-131, 4.74438e-133, 5.39134e-135, 6.05769e-137, 
  6.73076e-139, 7.39644e-141, 8.03961e-143, 8.64474e-145, 9.19653e-147, 
  9.68056e-149, 1.00839e-150, 1.03958e-152, 1.0608e-154, 1.07151e-156, 
  1.07151e-158 ]
gap> 1.5e10;
1.5e+10
gap> -1.5e0;
-1.5
gap> 0.7e-10;
7.e-11
gap> -0.8e-0;
-0.8
gap> 1000000000000000000000000000000000000000000000000000000000000000\
> 00000000000000000000000000000000000000000000000000000000000000.0;
1.e+125
gap> 1.5+1;
2.5
gap> last-1.6;
0.9
gap> last*2;
1.8
gap> last/2.0;
0.9
gap> Sqrt(last);
0.948683
gap> Log(last);
-0.0526803
gap> Exp(last);
0.948683
gap> last^2;
0.9

#
# some tests with infinity
#
gap> 1.0/0.0;
inf
gap> -1.0/0.0;
-inf
gap> List([posinf, neginf, nan, 0.0, 1.0], IsPInfinity);
[ true, false, false, false, false ]
gap> List([posinf, neginf, nan, 0.0, 1.0], IsNInfinity);
[ false, true, false, false, false ]
gap> -posinf = neginf;
true
gap> posinf = -neginf;
true
gap> neginf < posinf;
true
gap> neginf <> posinf;
true
gap> neginf < 0.0;
true
gap> 0.0 < posinf;
true
gap> MakeFloat(1.0, infinity) = posinf;
true
gap> -MakeFloat(1.0, infinity) = neginf;
true
gap> MakeFloat(1.0, -infinity) = neginf;
true

#
# test sign handling
#
gap> SignBit(posinf);
false
gap> SignFloat(posinf);
1
gap> SignBit(neginf);
true
gap> SignFloat(neginf);
-1
gap> SignBit(+0.0);
false
gap> SignFloat(+0.0);
0
gap> SignBit(-0.0);
true
gap> SignFloat(-0.0);
0
gap> SignBit(42.0);
false
gap> SignFloat(42.0);
1
gap> SignBit(-42.0);
true
gap> SignFloat(-42.0);
-1

# sign of NaN is machine specific; but we can still test whether
# SignBit and SignFloat return consistent results
gap> SignBit(nan) = (SignFloat(nan) = -1);
true
gap> SignBit(-nan) = (SignFloat(-nan) = -1);
true

#
# test float comparison
#

#
gap> EqFloat(1.0, 1.1);
false
gap> EqFloat(1.0, 1.0);
true
gap> EqFloat(0.0/0.0,0.0/0.0);
false
gap> EqFloat(0.0,0.0/0.0);
false

#
# float literals in the REPL
#
gap> 1.1;
1.1
gap> 1.1_;
1.1
gap> 1.x1;
Syntax error: Badly formed number in stream:1
1.x1;
^^^
gap> 1.1xx;
Syntax error: Badly formed number in stream:1
1.1xx;
^^^^

# The following is potentially correct, *if* there is a conversion handler for
# 'x' installed, which normally isn't the case.
gap> 1.1x;
Error, failed to convert float literal

#
# float literal expressions in functions
#

# eager literal
gap> f := {} -> 0.0_;; f();
0.
gap> f := {} -> 1.0_;; f();
1.
gap> f := {} -> 42.0_;; f();
42.
gap> Display(f);
function (  )
    return 42.0_;
end

# lazy literal
gap> g := {} -> 0.0;; g();
0.
gap> g := {} -> 1.0;; g();
1.
gap> g := {} -> 23.0;; g();
23.
gap> Display(g);
function (  )
    return 23.0;
end

#
#
#
gap> Cos(0.);
1.
gap> Sin(0.);
0.
gap> Tan(0.);
0.
gap> Acos(1.);
0.
gap> Asin(0.);
0.
gap> Log(1.);
0.
gap> Exp(0.);
1.
gap> if IsBound(Log2) then Assert(0, Log2(1.) = 0.); fi;
gap> if IsBound(Log10) then Assert(0, Log10(1.) = 0.); fi;
gap> if IsBound(Log1p) then Assert(0, Log1p(0.) = 0.); fi;
gap> if IsBound(Exp2) then Assert(0, Exp2(0.) = 1.); fi;
gap> if IsBound(Exp10) then Assert(0, Exp10(0.) = 1.); fi;
gap> if IsBound(Expm1) then Assert(0, Expm1(0.) = 0.); fi;

#
gap> Round(1.3);
1.
gap> Round(1.9);
2.
gap> Round(-1.9);
-2.
gap> Round(-1.3);
-1.

#
gap> Floor(1.3);
1.
gap> Floor(1.9);
1.
gap> Floor(-1.9);
-2.
gap> Floor(-1.3);
-2.

#
gap> Ceil(1.3);
2.
gap> Ceil(1.9);
2.
gap> Ceil(-1.9);
-1.
gap> Ceil(-1.3);
-1.

#
gap> AbsoluteValue(1.3);
1.3
gap> AbsoluteValue(1.9);
1.9
gap> AbsoluteValue(-1.9);
1.9
gap> AbsoluteValue(-1.3);
1.3

#
gap> Atan2(0.,0.);
0.
gap> Hypothenuse(3.,4.);
5.

#
gap> ComplexConjugate(1.3);
1.3

#
gap> Display(1.3);
1.3
gap> Display(-.4e6);
-400000.
gap> PrintObj(1.3); Print("Q\n");
1.3Q
gap> DisplayString(1.3);
"1.3\n"

#
gap> STOP_TEST( "float.tst", 1);

#############################################################################
##
#E  float.tst . . . . . . . . . . . . . . . . . . . . . . . . . . . ends here