tests: update tests related to default arguments
[nit.git] / lib / standard / math.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2004-2008 Jean Privat <jean@pryen.org>
4 #
5 # This file is free software, which comes along with NIT. This software is
6 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
7 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
8 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
9 # is kept unaltered, and a notification of the changes is added.
10 # You are allowed to redistribute it and sell it, alone or is a part of
11 # another product.
12
13 # Mathematical operations
14 module math is ldflags "-lm"
15
16 import kernel
17 import collection
18
19 in "C header" `{
20 #include <stdlib.h>
21 #include <math.h>
22 #include <time.h>
23 `}
24
25 redef class Int
26 # Returns a random `Int` in `[0 .. self[`.
27 fun rand: Int `{
28 return (long)(((double)self)*rand()/(RAND_MAX+1.0));
29 `}
30
31 # Returns the result of a binary AND operation on `self` and `i`
32 #
33 # assert 0x10.bin_and(0x01) == 0
34 fun bin_and(i: Int): Int `{ return self & i; `}
35
36 # Alias of `bin_and`
37 fun &(i: Int): Int do return bin_and(i)
38
39 # Returns the result of a binary OR operation on `self` and `i`
40 #
41 # assert 0x10.bin_or(0x01) == 0x11
42 fun bin_or(i: Int): Int `{ return self | i; `}
43
44 # Alias of `bin_or`
45 fun |(i: Int): Int do return bin_or(i)
46
47 # Returns the result of a binary XOR operation on `self` and `i`
48 #
49 # assert 0x101.bin_xor(0x110) == 0x11
50 fun bin_xor(i: Int): Int `{ return self ^ i; `}
51
52 # Alias of `bin_xor`
53 fun ^(i: Int): Int do return bin_xor(i)
54
55 # Returns the 1's complement of `self`
56 #
57 # assert 0x2F.bin_not == -48
58 fun bin_not: Int `{ return ~self; `}
59
60 # Alias of `bin_not`
61 fun ~: Int do return bin_not
62
63 # Returns the square root of `self`
64 #
65 # assert 16.sqrt == 4
66 fun sqrt: Int `{ return sqrt(self); `}
67
68 # Returns the greatest common divisor of `self` and `o`
69 #
70 # assert 54.gcd(24) == 6
71 # assert -54.gcd(-24) == 6
72 # assert 54.gcd(-24) == -6
73 # assert -54.gcd(24) == -6
74 # assert 12.gcd(6) == 6
75 fun gcd(o: Int): Int
76 do
77 if self < 0 then return -(-self).gcd(o)
78 if o < 0 then return -(self.gcd(-o))
79 if self == 0 or o == self then return o
80 if o == 0 then return self
81 if self.bin_and(1) == 0 then
82 if o.bin_and(1) == 1 then
83 return self.rshift(1).gcd(o)
84 else
85 return self.rshift(1).gcd(o.rshift(1)).lshift(1)
86 end
87 end
88 if o.bin_and(1) == 0 then return self.gcd(o.rshift(1))
89 if self > o then return (self - o).rshift(1).gcd(o)
90 return (o - self).rshift(1).gcd(self)
91 end
92
93 # Is `self` even ?
94 #
95 # assert 12.is_even
96 fun is_even: Bool do return self % 2 == 0
97
98 # Is `self` odd ?
99 #
100 # assert not 13.is_even
101 fun is_odd: Bool do return not is_even
102
103 # Is self a prime number ?
104 #
105 # assert 3.is_prime
106 # assert not 1.is_prime
107 # assert not 12.is_prime
108 fun is_prime: Bool
109 do
110 if self == 2 then
111 return true
112 else if self <= 1 or self.is_even then
113 return false
114 end
115 for i in [3..self.sqrt[ do
116 if self % i == 0 then return false
117 end
118 return true
119 end
120
121 # Returns the `self` raised to the power of `e`.
122 #
123 # assert 2 ** 3 == 8
124 fun **(e: Int): Int
125 do
126 return self.to_f.pow(e.to_f).to_i
127 end
128
129 # The factorial of `self` (aka `self!`)
130 #
131 # Returns `1 * 2 * 3 * ... * self-1 * self`
132 #
133 # assert 0.factorial == 1 # by convention for an empty product
134 # assert 1.factorial == 1
135 # assert 4.factorial == 24
136 # assert 9.factorial == 362880
137 fun factorial: Int
138 do
139 assert self >= 0
140 var res = 1
141 var n = self
142 while n > 0 do
143 res = res * n
144 n -= 1
145 end
146 return res
147 end
148 end
149
150 redef class Byte
151 # Returns the result of a binary AND operation on `self` and `i`
152 #
153 # assert 0x10.bin_and(0x01) == 0
154 fun bin_and(i: Byte): Byte `{ return self & i; `}
155
156 # Alias of `bin_and`
157 fun &(i: Byte): Byte do return bin_and(i)
158
159 # Returns the result of a binary OR operation on `self` and `i`
160 #
161 # assert 0x10.bin_or(0x01) == 0x11
162 fun bin_or(i: Byte): Byte `{ return self | i; `}
163
164 # Alias of `bin_or`
165 fun |(i: Byte): Byte do return bin_or(i)
166
167 # Returns the result of a binary XOR operation on `self` and `i`
168 #
169 # assert 0x101.bin_xor(0x110) == 0x11
170 fun bin_xor(i: Byte): Byte `{ return self ^ i; `}
171
172 # Alias of `bin_xor`
173 fun ^(i: Byte): Byte do return bin_xor(i)
174
175 # Returns the 1's complement of `self`
176 #
177 # assert 0x2F.bin_not == -48
178 fun bin_not: Byte `{ return ~self; `}
179
180 # Alias of `bin_not`
181 fun ~: Byte do return bin_not
182 end
183
184 redef class Float
185
186 # Returns the non-negative square root of `self`.
187 #
188 # assert 9.0.sqrt == 3.0
189 # #assert 3.0.sqrt == 1.732
190 # assert 1.0.sqrt == 1.0
191 # assert 0.0.sqrt == 0.0
192 fun sqrt: Float `{ return sqrt(self); `}
193
194 # Computes the cosine of `self` (expressed in radians).
195 #
196 # #assert pi.cos == -1.0
197 fun cos: Float `{ return cos(self); `}
198
199 # Computes the sine of `self` (expressed in radians).
200 #
201 # #assert pi.sin == 0.0
202 fun sin: Float `{ return sin(self); `}
203
204 # Computes the cosine of x (expressed in radians).
205 #
206 # #assert 0.0.tan == 0.0
207 fun tan: Float `{ return tan(self); `}
208
209 # Computes the arc cosine of `self`.
210 #
211 # #assert 0.0.acos == pi / 2.0
212 fun acos: Float `{ return acos(self); `}
213
214 # Computes the arc sine of `self`.
215 #
216 # #assert 1.0.asin == pi / 2.0
217 fun asin: Float `{ return asin(self); `}
218
219 # Computes the arc tangent of `self`.
220 #
221 # #assert 0.0.tan == 0.0
222 fun atan: Float `{ return atan(self); `}
223
224 # Returns the absolute value of `self`.
225 #
226 # assert 12.0.abs == 12.0
227 # assert (-34.56).abs == 34.56
228 # assert -34.56.abs == -34.56
229 fun abs: Float `{ return fabs(self); `}
230
231 # Returns `self` raised at `e` power.
232 #
233 # #assert 2.0.pow(0.0) == 1.0
234 # #assert 2.0.pow(3.0) == 8.0
235 # #assert 0.0.pow(9.0) == 0.0
236 fun pow(e: Float): Float `{ return pow(self, e); `}
237
238 # Natural logarithm of `self`.
239 #
240 # assert 0.0.log.is_inf == -1
241 # #assert 1.0.log == 0.0
242 fun log: Float `{ return log(self); `}
243
244 # Logarithm of `self` to base `base`.
245 #
246 # assert 100.0.log_base(10.0) == 2.0
247 # assert 256.0.log_base(2.0) == 8.0
248 fun log_base(base: Float): Float do return log/base.log
249
250 # Returns *e* raised to `self`.
251 fun exp: Float `{ return exp(self); `}
252
253 # assert 1.1.ceil == 2.0
254 # assert 1.9.ceil == 2.0
255 # assert 2.0.ceil == 2.0
256 # assert (-1.5).ceil == -1.0
257 fun ceil: Float `{ return ceil(self); `}
258
259 # assert 1.1.floor == 1.0
260 # assert 1.9.floor == 1.0
261 # assert 2.0.floor == 2.0
262 # assert (-1.5).floor == -2.0
263 fun floor: Float `{ return floor(self); `}
264
265 # Rounds the value of a float to its nearest integer value
266 #
267 # assert 1.67.round == 2.0
268 # assert 1.34.round == 1.0
269 # assert -1.34.round == -1.0
270 # assert -1.67.round == -2.0
271 fun round: Float `{ return round(self); `}
272
273 # Returns a random `Float` in `[0.0 .. self[`.
274 fun rand: Float `{ return ((self)*rand())/(RAND_MAX+1.0); `}
275
276 # Returns the euclidean distance from `b`.
277 fun hypot_with(b: Float): Float `{ return hypotf(self, b); `}
278
279 # Returns true is self is not a number.
280 #
281 # As `nan != nan`, `is_nan` should be used to test if a float is the special *not a number* value.
282 #
283 # ~~~
284 # assert nan != nan # By IEEE 754
285 # assert nan.is_nan
286 # assert not 10.0.is_nan
287 # ~~~
288 fun is_nan: Bool `{ return isnan(self); `}
289
290 # Is the float an infinite value
291 # this function returns:
292 #
293 # * 1 if self is positive infinity
294 # * -1 if self is negative infinity
295 # * 0 otherwise
296 #
297 # ~~~
298 # assert 10.0.is_inf == 0
299 # assert inf.is_inf == 1
300 # assert (-inf).is_inf == -1
301 # ~~~
302 fun is_inf: Int do
303 if native_is_inf then
304 if self < 0.0 then return -1
305 return 1
306 end
307 return 0
308 end
309
310 private fun native_is_inf: Bool `{ return isinf(self); `}
311
312 # Linear interpolation between `a` and `b` using `self` as weight
313 #
314 # ~~~
315 # assert 0.0.lerp(0.0, 128.0) == 0.0
316 # assert 0.5.lerp(0.0, 128.0) == 64.0
317 # assert 1.0.lerp(0.0, 128.0) == 128.0
318 # assert -0.5.lerp(0.0, 128.0) == -64.0
319 # ~~~
320 fun lerp(a, b: Float): Float do return (1.0 - self) * a + self * b
321 end
322
323 # Positive float infinite (IEEE 754)
324 #
325 # assert inf > 10.0
326 # assert inf.is_inf == 1
327 #
328 # `inf` follows the arithmetic of infinites
329 #
330 # assert (inf - 1.0) == inf
331 # assert (inf - inf).is_nan
332 #
333 # The negative infinite can be used as `-inf`.
334 #
335 # assert -inf < -10.0
336 # assert (-inf).is_inf == -1
337 fun inf: Float do return 1.0 / 0.0
338
339 # Not a Number, representation of an undefined or unrepresentable float (IEEE 754).
340 #
341 # `nan` is not comparable with itself, you should use `Float::is_nan` to test it.
342 #
343 # ~~~
344 # assert nan.is_nan
345 # assert nan != nan # By IEEE 754
346 # ~~~
347 #
348 # `nan` is the quiet result of some undefined operations.
349 #
350 # ~~~
351 # assert (1.0 + nan).is_nan
352 # assert (0.0 / 0.0).is_nan
353 # assert (inf - inf).is_nan
354 # assert (inf / inf).is_nan
355 # assert (-1.0).sqrt.is_nan
356 # ~~~
357 fun nan: Float do return 0.0 / 0.0
358
359 redef class Collection[ E ]
360 # Return a random element form the collection
361 # There must be at least one element in the collection
362 #
363 # ~~~
364 # var x = [1,2,3].rand
365 # assert x == 1 or x == 2 or x == 3
366 # ~~~
367 fun rand: E
368 do
369 if is_empty then abort
370 var rand_index = length.rand
371
372 for e in self do
373 if rand_index == 0 then return e
374 rand_index -= 1
375 end
376 abort
377 end
378
379 # Return a new array made of elements in a random order.
380 #
381 # ~~~
382 # var a = [1,2,1].to_shuffle
383 # assert a == [1,1,2] or a == [1,2,1] or a == [2,1,1]
384 # ~~~
385 fun to_shuffle: Array[E]
386 do
387 var res = self.to_a
388 res.shuffle
389 return res
390 end
391 end
392
393 redef class SequenceRead[E]
394 # Optimized for large collections using `[]`
395 redef fun rand
396 do
397 assert not is_empty
398 return self[length.rand]
399 end
400 end
401
402 redef class AbstractArray[E]
403 # Reorder randomly the elements in self.
404 #
405 # ~~~
406 # var a = new Array[Int]
407 #
408 # a.shuffle
409 # assert a.is_empty
410 #
411 # a.add 1
412 # a.shuffle
413 # assert a == [1]
414 #
415 # a.add 2
416 # a.shuffle
417 # assert a == [1,2] or a == [2,1]
418 # ~~~
419 #
420 # ENSURE self.shuffle.has_exactly(old(self))
421 fun shuffle
422 do
423 for i in [0..length[ do
424 var j = i + (length-i).rand
425 var tmp = self[i]
426 self[i] = self[j]
427 self[j] = tmp
428 end
429 end
430 end
431
432 redef class Sys
433 init
434 do
435 srand
436 end
437 end
438
439 # Computes the arc tangent given `x` and `y`.
440 #
441 # assert atan2(-0.0, 1.0) == -0.0
442 # assert atan2(0.0, 1.0) == 0.0
443 fun atan2(x: Float, y: Float): Float `{ return atan2(x, y); `}
444
445 # Approximate value of **pi**.
446 fun pi: Float do return 3.14159265
447
448 # Initialize the pseudo-random generator with the given seed.
449 # The pseudo-random generator is used by the method `rand` and other to generate sequence of numbers.
450 # These sequences are repeatable by calling `srand_from` with a same seed value.
451 #
452 # ~~~~
453 # srand_from(0)
454 # var a = 10.rand
455 # var b = 100.rand
456 # srand_from(0)
457 # assert 10.rand == a
458 # assert 100.rand == b
459 # ~~~~
460 fun srand_from(x: Int) `{ srand(x); `}
461
462 # Reinitialize the pseudo-random generator used by the method `rand` and other.
463 # This method is automatically invoked at the begin of the program, so usually, there is no need to manually invoke it.
464 # The only exception is in conjunction with `srand_from` to reset the pseudo-random generator.
465 fun srand `{ srand(time(NULL)); `}