COVERAGE SUMMARY
FILE SUMMARY
NameExecutedRoutines%ExecutedLines%Unexecuted
/home/matt/eu/rds/include/std/math.e3838100.00%26628693.01%20
ROUTINE SUMMARY
RoutineExecutedLinesUnexecuted
round()263868.42%12
atan2()101283.33%2
abs_below_1()7887.50%1
approx()222395.65%1
ensure_in_list()7887.50%1
gcd()212295.45%1
not_below_1()7887.50%1
trig_range()7887.50%1
abs()1313100.00%0
arccos()22100.00%0
arccosh()22100.00%0
arcsin()22100.00%0
arcsinh()22100.00%0
arctanh()22100.00%0
ceil()22100.00%0
cosh()22100.00%0
deg2rad()22100.00%0
ensure_in_range()88100.00%0
exp()22100.00%0
frac()33100.00%0
intdiv()22100.00%0
is_even()22100.00%0
is_even_obj()99100.00%0
log10()22100.00%0
max()1010100.00%0
min()1010100.00%0
mod()44100.00%0
or_all()1010100.00%0
powof2()22100.00%0
product()1010100.00%0
rad2deg()22100.00%0
rotate_bits()1717100.00%0
shift_bits()1919100.00%0
sign()22100.00%0
sinh()22100.00%0
sum()1010100.00%0
tanh()22100.00%0
trunc()22100.00%0
LINE COVERAGE DETAIL
#Executed
1
-- (c) Copyright - See License.txt
2
--
3
--****
4
-- == Math
5
--
6
-- <>
7
--
8
namespace math
9
10
public include std/rand.e
11
public include std/mathcons.e
12
include std/error.e
13
146
15
-- values passed to arccos and arcsin must be [-1,+1]
166
if atom(x) then
174
return x >= -1 and x <= 1
18
else
192
for i = 1 to length(x) do
203
if not trig_range(x[i]) then
210
return 0
22
end if
233
end for
242
return 1
25
end if
26
end type
27
28
29
--****
30
-- === Sign and comparisons
31
--
32
33
--**
34
-- Returns the absolute value of numbers.
35
--
36
-- Parameters:
37
-- # ##value## : an object, each atom is processed, no matter how deeply nested.
38
--
39
-- Returns:
40
-- An **object**, the same shape as ##value##. When ##value## is an atom,
41
-- the result is the same if not less than zero, and the opposite value otherwise.
42
--
43
-- Comments:
44
-- This function may be applied to an atom or to all elements of a sequence
45
--
46
-- Example 1:
47
--
48
-- x = abs({10.5, -12, 3})
49
-- -- x is {10.5, 12, 3}
50
--
51
-- i = abs(-4)
52
-- -- i is 4
53
--
54
--
55
-- See Also:
56
-- [[:sign]]
57
58140
59
object t
60140
if atom(a) then
61115
if a >= 0 then
6280
return a
63
else
6435
return - a
65
end if
66
end if
6725
for i = 1 to length(a) do
6878
t = a[i]
6978
if atom(t) then
7071
if t < 0 then
7121
a[i] = - t
72
end if
73
else
747
a[i] = abs(t)
75
end if
7678
end for
7725
return a
78
end function
79
80
--**
81
-- Return -1, 0 or 1 for each element according to it being negative, zero or positive
82
--
83
-- Parameters:
84
-- # ##value## : an object, each atom of which will be acted upon, no matter how deeply nested.
85
--
86
-- Returns:
87
-- An **object**, the same shape as ##value##. When ##value## is an atom, the result is -1 if ##value## is less than zero, 1 if greater and 0 if equal.
88
--
89
-- Comments:
90
-- This function may be applied to an atom or to all elements of a sequence.
91
--
92
-- For an atom, ##sign(x)## is the same as [[:compare]](x,0).
93
--
94
-- Example 1:
95
--
96
-- i = sign(5)
97
-- i is 1
98
--
99
-- i = sign(0)
100
-- -- i is 0
101
--
102
-- i = sign(-2)
103
-- -- i is -1
104
--
105
--
106
-- See Also:
107
-- [[:compare]]
108
10953
110
-- small so normally it will be inlined
11153
return (a > 0) - (a < 0)
112
end function
113
114
115
--**
116
-- Computes the maximum value among all the argument's elements
117
--
118
-- Parameters:
119
-- # ##values## : an object, all atoms of which will be inspected, no matter how deeply nested.
120
--
121
-- Returns:
122
-- An **atom**, the maximum of all atoms in [[:flatten]](##values##).
123
--
124
-- Comments:
125
-- This function may be applied to an atom or to a sequence of any shape.
126
--
127
-- Example 1:
128
--
129
-- a = max({10,15.4,3})
130
-- -- a is 15.4
131
--
132
--
133
-- See Also:
134
-- [[:min]], [[:compare]], [[:flatten]]
135
136259
137
atom b, c
138259
if atom(a) then
139170
return a
140
end if
14189
b = MINF
14289
for i = 1 to length(a) do
143189
c = max(a[i])
144189
if c > b then
145137
b = c
146
end if
147189
end for
14889
return b
149
end function
150
151
--**
152
-- Computes the minimum value among all the argument's elements
153
--
154
-- Parameters:
155
-- # ##values## : an object, all atoms of which will be inspected, no matter how deeply nested.
156
--
157
-- Returns:
158
-- An **atom**, the minimum of all atoms in [[:flatten]](##values##).
159
--
160
-- Comments:
161
-- This function may be applied to an atom or to a sequence of any shape.
162
--
163
-- Example 1:
164
--
165
-- a = min({10,15.4,3})
166
-- -- a is 3
167
--
168
16979
170
atom b, c
17179
if atom(a) then
17254
return a
173
end if
17425
b = PINF
17525
for i = 1 to length(a) do
17653
c = min(a[i])
17753
if c < b then
17850
b = c
179
end if
18053
end for
18125
return b
182
end function
183
184
--**
185
-- Ensures that the ##item## is in a range of values supplied by inclusive ##range_limits##
186
--
187
-- Parameters:
188
-- # ##item## : The object to test for.
189
-- # ##range_limits## : A sequence of two or more elements. The first is assumed
190
-- to be the smallest value and the last is assumed to be the highest value.
191
--
192
-- Returns:
193
-- A **object**, If ##item## is lower than the first item in the ##range_limits##
194
-- it returns the first item.
195
-- If ##item## is higher than the last element in the ##range_limits##
196
-- it returns the last item.
197
-- Otherwise it returns ##item##.
198
--
199
-- Example 1:
200
--
201
-- object valid_data = ensure_in_range(user_data, {2, 75})
202
-- if not equal(valid_data, user_data) then
203
-- errmsg("Invalid input supplied. Using %d instead.", valid_data)
204
-- end if
205
-- procA(valid_data)
206
--
207
2087
2097
if length(range_limits) < 2 then
2102
return item
211
end if
212
2135
if eu:compare(item, range_limits[1]) < 0 then
2141
return range_limits[1]
215
end if
2164
if eu:compare(item, range_limits[$]) > 0 then
2171
return range_limits[$]
218
end if
2193
return item
220
end function
221
222
--**
223
-- Ensures that the ##item## is in a list of values supplied by ##list##
224
--
225
-- Parameters:
226
-- # ##item## : The object to test for.
227
-- # ##list## : A sequence of elements that ##item## should be a member of.
228
-- # ##default## : an integer, the index of the list item to return if ##item## is not found. Defaults to 1.
229
--
230
-- Returns:
231
-- An **object**, if ##item## is not in the list, it returns the list item of index ##default##,
232
-- otherwise it returns ##item##.
233
--
234
-- Comments:
235
--
236
-- If ##default## is set to an invalid index, the first item on the list is returned instead
237
-- when ##item## is not on the list.
238
--
239
-- Example 1:
240
--
241
-- object valid_data = ensure_in_list(user_data, {100, 45, 2, 75, 121})
242
-- if not equal(valid_data, user_data) then
243
-- errmsg("Invalid input supplied. Using %d instead.", valid_data)
244
-- end if
245
-- procA(valid_data)
246
--
247
2486
2496
if length(list) = 0 then
2501
return item
251
end if
2525
if find(item, list) = 0 then
2531
if default>=1 and default<=length(list) then
2541
return list[default]
255
else
2560
return list[1]
257
end if
258
end if
2594
return item
260
end function
261
262
--****
263
-- === Roundings and remainders
264
--
265
266
--****
267
-- Signature:
268
-- function remainder(object dividend, object divisor)
269
--
270
-- Description:
271
-- Compute the remainder of the division of two objects using truncated division.
272
--
273
-- Parameters:
274
-- # ##dividend## : any Euphoria object.
275
-- # ##divisor## : any Euphoria object.
276
--
277
-- Returns:
278
-- An **object**, the shape of which depends on ##dividend##'s and
279
-- ##divisor##'s. For two atoms, this is the remainder of dividing
280
-- ##dividend## by ##divisor##, with ##dividend##'s sign.
281
--
282
-- Errors:
283
-- # If any atom in ##divisor## is 0, this is an error condition as it
284
-- amounts to an attempt to divide by zero.
285
-- # If both ##dividend## and ##divisor## are sequences, they must be the
286
-- same length as each other.
287
--
288
-- Comments:
289
-- * There is a integer ##N## such that ##dividend## = ##N## * ##divisor## + result.
290
-- * The result has the sign of ##dividend## and lesser magnitude than ##divisor##.
291
-- * The result has the same sign as the dividend.
292
-- * This differs from [[:mod]]() in that when the operands' signs are different
293
-- this function rounds ##dividend/divisior## towards zero whereas mod() rounds
294
-- away from zero.
295
--
296
-- The arguments to this function may be atoms or sequences. The rules for
297
-- [[:operations on sequences]] apply, and determine the shape of the returned object.
298
--
299
-- Example 1:
300
--
301
-- a = remainder(9, 4)
302
-- -- a is 1
303
--
304
--
305
-- Example 2:
306
--
307
-- s = remainder({81, -3.5, -9, 5.5}, {8, -1.7, 2, -4})
308
-- -- s is {1, -0.1, -1, 1.5}
309
--
310
--
311
-- Example 3:
312
--
313
-- s = remainder({17, 12, 34}, 16)
314
-- -- s is {1, 12, 2}
315
--
316
--
317
-- Example 4:
318
--
319
-- s = remainder(16, {2, 3, 5})
320
-- -- s is {0, 1, 1}
321
--
322
-- See Also:
323
-- [[:mod]], [[:Relational operators]], [[:Operations on sequences]]
324
325
326
--**
327
-- Compute the remainder of the division of two objects using floored division.
328
--
329
-- Parameters:
330
-- # ##dividend## : any Euphoria object.
331
-- # ##divisor## : any Euphoria object.
332
--
333
-- Returns:
334
-- An **object**, the shape of which depends on ##dividend##'s and
335
-- ##divisor##'s. For two atoms, this is the remainder of dividing ##dividend##
336
-- by ##divisor##, with ##divisor##'s sign.
337
--
338
-- Comments:
339
-- * There is a integer ##N## such that ##dividend## = N * ##divisor## + result.
340
-- * The result is non-negative and has lesser magnitude than ##divisor##.
341
-- n needs not fit in an Euphoria integer.
342
-- * The result has the same sign as the dividend.
343
-- * The arguments to this function may be atoms or sequences. The rules for
344
-- [[:operations on sequences]] apply, and determine the shape of the returned object.
345
-- * When both arguments have the same sign, mod() and [[:remainder]]()
346
-- return the same result.
347
-- * This differs from [[:remainder]]() in that when the operands' signs are different
348
-- this function rounds ##dividend/divisior## away from zero whereas remainder() rounds
349
-- towards zero.
350
--
351
-- Example 1:
352
--
353
-- a = mod(9, 4)
354
-- -- a is 1
355
--
356
--
357
-- Example 2:
358
--
359
-- s = mod({81, -3.5, -9, 5.5}, {8, -1.7, 2, -4})
360
-- -- s is {1,-0.1,1,-2.5}
361
--
362
--
363
-- Example 3:
364
--
365
-- s = mod({17, 12, 34}, 16)
366
-- -- s is {1, 12, 2}
367
--
368
--
369
-- Example 4:
370
--
371
-- s = mod(16, {2, 3, 5})
372
-- -- s is {0, 1, 1}
373
--
374
-- See Also:
375
-- [[:remainder]], [[:Relational operators]], [[:Operations on sequences]]
376
37710
37810
if equal(sign(x), sign(y)) then
3795
return remainder(x,y)
380
end if
3815
return x - y * floor(x / y)
382
end function
383
384
--**
385
-- Return the integer portion of a number.
386
--
387
-- Parameters:
388
-- # ##value## : any Euphoria object.
389
--
390
-- Returns:
391
-- An **object**, the shape of which depends on ##values##'s. Each item in the
392
-- returned object will be an integer. These are the same corresponding items
393
-- in ##value## except with any fractional portion removed.
394
--
395
-- Comments:
396
-- * This is essentially done by always rounding towards zero. The [[:floor]]() function
397
-- rounds towards negative infinity, which means it rounds towards zero for positive
398
-- values and away from zero for negative values.
399
-- * Note that ##trunc(x) + frac(x) = x##
400
--
401
-- Example 1:
402
--
403
-- a = trunc(9.4)
404
-- -- a is 9
405
--
406
--
407
-- Example 2:
408
--
409
-- s = trunc({81, -3.5, -9.999, 5.5})
410
-- -- s is {81,-3, -9, 5}
411
--
412
-- See Also:
413
-- [[:floor]] [[:frac]]
4143
4153
return sign(x) * floor(abs(x))
416
end function
417
418
419
--**
420
-- Return the fractional portion of a number.
421
--
422
-- Parameters:
423
-- # ##value## : any Euphoria object.
424
--
425
-- Returns:
426
-- An **object**, the shape of which depends on ##values##'s. Each item in the
427
-- returned object will be the same corresponding items
428
-- in ##value## except with the integer portion removed.
429
--
430
-- Comments:
431
-- Note that ##trunc(x) + frac(x) = x##
432
--
433
-- Example 1:
434
--
435
-- a = frac(9.4)
436
-- -- a is 0.4
437
--
438
--
439
-- Example 2:
440
--
441
-- s = frac({81, -3.5, -9.999, 5.5})
442
-- -- s is {0, -0.5, -0.999, 0.5}
443
--
444
--
445
-- See Also:
446
-- [[:trunc]]
447
4483
4493
object temp = abs(x)
4503
return sign(x) * (temp - floor(temp))
451
end function
452
453
454
--**
455
-- Return an integral division of two objects.
456
--
457
-- Parameters:
458
-- # ##divided## : any Euphoria object.
459
-- # ##divisor## : any Euphoria object.
460
--
461
-- Returns:
462
-- An **object**, which will be a sequence if either ##dividend## or ##divisor##
463
-- is a sequence.
464
--
465
-- Comments:
466
-- * This calculates how many non-empty sets when ##dividend## is divided by ##divisor##.
467
-- * The result's sign is the same as the ##dividend##'s sign.
468
--
469
-- Example 1:
470
--
471
-- object Tokens = 101
472
-- object MaxPerEnvelope = 5
473
-- integer Envelopes = intdiv( Tokens, MaxPerEnvelope) --> 21
474
--
475
--
476
4778
4788
return sign(a)*ceil(abs(a)/abs(b))
479
end function
480
481
--****
482
-- Signature:
483
-- function floor(object value)
484
--
485
-- Description:
486
-- Rounds ##value## down to the next integer less than or equal to ##value##. It
487
-- does not simply truncate the fractional part, but actually rounds towards
488
-- negative infinity.
489
--
490
-- Parameters:
491
-- # ##value## : any Euphoria object; each atom in ##value## will be acted upon.
492
--
493
-- Returns:
494
-- An **object**, the same shape as ##value## but with each item guarenteed to be
495
-- an integer less than or equal to the corresponding item in ##value##.
496
--
497
-- Example 1:
498
--
499
-- y = floor({0.5, -1.6, 9.99, 100})
500
-- -- y is {0, -2, 9, 100}
501
--
502
--
503
-- See Also:
504
-- [[:ceil]], [[:round]]
505
506
--**
507
-- Computes the next integer equal or greater than the argument.
508
--
509
-- Parameters:
510
-- # ##value## : an object, each atom of which processed, no matter how deeply nested.
511
--
512
-- Returns:
513
-- An **object**, the same shape as ##value##. Each atom in ##value##
514
-- is returned as an integer that is the smallest integer equal to or greater
515
-- than the corresponding atom in ##value##.
516
--
517
-- Comments:
518
-- This function may be applied to an atom or to all elements of a sequence.
519
--
520
-- ##ceil(X)## is 1 more than ##floor(X)## for non-integers. For integers, ##X = floor(X) = ceil(X)##.
521
--
522
-- Example 1:
523
--
524
-- sequence nums
525
-- nums = {8, -5, 3.14, 4.89, -7.62, -4.3}
526
-- nums = ceil(nums) -- {8, -5, 4, 5, -7, -4}
527
--
528
--
529
-- See Also:
530
-- [[:floor]], [[:round]]
531
53215
53315
return -floor(-a)
534
end function
535
536
--**
537
-- Return the argument's elements rounded to some precision
538
--
539
-- Parameters:
540
-- # ##value## : an object, each atom of which will be acted upon, no matter how deeply nested.
541
-- # ##precision## : an object, the rounding precision(s). If not passed, this defaults to 1.
542
--
543
-- Returns:
544
-- An **object**, the same shape as ##value##. When ##value## is an atom, the result is that atom rounded to the nearest integer multiple of 1/##precision##.
545
--
546
-- Comments:
547
-- This function may be applied to an atom or to all elements of a sequence.
548
--
549
-- Example 1:
550
--
551
-- round(5.2) -- 5
552
-- round({4.12, 4.67, -5.8, -5.21}, 10) -- {4.1, 4.7, -5.8, -5.2}
553
-- round(12.2512, 100) -- 12.25
554
--
555
--
556
-- See Also:
557
-- [[:floor]], [[:ceil]]
558
55924
560
integer len
561
sequence s
562
object t, u
563
56424
precision = abs(precision)
56524
if atom(a) then
56614
if atom(precision) then
56714
return floor(0.5 + (a * precision )) / precision
568
end if
5690
len = length(precision)
5700
s = repeat(0, len)
5710
for i = 1 to len do
5720
t = precision[i]
5730
if atom (t) then
5740
s[i] = floor( 0.5 + (a * t)) / t
575
else
5760
s[i] = round(a, t)
577
end if
5780
end for
5790
return s
58010
elsif atom(precision) then
5819
len = length(a)
5829
s = repeat(0, len)
5839
for i = 1 to len do
58417
t = a[i]
58517
if atom(t) then
58614
s[i] = floor(0.5 + (t * precision)) / precision
587
else
5883
s[i] = round(t, precision)
589
end if
59017
end for
5919
return s
592
end if
5931
len = length(a)
5941
if len != length(precision) then
5950
crash("The lengths of the two supplied sequences do not match.")
596
end if
5971
s = repeat(0, len)
5981
for i = 1 to len do
5994
t = precision[i]
6004
if atom(t) then
6014
u = a[i]
6024
if atom(u) then
6034
s[i] = floor(0.5 + (u * t)) / t
604
else
6050
s[i] = round(u, t)
606
end if
607
else
6080
s[i] = round(a[i], t)
609
end if
6104
end for
6111
return s
612
end function
613
614
--****
615
-- === Trigonometry
616
617
--****
618
-- Signature:
619
-- function arctan(object tangent)
620
--
621
-- Description:
622
-- Return an angle with given tangent.
623
--
624
-- Parameters:
625
-- # ##tangent## : an object, each atom of which will be converted, no matter how deeply nested.
626
--
627
-- Returns:
628
-- An **object**, of the same shape as ##tangent##. For each atom in ##flatten(tangent)##,
629
-- the angle with smallest magnitude that has this atom as tangent is computed.
630
--
631
-- Comments:
632
-- All atoms in the returned value lie between -PI/2 and PI/2, exclusive.
633
--
634
-- This function may be applied to an atom or to all elements of a sequence (of sequence (...)).
635
--
636
-- ##arctan##() is faster than ##[[:arcsin]]()## or ##[[:arccos]]()##.
637
--
638
-- Example 1:
639
--
640
-- s = arctan({1,2,3})
641
-- -- s is {0.785398, 1.10715, 1.24905}
642
--
643
-- See Also:
644
-- [[:arcsin]], [[:arccos]], [[:tan]], [[:flatten]]
645
646
--****
647
-- Signature:
648
-- function tan(object angle)
649
--
650
-- Description:
651
-- Return the tangent of an angle, or a sequence of angles.
652
--
653
-- Parameters:
654
-- # ##angle## : an object, each atom of which will be converted, no matter how deeply nested.
655
--
656
-- Returns:
657
-- An **object**, of the same shape as ##angle##. Each atom in the flattened ##angle## is
658
-- replaced by its tangent.
659
--
660
-- Errors:
661
-- If any atom in ##angle## is an odd multiple of PI/2, an error occurs, as its tangent
662
-- would be infinite.
663
--
664
-- Comments:
665
-- This function may be applied to an atom or to all elements of a sequence of arbitrary
666
-- shape, recursively.
667
--
668
-- Example 1:
669
--
670
-- t = tan(1.0)
671
-- -- t is 1.55741
672
--
673
-- See Also:
674
-- [[:sin]], [[:cos]], [[:arctan]]
675
676
--****
677
-- Signature:
678
-- function cos(object angle)
679
--
680
-- Description:
681
-- Return the cosine of an angle expressed in radians
682
--
683
-- Parameters:
684
-- # ##angle## : an object, each atom of which will be converted, no matter how deeply nested.
685
--
686
-- Returns:
687
-- An **object**, the same shape as ##angle##. Each atom in ##angle## is turned into its cosine.
688
--
689
-- Comments:
690
-- This function may be applied to an atom or to all elements of a sequence.
691
--
692
-- The cosine of an angle is an atom between -1 and 1 inclusive. 0.0 is hit by odd multiples of PI/2 only.
693
--
694
-- Example 1:
695
--
696
-- x = cos({.5, .6, .7})
697
-- -- x is {0.8775826, 0.8253356, 0.7648422}
698
--
699
--
700
-- See Also:
701
-- [[:sin]], [[:tan]], [[:arccos]], [[:PI]], [[:deg2rad]]
702
703
--****
704
-- Signature:
705
-- function sin(object angle)
706
--
707
-- Description:
708
-- Return the sine of an angle expressed in radians
709
--
710
-- Parameters:
711
-- # ##angle## : an object, each atom in which will be acted upon.
712
--
713
-- Returns:
714
-- An **object**, the same shape as ##angle##. When ##angle## is an atom, the
715
-- result is the sine of ##angle##.
716
--
717
-- Comments:
718
-- This function may be applied to an atom or to all elements of a sequence.
719
--
720
-- The sine of an angle is an atom between -1 and 1 inclusive. 0.0 is hit by integer
721
-- multiples of PI only.
722
--
723
-- Example 1:
724
--
725
-- sin_x = sin({.5, .9, .11})
726
-- -- sin_x is {.479, .783, .110}
727
--
728
--
729
-- See Also:
730
-- [[:cos]], [[:arcsin]], [[:PI]], [[:deg2rad]]
731
732
--**
733
-- Return an angle given its cosine.
734
--
735
-- Parameters:
736
-- # ##value## : an object, each atom in which will be acted upon.
737
--
738
-- Returns:
739
-- An **object**, the same shape as ##value##. When ##value## is an atom, the result is
740
-- an atom, an angle whose cosine is ##value##.
741
--
742
-- Errors:
743
-- If any atom in ##value## is not in the -1..1 range, it cannot be the cosine of a real
744
-- number, and an error occurs.
745
--
746
-- Comments:
747
--
748
-- A value between 0 and [[:PI]] radians will be returned.
749
--
750
-- This function may be applied to an atom or to all elements of a sequence.
751
--
752
-- ##arccos##() is not as fast as [[:arctan]]().
753
--
754
-- Example 1:
755
--
756
-- s = arccos({-1,0,1})
757
-- -- s is {3.141592654, 1.570796327, 0}
758
--
759
--
760
-- See Also:
761
-- [[:cos]], [[:PI]], [[:arctan]]
762
7632
764
-- returns angle in radians
7652
return HALFPI - 2 * arctan(x / (1.0 + sqrt(1.0 - x * x)))
766
end function
767
768
--**
769
-- Return an angle given its sine.
770
--
771
-- Parameters:
772
-- # ##value## : an object, each atom in which will be acted upon.
773
--
774
-- Returns:
775
-- An **object**, the same shape as ##value##. When ##value## is an atom, the result is an atom, an angle whose sine is ##value##.
776
--
777
-- Errors:
778
-- If any atom in ##value## is not in the -1..1 range, it cannot be the sine of a real number, and an error occurs.
779
--
780
-- Comments:
781
-- A value between -PI/2 and +PI/2 (radians) inclusive will be returned.
782
--
783
-- This function may be applied to an atom or to all elements of a sequence.
784
--
785
-- ##arcsin##() is not as fast as [[:arctan]]().
786
--
787
-- Example 1:
788
--
789
-- s = arcsin({-1,0,1})
790
-- s is {-1.570796327, 0, 1.570796327}
791
--
792
--
793
-- See Also:
794
-- [[:arccos]], [[:arccos]], [[:sin]]
795
7961
797
-- returns angle in radians
7981
return 2 * arctan(x / (1.0 + sqrt(1.0 - x * x)))
799
end function
800
801
--**
802
-- Calculate the arctangent of a ratio.
803
--
804
-- Parameters:
805
-- # ##y## : an atom, the numerator of the ratio
806
-- # ##x## : an atom, the denominator of the ratio
807
--
808
-- Returns:
809
-- An **atom**, which is equal to [[:arctan]](##y##/##x##), except that it can handle zero denominator and is more accurate.
810
--
811
-- Example 1:
812
--
813
-- a = atan2(10.5, 3.1)
814
-- -- a is 1.283713958
815
--
816
--
817
-- See Also:
818
-- [[:arctan]]
819
8205
8215
if x > 0 then
8222
return arctan(y/x)
8233
elsif x < 0 then
8242
if y < 0 then
8251
return arctan(y/x) - PI
826
else
8271
return arctan(y/x) + PI
828
end if
8291
elsif y > 0 then
8300
return HALFPI
8311
elsif y < 0 then
8320
return -(HALFPI)
833
else
8341
return 0
835
end if
836
end function
837
838
--**
839
-- Convert an angle measured in radians to an angle measured in degrees
840
--
841
-- Parameters:
842
-- # ##angle## : an object, all atoms of which will be converted, no matter how deeply nested.
843
--
844
-- Returns:
845
-- An **object**, the same shape as ##angle##, all atoms of which were multiplied by 180/PI.
846
--
847
-- Comments:
848
-- This function may be applied to an atom or sequence. A flat angle is PI radians and 180 degrees.
849
--
850
-- [[:arcsin]](), [[:arccos]]() and [[:arctan]]() return angles in radians.
851
--
852
-- Example 1:
853
--
854
-- x = rad2deg(3.385938749)
855
-- -- x is 194
856
--
857
--
858
-- See Also:
859
-- [[:deg2rad]]
860
8613
8623
return x * RADIANS_TO_DEGREES
863
end function
864
865
--**
866
-- Convert an angle measured in degrees to an angle measured in radians
867
--
868
-- Parameters:
869
-- # ##angle## : an object, all atoms of which will be converted, no matter how deeply nested.
870
--
871
-- Returns:
872
-- An **object**, the same shape as ##angle##, all atoms of which were multiplied by PI/180.
873
--
874
-- Comments:
875
-- This function may be applied to an atom or sequence. A flat angle is PI radians and 180 degrees.
876
-- [[:sin]](), [[:cos]]() and [[:tan]]() expect angles in radians.
877
--
878
-- Example 1:
879
--
880
-- x = deg2rad(194)
881
-- -- x is 3.385938749
882
--
883
-- See Also:
884
-- [[:rad2deg]]
885
8867
8877
return x * DEGREES_TO_RADIANS
888
end function
889
890
--****
891
-- === Logarithms and powers.
892
--
893
894
--****
895
-- Signature:
896
-- function log(object value)
897
--
898
-- Description:
899
-- Return the natural logarithm of a positive number.
900
--
901
-- Parameters:
902
-- # ##value## : an object, any atom of which ##log##() acts upon.
903
--
904
-- Returns:
905
-- An **object**, the same shape as ##value##. For an atom, the returned atom is its logarithm of base E.
906
--
907
-- Errors:
908
-- If any atom in ##value## is not greater than zero, an error occurs as its logarithm is not defined.
909
--
910
-- Comments:
911
-- This function may be applied to an atom or to all elements
912
-- of a sequence.
913
--
914
-- To compute the inverse, you can use power(E, x)
915
-- where E is 2.7182818284590452, or equivalently [[:exp]](x). Beware that the logarithm grows very slowly with x, so that [[:exp]]() grows very fast.
916
--
917
-- Example 1:
918
--
919
-- a = log(100)
920
-- -- a is 4.60517
921
--
922
-- See Also:
923
-- [[:E]], [[:exp]], [[:log10]]
924
--
925
926
--**
927
-- Return the base 10 logarithm of a number.
928
--
929
-- Parameters:
930
-- # ##value## : an object, each atom of which will be converted, no matter how deeply nested.
931
--
932
-- Returns:
933
-- An **object**, the same shape as ##value##. When ##value## is an atom, raising 10 to the returned atom yields ##value## back.
934
--
935
-- Errors:
936
-- If any atom in ##value## is not greater than zero, its logarithm is not a real number and an error occurs.
937
--
938
-- Comments:
939
-- This function may be applied to an atom or to all elements
940
-- of a sequence.
941
--
942
-- ##log10##() is proportional to ##log##() by a factor of ##1/log(10)##,
943
-- which is about ##0.435## .
944
--
945
-- Example 1:
946
--
947
-- a = log10(12)
948
-- -- a is 2.48490665
949
--
950
--
951
-- See Also:
952
-- [[:log]]
953
9543
9553
return log(x1) * INVLN10
956
end function
957
958
--**
959
-- Computes some power of E.
960
--
961
-- Parameters:
962
-- # ##value## : an object, all atoms of which will be acted upon, no matter how deeply nested.
963
--
964
--Returns:
965
-- An **object**, the same shape as ##value##. When ##value## is an atom, its exponential is being returned.
966
--
967
-- Comments:
968
-- This function can be applied to a single atom or to a sequence of any shape.
969
--
970
-- Due to its rapid growth, the returned values start losing accuracy as soon as values are greater than 10. Values above 710 will cause an overflow in hardware.
971
--
972
-- Example 1:
973
--
974
-- x = exp(5.4)
975
-- -- x is 221.4064162
976
--
977
-- See Also:
978
-- [[:log]]
979
98010
98110
return power(E, x)
982
end function
983
984
--****
985
-- Signature:
986
-- function power(object base, object exponent)
987
--
988
-- Description:
989
-- Raise a base value to some power.
990
--
991
-- Parameters:
992
-- # ##base## : an object, the value(s) to raise to some power.
993
-- # ##exponent## : an object, the exponent(s) to apply to ##base##.
994
--
995
-- Returns:
996
-- An **object**, the shape of which depends on ##base##'s and ##exponent##'s. For two atoms, this will be ##base## raised to the power ##exponent##.
997
--
998
-- Errors:
999
-- If some atom in ##base## is negative and is raised to a non integer exponent, an error will occur, as the result is undefined.
1000
--
1001
-- If 0 is raised to any negative power, this is the same as a zero divide and causes an error.
1002
--
1003
-- ##power(0,0)## is illegal, because there is not an unique value that can be assigned to that quantity.
1004
--
1005
-- Comments:
1006
--
1007
-- The arguments to this function may be atoms or sequences. The rules for
1008
-- [[:operations on sequences]] apply.
1009
--
1010
-- Powers of 2 are calculated very efficiently.
1011
--
1012
-- Other languages have a ~** or ^ operator to perform the same action. But they don't have sequences.
1013
--
1014
-- Example 1:
1015
--
1016
-- ? power(5, 2)
1017
-- -- 25 is printed
1018
--
1019
--
1020
-- Example 2:
1021
--
1022
-- ? power({5, 4, 3.5}, {2, 1, -0.5})
1023
-- -- {25, 4, 0.534522} is printed
1024
--
1025
--
1026
-- Example 3:
1027
--
1028
-- ? power(2, {1, 2, 3, 4})
1029
-- -- {2, 4, 8, 16}
1030
--
1031
--
1032
-- Example 4:
1033
--
1034
-- ? power({1, 2, 3, 4}, 2)
1035
-- -- {1, 4, 9, 16}
1036
--
1037
--
1038
-- See Also:
1039
-- [[:log]], [[:Operations on sequences]]
1040
1041
--****
1042
-- Signature:
1043
-- function sqrt(object value)
1044
--
1045
-- Description:
1046
-- Calculate the square root of a number.
1047
--
1048
-- Parameters:
1049
-- # ##value## : an object, each atom in which will be acted upon.
1050
--
1051
-- Returns:
1052
-- An **object**, the same shape as ##value##. When ##value## is an atom, the result is the positive atom whose square is ##value##.
1053
--
1054
-- Errors:
1055
-- If any atom in ##value## is less than zero, an error will occur, as no squared real can be less than zero.
1056
--
1057
-- Comments:
1058
-- This function may be applied to an atom or to all elements of a sequence.
1059
--
1060
-- Example 1:
1061
--
1062
-- r = sqrt(16)
1063
-- -- r is 4
1064
--
1065
--
1066
-- See Also:
1067
-- [[:power]], [[:Operations on sequences]]
1068
--
1069
1070
--****
1071
-- === Hyperbolic trigonometry
1072
--
1073
1074
--**
1075
-- Computes the hyperbolic cosine of an object.
1076
--
1077
-- Parameters:
1078
-- # ##x## : the object to process.
1079
--
1080
-- Returns:
1081
-- An **object**, the same shape as ##x##, each atom of which was acted upon.
1082
--
1083
-- Comments:
1084
--
1085
-- The hyperbolic cosine grows like the exponential function.
1086
--
1087
-- For all reals, ##power(cosh(x), 2) - power(sinh(x), 2) = 1##. Compare
1088
-- with ordinary trigonometry.
1089
--
1090
-- Example 1:
1091
--
1092
-- ? cosh(LN2) -- prints out 1.25
1093
--
1094
--
1095
-- See Also:
1096
-- [[:cos]], [[:sinh]], [[:arccosh]]
1097
10982
10992
return (exp(a)+exp(-a))/2
1100
end function
1101
1102
--**
1103
-- Computes the hyperbolic sine of an object.
1104
--
1105
-- Parameters:
1106
-- # ##x## : the object to process.
1107
--
1108
-- Returns:
1109
-- An **object**, the same shape as ##x##, each atom of which was acted upon.
1110
--
1111
-- Comments:
1112
--
1113
-- The hyperbolic sine grows like the exponential function.
1114
--
1115
-- For all reals, ##power(cosh(x), 2) - power(sinh(x), 2) = 1##. Compare
1116
-- with ordinary trigonometry.
1117
--
1118
-- Example 1:
1119
--
1120
-- ? sinh(LN2) -- prints out 0.75
1121
--
1122
--
1123
-- See Also:
1124
-- [[:cosh]], [[:sin]], [[:arcsinh]]
1125
11262
11272
return (exp(a)-exp(-a))/2
1128
end function
1129
1130
--**
1131
-- Computes the hyperbolic tangent of an object.
1132
--
1133
-- Parameters:
1134
-- # ##x## : the object to process.
1135
--
1136
-- Returns:
1137
-- An **object**, the same shape as ##x##, each atom of which was acted upon.
1138
--
1139
-- Comments:
1140
--
1141
-- The hyperbolic tangent takes values from -1 to +1.
1142
--
1143
-- ##tanh##() is the ratio ##sinh() / cosh()##. Compare with ordinary trigonometry.
1144
--
1145
-- Example 1:
1146
--
1147
-- ? tanh(LN2) -- prints out 0.6
1148
--
1149
--
1150
-- See Also:
1151
-- [[:cosh]], [[:sinh]], [[:tan]], [[:arctanh]]
1152
11531
11541
return sinh(a)/cosh(a)
1155
end function
1156
1157
--**
1158
-- Computes the reverse hyperbolic sine of an object.
1159
--
1160
-- Parameters:
1161
-- # ##x## : the object to process.
1162
--
1163
-- Returns:
1164
-- An **object**, the same shape as ##x##, each atom of which was acted upon.
1165
--
1166
-- Comments:
1167
--
1168
-- The hyperbolic sine grows like the logarithm function.
1169
--
1170
-- Example 1:
1171
--
1172
-- ? arcsinh(1) -- prints out 0,4812118250596034
1173
--
1174
--
1175
-- See Also:
1176
-- [[:arccosh]], [[:arcsin]], [[:sinh]]
1177
11781
11791
return log(a+sqrt(1+a*a))
1180
end function
1181
11825
11835
if atom(x) then
11843
return x>=1.0
1185
end if
11862
for i=1 to length(x) do
11873
if not not_below_1(x[i]) then
11880
return 0
1189
end if
11903
end for
11912
return 1
1192
end type
1193
1194
--**
1195
-- Computes the reverse hyperbolic cosine of an object.
1196
--
1197
-- Parameters:
1198
-- # ##x## : the object to process.
1199
--
1200
-- Returns:
1201
-- An **object**, the same shape as ##x##, each atom of which was acted upon.
1202
--
1203
-- Errors:
1204
-- Since [[:cosh]] only takes values not below 1, an argument below 1 causes an error.
1205
--
1206
-- Comments:
1207
--
1208
-- The hyperbolic cosine grows like the logarithm function.
1209
--
1210
-- Example 1:
1211
--
1212
-- ? arccosh(1) -- prints out 0
1213
--
1214
--
1215
-- See Also:
1216
-- [[:arccos]], [[:arcsinh]], [[:cosh]]
1217
12182
12192
return log(a+sqrt(a*a-1))
1220
end function
1221
12225
12235
if atom(x) then
12243
return x>-1.0 and x<1.0
1225
end if
12262
for i=1 to length(x) do
12273
if not abs_below_1(x[i]) then
12280
return 0
1229
end if
12303
end for
12312
return 1
1232
end type
1233
1234
--**
1235
-- Computes the reverse hyperbolic tangent of an object.
1236
--
1237
-- Parameters:
1238
-- # ##x## : the object to process.
1239
--
1240
-- Returns:
1241
-- An **object**, the same shape as ##x##, each atom of which was acted upon.
1242
--
1243
-- Errors:
1244
-- Since [[:tanh]] only takes values between -1 and +1 excluded, an out of range argument causes an error.
1245
--
1246
-- Comments:
1247
--
1248
-- The hyperbolic cosine grows like the logarithm function.
1249
--
1250
-- Example 1:
1251
--
1252
-- ? arctanh(1/2) -- prints out 0,5493061443340548456976
1253
--
1254
--
1255
-- See Also:
1256
-- [[:arccos]], [[:arcsinh]], [[:cosh]]
1257
12582
12592
return log((1+a)/(1-a))/2
1260
end function
1261
1262
--****
1263
-- === Accumulation
1264
--
1265
1266
--**
1267
-- Compute the sum of all atoms in the argument, no matter how deeply nested
1268
--
1269
-- Parameters:
1270
-- # ##values## : an object, all atoms of which will be added up, no matter how nested.
1271
--
1272
-- Returns:
1273
-- An **atom**, the sum of all atoms in [[:flatten]](##values##).
1274
--
1275
-- Comments:
1276
-- This function may be applied to an atom or to all elements of a sequence
1277
--
1278
-- Example 1:
1279
--
1280
-- a = sum({10, 20, 30})
1281
-- -- a is 60
1282
--
1283
-- a = sum({10.5, {11.2} , 8.1})
1284
-- -- a is 29.8
1285
--
1286
--
1287
-- See Also:
1288
-- [[:can_add]], [[:product]], [[:or_all]]
1289
12904
1291
atom b
12924
if atom(a) then
12931
return a
1294
end if
12953
b = 0
12963
for i = 1 to length(a) do
12979
if atom(a[i]) then
12988
b += a[i]
1299
else
13001
b += sum(a[i])
1301
end if
13029
end for
13033
return b
1304
end function
1305
1306
--**
1307
-- Compute the product of all the atom in the argument, no matter how deeply nested.
1308
--
1309
-- Parameters:
1310
-- # ##values## : an object, all atoms of which will be multiplied up, no matter how nested.
1311
--
1312
-- Returns:
1313
-- An **atom**, the product of all atoms in [[:flatten]](##values##).
1314
--
1315
-- Comments:
1316
-- This function may be applied to an atom or to all elements of a sequence
1317
--
1318
-- Example 1:
1319
--
1320
-- a = product({10, 20, 30})
1321
-- -- a is 6000
1322
--
1323
-- a = product({10.5, {11.2} , 8.1})
1324
-- -- a is 952.56
1325
--
1326
--
1327
-- See Also:
1328
-- [[:can_add]], [[:sum]], [[:or_all]]
1329
13306
1331
atom b
13326
if atom(a) then
13331
return a
1334
end if
13355
b = 1
13365
for i = 1 to length(a) do
133712
if atom(a[i]) then
133810
b *= a[i]
1339
else
13402
b *= product(a[i])
1341
end if
134212
end for
13435
return b
1344
end function
1345
1346
1347
--**
1348
-- Or's together all atoms in the argument, no matter how deeply nested.
1349
--
1350
-- Parameters:
1351
-- # ##values## : an object, all atoms of which will be added up, no matter how nested.
1352
--
1353
-- Returns:
1354
-- An **atom**, the result of or'ing all atoms in [[:flatten]](##values##).
1355
--
1356
-- Comments:
1357
--
1358
-- This function may be applied to an atom or to all elements of a sequence. It performs [[:or_bits]]() operations repeatedly.
1359
--
1360
-- Example 1:
1361
--
1362
-- a = sum({10, 7, 35})
1363
-- -- a is 47
1364
--
1365
--
1366
-- See Also:
1367
-- [[:can_add]], [[:sum]], [[:product]], [[:or_bits]]
1368
136922
1370
atom b
137122
if atom(a) then
13721
return a
1373
end if
137421
b = 0
137521
for i = 1 to length(a) do
1376204
if atom(a[i]) then
1377203
b = or_bits(b, a[i])
1378
else
13791
b = or_bits(b, or_all(a[i]))
1380
end if
1381204
end for
138221
return b
1383
end function
1384
1385
--****
1386
-- === Bitwise operations
1387
--
1388
1389
--****
1390
-- Signature:
1391
-- function and_bits(object a, object b)
1392
--
1393
-- Description:
1394
-- Perform the logical AND operation on corresponding bits in two objects. A bit in the
1395
-- result will be 1 only if the corresponding bits in both arguments are 1.
1396
--
1397
-- Parameters:
1398
-- # ##a## : one of the objects involved
1399
-- # ##b## : the second object
1400
--
1401
-- Returns:
1402
-- An **object**, whose shape depends on the shape of both arguments. Each atom in this object
1403
-- is obtained by logical AND between atoms on both objects.
1404
--
1405
-- Comments:
1406
--
1407
-- The arguments to this function may be atoms or sequences. The rules for operations on sequences apply.
1408
-- The atoms in the arguments must be representable as 32-bit numbers, either signed or unsigned.
1409
--
1410
-- If you intend to manipulate full 32-bit values, you should declare your variables as atom, rather than integer. Euphoria's integer type is limited to 31-bits.
1411
--
1412
-- Results are treated as signed numbers. They will be negative when the highest-order bit is 1.
1413
--
1414
-- To understand the binary representation of a number you should display it in hexadecimal notation.
1415
-- Use the %x format of [[:printf]](). Using [[:int_to_bits]]() is an even more direct approach.
1416
--
1417
-- Example 1:
1418
--
1419
-- a = and_bits(#0F0F0000, #12345678)
1420
-- -- a is #02040000
1421
--
1422
--
1423
-- Example 2:
1424
--
1425
-- a = and_bits(#FF, {#123456, #876543, #2211})
1426
-- -- a is {#56, #43, #11}
1427
--
1428
--
1429
-- Example 3:
1430
--
1431
-- a = and_bits(#FFFFFFFF, #FFFFFFFF)
1432
-- -- a is -1
1433
-- -- Note that #FFFFFFFF is a positive number,
1434
-- -- but the result of a bitwise logical operation is interpreted
1435
-- -- as a signed 32-bit number, so it's negative.
1436
--
1437
--
1438
-- See Also:
1439
-- [[:or_bits]], [[:xor_bits]], [[:not_bits]], [[:int_to_bits]]
1440
--
1441
1442
--****
1443
-- Signature:
1444
-- function xor_bits(object a, object b)
1445
--
1446
-- Description:
1447
-- Perform the logical XOR operation on corresponding bits in two objects. A bit in the
1448
-- result will be 1 only if the corresponding bits in both arguments are different.
1449
--
1450
-- Parameters:
1451
-- # ##a## : one of the objects involved
1452
-- # ##b## : the second object
1453
--
1454
-- Returns:
1455
-- An **object**, whose shape depends on the shape of both arguments. Each atom in this object
1456
-- is obtained by logical XOR between atoms on both objects.
1457
--
1458
-- Comments:
1459
-- The arguments must be representable as 32-bit numbers, either signed or unsigned.
1460
--
1461
-- If you intend to manipulate full 32-bit values, you should declare your variables as atom, rather than integer. Euphoria's integer type is limited to 31-bits.
1462
--
1463
-- Results are treated as signed numbers. They will be negative when the highest-order bit is 1.
1464
--
1465
-- Example 1:
1466
--
1467
-- a = xor_bits(#0110, #1010)
1468
-- -- a is #1100
1469
--
1470
--
1471
-- See Also:
1472
-- [[:and_bits]], [[:or_bits]], [[:not_bits]], [[:int_to_bits]]
1473
1474
--****
1475
-- Signature:
1476
-- function or_bits(object a, object b)
1477
--
1478
-- Description:
1479
-- Perform the logical OR operation on corresponding bits in two objects. A bit in the
1480
-- result will be 1 only if the corresponding bits in both arguments are both 0.
1481
--
1482
-- Parameters:
1483
-- # ##a## : one of the objects involved
1484
-- # ##b## : the second object
1485
--
1486
-- Returns:
1487
-- An **object**, whose shape depends on the shape of both arguments. Each atom in this object
1488
-- is obtained by logical XOR between atoms on both objects.
1489
--
1490
-- Comments:
1491
-- The arguments must be representable as 32-bit numbers, either signed or unsigned.
1492
--
1493
-- If you intend to manipulate full 32-bit values, you should declare your variables as atom, rather than integer. Euphoria's integer type is limited to 31-bits.
1494
--
1495
-- Results are treated as signed numbers. They will be negative when the highest-order bit is 1.
1496
--
1497
-- Example 1:
1498
--
1499
-- a = or_bits(#0F0F0000, #12345678)
1500
-- -- a is #1F3F5678
1501
--
1502
--
1503
-- Example 2:
1504
--
1505
-- a = or_bits(#FF, {#123456, #876543, #2211})
1506
-- -- a is {#1234FF, #8765FF, #22FF}
1507
--
1508
--
1509
-- See Also:
1510
-- [[:and_bits]], [[:xor_bits]], [[:not_bits]], [[:int_to_bits]]
1511
1512
--****
1513
-- Signature:
1514
-- function not_bits(object a)
1515
--
1516
-- Description:
1517
-- Perform the logical NOT operation on each bit in an object. A bit in the result will be 1
1518
-- when the corresponding bit in x1 is 0, and will be 0 when the corresponding bit in x1 is 1.
1519
--
1520
-- Parameters:
1521
-- # ##a## : the object to invert the bits of.
1522
--
1523
-- Returns:
1524
-- An **object**, the same shape as ##a##. Each bit in an atom of the result is the reverse of the corresponding bit inside ##a##.
1525
--
1526
-- Comments:
1527
-- The argument to this function may be an atom or a sequence.
1528
--
1529
-- The argument must be representable as a 32-bit number, either signed or unsigned.
1530
--
1531
-- If you intend to manipulate full 32-bit values, you should declare your variables as atom, rather than integer. Euphoria's integer type is limited to 31-bits.
1532
--
1533
-- Results are treated as signed numbers. They will be negative when the highest-order bit is 1.
1534
--
1535
-- A simple equality holds for an atom ##a##: ##a + not_bits(a) = -1##.
1536
--
1537
-- Example 1:
1538
--
1539
-- a = not_bits(#000000F7)
1540
-- -- a is -248 (i.e. FFFFFF08 interpreted as a negative number)
1541
--
1542
--
1543
-- See Also:
1544
-- [[:and_bits]], [[:or_bits]], [[:xor_bits]], [[:int_to_bits]]
1545
1546
--**
1547
-- Moves the bits in the input value by the specified distance.
1548
--
1549
-- Parameters:
1550
-- # ##source_number## : object: The value(s) whose bits will be be moved.
1551
-- # ##shift_distance## : integer: number of bits to be moved by.
1552
-- Comments:
1553
-- * If ##source_number## is a sequence, each element is shifted.
1554
-- * The value(s) in ##source_number## are first truncated to a 32-bit integer.
1555
-- * The output is truncated to a 32-bit integer.
1556
-- * Vacated bits are replaced with zero.
1557
-- * If ##shift_distance## is negative, the bits in ##source_number## are moved left.
1558
-- * If ##shift_distance## is positive, the bits in ##source_number## are moved right.
1559
-- * If ##shift_distance## is zero, the bits in ##source_number## are not moved.
1560
--
1561
-- Returns:
1562
-- Atom(s) containing a 32-bit integer. A single atom in ##source_number## is an atom, or
1563
-- a sequence in the same form as ##source_number## containing 32-bit integers.
1564
--
1565
-- Example 1:
1566
--
1567
-- ? shift_bits((7, -3) --> 56
1568
-- ? shift_bits((0, -9) --> 0
1569
-- ? shift_bits((4, -7) --> 512
1570
-- ? shift_bits((8, -4) --> 128
1571
-- ? shift_bits((0xFE427AAC, -7) --> 0x213D5600
1572
-- ? shift_bits((-7, -3) --> -56 which is 0xFFFFFFC8
1573
-- ? shift_bits((131, 0) --> 131
1574
-- ? shift_bits((184.464, 0) --> 184
1575
-- ? shift_bits((999_999_999_999_999, 0) --> -1530494977 which is 0xA4C67FFF
1576
-- ? shift_bits((184, 3) -- 23
1577
-- ? shift_bits((48, 2) --> 12
1578
-- ? shift_bits((121, 3) --> 15
1579
-- ? shift_bits((0xFE427AAC, 7) --> 0x01FC84F5
1580
-- ? shift_bits((-7, 3) --> 0x1FFFFFFF
1581
-- ? shift_bits({48, 121}, 2) --> {12, 30}
1582
--
1583
--
1584
-- See Also:
1585
-- [[:rotate_bits]]
1586
15871177
1588
15891177
if sequence(source_number) then
15901
for i = 1 to length(source_number) do
15916
source_number[i] = shift_bits(source_number[i], shift_distance)
15926
end for
15931
return source_number
1594
end if
15951176
source_number = and_bits(source_number, 0xFFFFFFFF)
15961176
if shift_distance = 0 then
15975
return source_number
1598
end if
1599
16001171
if shift_distance < 0 then
1601583
source_number *= power(2, -shift_distance)
1602
else
1603588
integer lSigned = 0
1604
-- Check for the sign bit so we don't propagate it.
1605588
if and_bits(source_number, 0x80000000) then
1606302
lSigned = 1
1607302
source_number = and_bits(source_number, 0x7FFFFFFF)
1608
end if
1609588
source_number /= power(2, shift_distance)
1610588
if lSigned and shift_distance < 32 then
1611
-- Put back the sign bit now shifted
1612302
source_number = or_bits(source_number, power(2, 31-shift_distance))
1613
end if
1614
end if
1615
16161171
return and_bits(source_number, 0xFFFFFFFF)
1617
end function
1618
1619
--**
1620
-- Rotates the bits in the input value by the specified distance.
1621
--
1622
-- Parameters:
1623
-- # ##source_number## : object: value(s) whose bits will be be rotated.
1624
-- # ##shift_distance## : integer: number of bits to be moved by.
1625
-- Comments:
1626
-- * If ##source_number## is a sequence, each element is rotated.
1627
-- * The value(s) in ##source_number## are first truncated to a 32-bit integer.
1628
-- * The output is truncated to a 32-bit integer.
1629
-- * If ##shift_distance## is negative, the bits in ##source_number## are rotated left.
1630
-- * If ##shift_distance## is positive, the bits in ##source_number## are rotated right.
1631
-- * If ##shift_distance## is zero, the bits in ##source_number## are not rotated.
1632
--
1633
-- Returns:
1634
-- Atom(s) containing a 32-bit integer. A single atom in ##source_number## is an atom, or
1635
-- a sequence in the same form as ##source_number## containing 32-bit integers.
1636
--
1637
-- Example 1:
1638
--
1639
-- ? rotate_bits(7, -3) --> 56
1640
-- ? rotate_bits(0, -9) --> 0
1641
-- ? rotate_bits(4, -7) --> 512
1642
-- ? rotate_bits(8, -4) --> 128
1643
-- ? rotate_bits(0xFE427AAC, -7) --> 0x213D567F
1644
-- ? rotate_bits(-7, -3) --> -49 which is 0xFFFFFFCF
1645
-- ? rotate_bits(131, 0) --> 131
1646
-- ? rotate_bits(184.464, 0) --> 184
1647
-- ? rotate_bits(999_999_999_999_999, 0) --> -1530494977 which is 0xA4C67FFF
1648
-- ? rotate_bits(184, 3) -- 23
1649
-- ? rotate_bits(48, 2) --> 12
1650
-- ? rotate_bits(121, 3) --> 536870927
1651
-- ? rotate_bits(0xFE427AAC, 7) --> 0x59FC84F5
1652
-- ? rotate_bits(-7, 3) --> 0x3FFFFFFF
1653
-- ? rotate_bits({48, 121}, 2) --> {12, 1073741854}
1654
--
1655
--
1656
-- See Also:
1657
-- [[:shift_bits]]
1658
1659601
1660
atom lTemp
1661
atom lSave
1662
integer lRest
1663
1664601
if sequence(source_number) then
16651
for i = 1 to length(source_number) do
16666
source_number[i] = rotate_bits(source_number[i], shift_distance)
16676
end for
16681
return source_number
1669
end if
1670
1671600
source_number = and_bits(source_number, 0xFFFFFFFF)
1672600
if shift_distance = 0 then
167322
return source_number
1674
end if
1675
1676578
if shift_distance < 0 then
16775
lSave = not_bits(power(2, 32 + shift_distance) - 1)
16785
lRest = 32 + shift_distance
1679
else
1680573
lSave = power(2, shift_distance) - 1
1681573
lRest = shift_distance - 32
1682
end if
1683
1684578
lTemp = shift_bits(and_bits(source_number, lSave), lRest)
1685578
source_number = shift_bits(source_number, shift_distance)
1686578
return or_bits(source_number, lTemp)
1687
end function
1688
1689
--****
1690
-- Arithmetics
1691
--
1692
1693
--**
1694
-- Returns the greater common divisor of to atoms
1695
--
1696
-- Parameters:
1697
-- # ##p## : one of the atoms to consider
1698
-- # ##q## : the other atom.
1699
--
1700
-- Returns:
1701
-- A positive **atom**, without a fractional part, evenly dividing both parameters, and is the
1702
-- greatest value with those properties.
1703
--
1704
-- Comments:
1705
--
1706
-- Signs are ignored. Atoms are rounded down to integers.
1707
--
1708
-- Any zero parameter causes 0 to be returned.
1709
--
1710
-- Parameters and return value are atoms so as to take mathematical integers up to ##power(2,53)##.
1711
--
1712
-- Example 1:
1713
--
1714
-- ? gcd(76.3, -114) -- prints out gcd(76,114), which is 38
1715
--
1716
--
1717
17182
1719
atom r
1720
17212
if p<0 then
17221
p=floor(-p)
1723
else
17241
p=floor(p)
1725
end if
17262
if q<0 then
17271
q=floor(-q)
1728
else
17291
q=floor(q)
1730
end if
17312
if p
17321
r=p
17331
p=q
17341
q=r
1735
end if
17362
if q<=1 then
17371
return q
1738
end if
1739
17401
while 1 do
17419
r=remainder(p,q)
17429
if r=1 then
17430
return r
17449
elsif r=0 then
17451
return q
1746
else
17478
p=q
17488
q=r
1749
end if
17508
end while
1751
end function
1752
1753
1754
--****
1755
-- Floating Point
1756
--
1757
1758
--**
1759
-- Compares two (sets of) numbers based on approximate equality.
1760
--
1761
-- Parameters:
1762
-- # ##p## : an object, one of the sets to consider
1763
-- # ##q## : an object, the other set.
1764
-- # ##epsilon## : an atom used to define the amount of inequality allowed.
1765
-- This must be a positive value. Default is 0.005
1766
--
1767
-- Returns:
1768
-- An **integer**,
1769
-- * 1 when p > (q + epsilon) : P is definitely greater than q.
1770
-- * -1 when p < (q - epsilon) : P is definitely less than q.
1771
-- * 0 when p >= (q - epsilon) and p <= (q + epsilon) : p and q are approximately equal.
1772
--
1773
-- Comments:
1774
-- This can be used to see if two numbers are near enough to each other.
1775
--
1776
-- Also, because of the way floating point numbers are stored, it not always possible
1777
-- express every real number exactly, especially after a series of arithmetic
1778
-- operations. You can use ##approx()## to see if two floating point numbers
1779
-- are almost the same value.
1780
--
1781
-- If ##p## and ##q## are both sequences, they must be the same length as each other.
1782
--
1783
-- If ##p## or ##q## is a sequence, but the other is not, then the result is a
1784
-- sequence of results whose length is the same as the sequence argument.
1785
--
1786
-- Example 1:
1787
--
1788
-- ? approx(10, 33.33 * 30.01 / 100) --> 0 because 10 and 10.002333 are within 0.005 of each other
1789
-- ? approx(10, 10.001) -> 0 because 10 and 10.001 are within 0.005 of each other
1790
-- ? approx(10, {10.001,9.999, 9.98, 10.04}) --> {0,0,1,-1}
1791
-- ? approx({10.001,9.999, 9.98, 10.04}, 10) --> {0,0,-1,1}
1792
-- ? approx({10.001,{9.999, 10.01}, 9.98, 10.04}, {10.01,9.99, 9.8, 10.4}) --> {-1,{1,1},1,-1}
1793
-- ? approx(23,32, 10) -> 0 because 23 and 32 are within 10 of each other.
1794
--
1795
--
179622
1797
179822
if sequence(p) then
17993
if sequence(q) then
18001
if length(p) != length(q) then
18010
crash("approx(): Sequence arguments must be the same length")
1802
end if
18031
for i = 1 to length(p) do
18044
p[i] = approx(p[i], q[i])
18054
end for
18061
return p
1807
else
18082
for i = 1 to length(p) do
18096
p[i] = approx(p[i], q)
18106
end for
18112
return p
1812
end if
181319
elsif sequence(q) then
18141
for i = 1 to length(q) do
18154
q[i] = approx(p, q[i])
18164
end for
18171
return q
1818
else
181918
if p > (q + epsilon) then
18205
return 1
1821
end if
1822
182313
if p < (q - epsilon) then
18244
return -1
1825
end if
1826
18279
return 0
1828
end if
1829
end function
1830
1831
--**
1832
-- Tests for power of 2
1833
--
1834
-- Parameters:
1835
-- # ##p## : an object. The item to test. This can be an integer, atom or sequence.
1836
--
1837
-- Returns:
1838
-- An **integer**,
1839
-- * 1 for each item in ##p## that is a power of two, eg. 2,4,8,16,32, ...
1840
-- * 0 for each item in ##p## that is **not** a power of two, eg. 3, 54.322, -2
1841
--
1842
-- Example 1:
1843
--
1844
-- for i = 1 to 10 do
1845
-- ? {i, powof2(i)}
1846
-- end for
1847
-- -- output ...
1848
-- -- {1,1}
1849
-- -- {2,1}
1850
-- -- {3,0}
1851
-- -- {4,1}
1852
-- -- {5,0}
1853
-- -- {6,0}
1854
-- -- {7,0}
1855
-- -- {8,1}
1856
-- -- {9,0}
1857
-- -- {10,0}
1858
--
1859
--
1860
186121
186221
return not (and_bits(p, p-1))
1863
end function
1864
1865
1866
--**
1867
-- Test if the supplied integer is a even or odd number.
1868
--
1869
-- Parameters:
1870
-- # ##test_integer## : an integer. The item to test.
1871
--
1872
-- Returns:
1873
-- An **integer**,
1874
-- * 1 if its even.
1875
-- * 0 if its odd.
1876
--
1877
-- Example 1:
1878
--
1879
-- for i = 1 to 10 do
1880
-- ? {i, is_even(i)}
1881
-- end for
1882
-- -- output ...
1883
-- -- {1,0}
1884
-- -- {2,1}
1885
-- -- {3,0}
1886
-- -- {4,1}
1887
-- -- {5,0}
1888
-- -- {6,1}
1889
-- -- {7,0}
1890
-- -- {8,1}
1891
-- -- {9,0}
1892
-- -- {10,1}
1893
--
1894
--
18952
18962
return (and_bits(test_integer, 1) = 0)
1897
end function
1898
1899
--**
1900
-- Test if the supplied Euphoria object is even or odd.
1901
--
1902
-- Parameters:
1903
-- # ##test_object## : any Euphoria object. The item to test.
1904
--
1905
-- Returns:
1906
-- An **object**,
1907
-- * If ##test_object## is an integer...
1908
-- ** 1 if its even.
1909
-- ** 0 if its odd.
1910
-- * Otherwise if ##test_object## is an atom this always returns 0
1911
-- * otherwise if ##test_object## is an sequence it tests each element recursively, returning a
1912
-- sequence of the same structure containing ones and zeros for each element. A
1913
-- 1 means that the element at this position was even otherwise it was odd.
1914
--
1915
-- Example 1:
1916
--
1917
-- for i = 1 to 5 do
1918
-- ? {i, is_even_obj(i)}
1919
-- end for
1920
-- -- output ...
1921
-- -- {1,0}
1922
-- -- {2,1}
1923
-- -- {3,0}
1924
-- -- {4,1}
1925
-- -- {5,0}
1926
--
1927
--
1928
-- Example 2:
1929
--
1930
-- ? is_even_obj(3.4) --> 0
1931
--
1932
--
1933
-- Example 3:
1934
--
1935
-- ? is_even_obj({{1,2,3}, {{4,5},6,{7,8}},9}) --> {{0,1,0},{{1,0},1,{0,1}},0}
1936
--
1937
--
19385
19395
if atom(test_object) then
19404
if integer(test_object) then
19413
return (and_bits(test_object, 1) = 0)
1942
end if
19431
return 0
1944
end if
19451
for i = 1 to length(test_object) do
19462
test_object[i] = is_even_obj(test_object[i])
19472
end for
1948
19491
return test_object
1950
end function
1951