Name | Executed | Routines | % | Executed | Lines | % | Unexecuted |
/home/matt/eu/rds/include/std/math.e | 38 | 38 | 100.00% | 266 | 286 | 93.01% | 20 |
Routine | Executed | Lines | Unexecuted | |
round() | 26 | 38 | 68.42% | 12 |
atan2() | 10 | 12 | 83.33% | 2 |
abs_below_1() | 7 | 8 | 87.50% | 1 |
approx() | 22 | 23 | 95.65% | 1 |
ensure_in_list() | 7 | 8 | 87.50% | 1 |
gcd() | 21 | 22 | 95.45% | 1 |
not_below_1() | 7 | 8 | 87.50% | 1 |
trig_range() | 7 | 8 | 87.50% | 1 |
abs() | 13 | 13 | 100.00% | 0 |
arccos() | 2 | 2 | 100.00% | 0 |
arccosh() | 2 | 2 | 100.00% | 0 |
arcsin() | 2 | 2 | 100.00% | 0 |
arcsinh() | 2 | 2 | 100.00% | 0 |
arctanh() | 2 | 2 | 100.00% | 0 |
ceil() | 2 | 2 | 100.00% | 0 |
cosh() | 2 | 2 | 100.00% | 0 |
deg2rad() | 2 | 2 | 100.00% | 0 |
ensure_in_range() | 8 | 8 | 100.00% | 0 |
exp() | 2 | 2 | 100.00% | 0 |
frac() | 3 | 3 | 100.00% | 0 |
intdiv() | 2 | 2 | 100.00% | 0 |
is_even() | 2 | 2 | 100.00% | 0 |
is_even_obj() | 9 | 9 | 100.00% | 0 |
log10() | 2 | 2 | 100.00% | 0 |
max() | 10 | 10 | 100.00% | 0 |
min() | 10 | 10 | 100.00% | 0 |
mod() | 4 | 4 | 100.00% | 0 |
or_all() | 10 | 10 | 100.00% | 0 |
powof2() | 2 | 2 | 100.00% | 0 |
product() | 10 | 10 | 100.00% | 0 |
rad2deg() | 2 | 2 | 100.00% | 0 |
rotate_bits() | 17 | 17 | 100.00% | 0 |
shift_bits() | 19 | 19 | 100.00% | 0 |
sign() | 2 | 2 | 100.00% | 0 |
sinh() | 2 | 2 | 100.00% | 0 |
sum() | 10 | 10 | 100.00% | 0 |
tanh() | 2 | 2 | 100.00% | 0 |
trunc() | 2 | 2 | 100.00% | 0 |
# | 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 | ||
14 | 6 | |
15 | -- values passed to arccos and arcsin must be [-1,+1] | |
16 | 6 | if atom(x) then |
17 | 4 | return x >= -1 and x <= 1 |
18 | else | |
19 | 2 | for i = 1 to length(x) do |
20 | 3 | if not trig_range(x[i]) then |
21 | 0 | return 0 |
22 | end if | |
23 | 3 | end for |
24 | 2 | 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 | ||
58 | 140 | |
59 | object t | |
60 | 140 | if atom(a) then |
61 | 115 | if a >= 0 then |
62 | 80 | return a |
63 | else | |
64 | 35 | return - a |
65 | end if | |
66 | end if | |
67 | 25 | for i = 1 to length(a) do |
68 | 78 | t = a[i] |
69 | 78 | if atom(t) then |
70 | 71 | if t < 0 then |
71 | 21 | a[i] = - t |
72 | end if | |
73 | else | |
74 | 7 | a[i] = abs(t) |
75 | end if | |
76 | 78 | end for |
77 | 25 | 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 | ||
109 | 53 | |
110 | -- small so normally it will be inlined | |
111 | 53 | 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 | ||
136 | 259 | |
137 | atom b, c | |
138 | 259 | if atom(a) then |
139 | 170 | return a |
140 | end if | |
141 | 89 | b = MINF |
142 | 89 | for i = 1 to length(a) do |
143 | 189 | c = max(a[i]) |
144 | 189 | if c > b then |
145 | 137 | b = c |
146 | end if | |
147 | 189 | end for |
148 | 89 | 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 | ||
169 | 79 | |
170 | atom b, c | |
171 | 79 | if atom(a) then |
172 | 54 | return a |
173 | end if | |
174 | 25 | b = PINF |
175 | 25 | for i = 1 to length(a) do |
176 | 53 | c = min(a[i]) |
177 | 53 | if c < b then |
178 | 50 | b = c |
179 | end if | |
180 | 53 | end for |
181 | 25 | 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 | ||
208 | 7 | |
209 | 7 | if length(range_limits) < 2 then |
210 | 2 | return item |
211 | end if | |
212 | ||
213 | 5 | if eu:compare(item, range_limits[1]) < 0 then |
214 | 1 | return range_limits[1] |
215 | end if | |
216 | 4 | if eu:compare(item, range_limits[$]) > 0 then |
217 | 1 | return range_limits[$] |
218 | end if | |
219 | 3 | 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 | ||
248 | 6 | |
249 | 6 | if length(list) = 0 then |
250 | 1 | return item |
251 | end if | |
252 | 5 | if find(item, list) = 0 then |
253 | 1 | if default>=1 and default<=length(list) then |
254 | 1 | return list[default] |
255 | else | |
256 | 0 | return list[1] |
257 | end if | |
258 | end if | |
259 | 4 | return item |
260 | end function | |
261 | ||
262 | --**** | |
263 | -- === Roundings and remainders | |
264 | -- | |
265 | ||
266 | --**** | |
267 | -- Signature: | |
268 | -- | |
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 | ||
377 | 10 | |
378 | 10 | if equal(sign(x), sign(y)) then |
379 | 5 | return remainder(x,y) |
380 | end if | |
381 | 5 | 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]] | |
414 | 3 | |
415 | 3 | 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 | ||
448 | 3 | |
449 | 3 | object temp = abs(x) |
450 | 3 | 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 | ||
477 | 8 | |
478 | 8 | return sign(a)*ceil(abs(a)/abs(b)) |
479 | end function | |
480 | ||
481 | --**** | |
482 | -- Signature: | |
483 | -- | |
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 | ||
532 | 15 | |
533 | 15 | 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 | ||
559 | 24 | |
560 | integer len | |
561 | sequence s | |
562 | object t, u | |
563 | ||
564 | 24 | precision = abs(precision) |
565 | 24 | if atom(a) then |
566 | 14 | if atom(precision) then |
567 | 14 | return floor(0.5 + (a * precision )) / precision |
568 | end if | |
569 | 0 | len = length(precision) |
570 | 0 | s = repeat(0, len) |
571 | 0 | for i = 1 to len do |
572 | 0 | t = precision[i] |
573 | 0 | if atom (t) then |
574 | 0 | s[i] = floor( 0.5 + (a * t)) / t |
575 | else | |
576 | 0 | s[i] = round(a, t) |
577 | end if | |
578 | 0 | end for |
579 | 0 | return s |
580 | 10 | elsif atom(precision) then |
581 | 9 | len = length(a) |
582 | 9 | s = repeat(0, len) |
583 | 9 | for i = 1 to len do |
584 | 17 | t = a[i] |
585 | 17 | if atom(t) then |
586 | 14 | s[i] = floor(0.5 + (t * precision)) / precision |
587 | else | |
588 | 3 | s[i] = round(t, precision) |
589 | end if | |
590 | 17 | end for |
591 | 9 | return s |
592 | end if | |
593 | 1 | len = length(a) |
594 | 1 | if len != length(precision) then |
595 | 0 | crash("The lengths of the two supplied sequences do not match.") |
596 | end if | |
597 | 1 | s = repeat(0, len) |
598 | 1 | for i = 1 to len do |
599 | 4 | t = precision[i] |
600 | 4 | if atom(t) then |
601 | 4 | u = a[i] |
602 | 4 | if atom(u) then |
603 | 4 | s[i] = floor(0.5 + (u * t)) / t |
604 | else | |
605 | 0 | s[i] = round(u, t) |
606 | end if | |
607 | else | |
608 | 0 | s[i] = round(a[i], t) |
609 | end if | |
610 | 4 | end for |
611 | 1 | return s |
612 | end function | |
613 | ||
614 | --**** | |
615 | -- === Trigonometry | |
616 | ||
617 | --**** | |
618 | -- Signature: | |
619 | -- | |
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 | -- | |
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 | -- | |
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 | -- | |
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 | ||
763 | 2 | |
764 | -- returns angle in radians | |
765 | 2 | 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 | ||
796 | 1 | |
797 | -- returns angle in radians | |
798 | 1 | 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 | ||
820 | 5 | |
821 | 5 | if x > 0 then |
822 | 2 | return arctan(y/x) |
823 | 3 | elsif x < 0 then |
824 | 2 | if y < 0 then |
825 | 1 | return arctan(y/x) - PI |
826 | else | |
827 | 1 | return arctan(y/x) + PI |
828 | end if | |
829 | 1 | elsif y > 0 then |
830 | 0 | return HALFPI |
831 | 1 | elsif y < 0 then |
832 | 0 | return -(HALFPI) |
833 | else | |
834 | 1 | 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 | ||
861 | 3 | |
862 | 3 | 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 | ||
886 | 7 | |
887 | 7 | return x * DEGREES_TO_RADIANS |
888 | end function | |
889 | ||
890 | --**** | |
891 | -- === Logarithms and powers. | |
892 | -- | |
893 | ||
894 | --**** | |
895 | -- Signature: | |
896 | -- | |
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 | ||
954 | 3 | |
955 | 3 | 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 | ||
980 | 10 | |
981 | 10 | return power(E, x) |
982 | end function | |
983 | ||
984 | --**** | |
985 | -- Signature: | |
986 | -- | |
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 | -- | |
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 | ||
1098 | 2 | |
1099 | 2 | 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 | ||
1126 | 2 | |
1127 | 2 | 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 | ||
1153 | 1 | |
1154 | 1 | 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 | ||
1178 | 1 | |
1179 | 1 | return log(a+sqrt(1+a*a)) |
1180 | end function | |
1181 | ||
1182 | 5 | |
1183 | 5 | if atom(x) then |
1184 | 3 | return x>=1.0 |
1185 | end if | |
1186 | 2 | for i=1 to length(x) do |
1187 | 3 | if not not_below_1(x[i]) then |
1188 | 0 | return 0 |
1189 | end if | |
1190 | 3 | end for |
1191 | 2 | 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 | ||
1218 | 2 | |
1219 | 2 | return log(a+sqrt(a*a-1)) |
1220 | end function | |
1221 | ||
1222 | 5 | |
1223 | 5 | if atom(x) then |
1224 | 3 | return x>-1.0 and x<1.0 |
1225 | end if | |
1226 | 2 | for i=1 to length(x) do |
1227 | 3 | if not abs_below_1(x[i]) then |
1228 | 0 | return 0 |
1229 | end if | |
1230 | 3 | end for |
1231 | 2 | 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 | ||
1258 | 2 | |
1259 | 2 | 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 | ||
1290 | 4 | |
1291 | atom b | |
1292 | 4 | if atom(a) then |
1293 | 1 | return a |
1294 | end if | |
1295 | 3 | b = 0 |
1296 | 3 | for i = 1 to length(a) do |
1297 | 9 | if atom(a[i]) then |
1298 | 8 | b += a[i] |
1299 | else | |
1300 | 1 | b += sum(a[i]) |
1301 | end if | |
1302 | 9 | end for |
1303 | 3 | 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 | ||
1330 | 6 | |
1331 | atom b | |
1332 | 6 | if atom(a) then |
1333 | 1 | return a |
1334 | end if | |
1335 | 5 | b = 1 |
1336 | 5 | for i = 1 to length(a) do |
1337 | 12 | if atom(a[i]) then |
1338 | 10 | b *= a[i] |
1339 | else | |
1340 | 2 | b *= product(a[i]) |
1341 | end if | |
1342 | 12 | end for |
1343 | 5 | 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 | ||
1369 | 22 | |
1370 | atom b | |
1371 | 22 | if atom(a) then |
1372 | 1 | return a |
1373 | end if | |
1374 | 21 | b = 0 |
1375 | 21 | for i = 1 to length(a) do |
1376 | 204 | if atom(a[i]) then |
1377 | 203 | b = or_bits(b, a[i]) |
1378 | else | |
1379 | 1 | b = or_bits(b, or_all(a[i])) |
1380 | end if | |
1381 | 204 | end for |
1382 | 21 | return b |
1383 | end function | |
1384 | ||
1385 | --**** | |
1386 | -- === Bitwise operations | |
1387 | -- | |
1388 | ||
1389 | --**** | |
1390 | -- Signature: | |
1391 | -- | |
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 | -- | |
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 | -- | |
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 | -- | |
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 | ||
1587 | 1177 | |
1588 | ||
1589 | 1177 | if sequence(source_number) then |
1590 | 1 | for i = 1 to length(source_number) do |
1591 | 6 | source_number[i] = shift_bits(source_number[i], shift_distance) |
1592 | 6 | end for |
1593 | 1 | return source_number |
1594 | end if | |
1595 | 1176 | source_number = and_bits(source_number, 0xFFFFFFFF) |
1596 | 1176 | if shift_distance = 0 then |
1597 | 5 | return source_number |
1598 | end if | |
1599 | ||
1600 | 1171 | if shift_distance < 0 then |
1601 | 583 | source_number *= power(2, -shift_distance) |
1602 | else | |
1603 | 588 | integer lSigned = 0 |
1604 | -- Check for the sign bit so we don't propagate it. | |
1605 | 588 | if and_bits(source_number, 0x80000000) then |
1606 | 302 | lSigned = 1 |
1607 | 302 | source_number = and_bits(source_number, 0x7FFFFFFF) |
1608 | end if | |
1609 | 588 | source_number /= power(2, shift_distance) |
1610 | 588 | if lSigned and shift_distance < 32 then |
1611 | -- Put back the sign bit now shifted | |
1612 | 302 | source_number = or_bits(source_number, power(2, 31-shift_distance)) |
1613 | end if | |
1614 | end if | |
1615 | ||
1616 | 1171 | 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 | ||
1659 | 601 | |
1660 | atom lTemp | |
1661 | atom lSave | |
1662 | integer lRest | |
1663 | ||
1664 | 601 | if sequence(source_number) then |
1665 | 1 | for i = 1 to length(source_number) do |
1666 | 6 | source_number[i] = rotate_bits(source_number[i], shift_distance) |
1667 | 6 | end for |
1668 | 1 | return source_number |
1669 | end if | |
1670 | ||
1671 | 600 | source_number = and_bits(source_number, 0xFFFFFFFF) |
1672 | 600 | if shift_distance = 0 then |
1673 | 22 | return source_number |
1674 | end if | |
1675 | ||
1676 | 578 | if shift_distance < 0 then |
1677 | 5 | lSave = not_bits(power(2, 32 + shift_distance) - 1) |
1678 | 5 | lRest = 32 + shift_distance |
1679 | else | |
1680 | 573 | lSave = power(2, shift_distance) - 1 |
1681 | 573 | lRest = shift_distance - 32 |
1682 | end if | |
1683 | ||
1684 | 578 | lTemp = shift_bits(and_bits(source_number, lSave), lRest) |
1685 | 578 | source_number = shift_bits(source_number, shift_distance) |
1686 | 578 | 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 | ||
1718 | 2 | |
1719 | atom r | |
1720 | ||
1721 | 2 | if p<0 then |
1722 | 1 | p=floor(-p) |
1723 | else | |
1724 | 1 | p=floor(p) |
1725 | end if | |
1726 | 2 | if q<0 then |
1727 | 1 | q=floor(-q) |
1728 | else | |
1729 | 1 | q=floor(q) |
1730 | end if | |
1731 | 2 | if p |
1732 | 1 | r=p |
1733 | 1 | p=q |
1734 | 1 | q=r |
1735 | end if | |
1736 | 2 | if q<=1 then |
1737 | 1 | return q |
1738 | end if | |
1739 | ||
1740 | 1 | while 1 do |
1741 | 9 | r=remainder(p,q) |
1742 | 9 | if r=1 then |
1743 | 0 | return r |
1744 | 9 | elsif r=0 then |
1745 | 1 | return q |
1746 | else | |
1747 | 8 | p=q |
1748 | 8 | q=r |
1749 | end if | |
1750 | 8 | 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 | -- | |
1796 | 22 | |
1797 | ||
1798 | 22 | if sequence(p) then |
1799 | 3 | if sequence(q) then |
1800 | 1 | if length(p) != length(q) then |
1801 | 0 | crash("approx(): Sequence arguments must be the same length") |
1802 | end if | |
1803 | 1 | for i = 1 to length(p) do |
1804 | 4 | p[i] = approx(p[i], q[i]) |
1805 | 4 | end for |
1806 | 1 | return p |
1807 | else | |
1808 | 2 | for i = 1 to length(p) do |
1809 | 6 | p[i] = approx(p[i], q) |
1810 | 6 | end for |
1811 | 2 | return p |
1812 | end if | |
1813 | 19 | elsif sequence(q) then |
1814 | 1 | for i = 1 to length(q) do |
1815 | 4 | q[i] = approx(p, q[i]) |
1816 | 4 | end for |
1817 | 1 | return q |
1818 | else | |
1819 | 18 | if p > (q + epsilon) then |
1820 | 5 | return 1 |
1821 | end if | |
1822 | ||
1823 | 13 | if p < (q - epsilon) then |
1824 | 4 | return -1 |
1825 | end if | |
1826 | ||
1827 | 9 | 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 | ||
1861 | 21 | |
1862 | 21 | 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 | -- | |
1895 | 2 | |
1896 | 2 | 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 | -- | |
1938 | 5 | |
1939 | 5 | if atom(test_object) then |
1940 | 4 | if integer(test_object) then |
1941 | 3 | return (and_bits(test_object, 1) = 0) |
1942 | end if | |
1943 | 1 | return 0 |
1944 | end if | |
1945 | 1 | for i = 1 to length(test_object) do |
1946 | 2 | test_object[i] = is_even_obj(test_object[i]) |
1947 | 2 | end for |
1948 | ||
1949 | 1 | return test_object |
1950 | end function | |
1951 |