1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2004-2008 Jean Privat <jean@pryen.org>
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
13 # Mathematical operations
14 module math
is ldflags
"-lm"
26 # Returns a random `Int` in `[0 .. self[`.
28 return (long)(((double)self)*rand()/(RAND_MAX+1.0));
31 # Returns the result of a binary AND operation on `self` and `i`
33 # assert 0x10.bin_and(0x01) == 0
34 fun bin_and
(i
: Int): Int `{ return self & i; `}
37 fun &(i: Int): Int do return bin_and(i)
39 # Returns the result of a binary OR operation on `self` and `i
`
41 # assert 0x10.bin_or(0x01) == 0x11
42 fun bin_or(i: Int): Int `{ return self | i; `}
45 fun |(i
: Int): Int do return bin_or
(i
)
47 # Returns the result of a binary XOR operation on `self` and `i`
49 # assert 0x101.bin_xor(0x110) == 0x11
50 fun bin_xor
(i
: Int): Int `{ return self ^ i; `}
53 fun ^(i: Int): Int do return bin_xor(i)
55 # Returns the 1's complement of `self`
57 # assert 0x2F.bin_not == -48
58 fun bin_not: Int `{ return ~self; `}
61 fun ~
: Int do return bin_not
63 # Returns the square root of `self`
66 fun sqrt
: Int `{ return sqrt(self); `}
68 # Returns the greatest common divisor of `self` and `o
`
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
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)
85 return self.rshift(1).gcd(o.rshift(1)).lshift(1)
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)
96 fun is_even: Bool do return self % 2 == 0
100 # assert not 13.is_even
101 fun is_odd: Bool do return not is_even
103 # Is self a prime number ?
106 # assert not 1.is_prime
107 # assert not 12.is_prime
112 else if self <= 1 or self.is_even then
115 for i in [3..self.sqrt[ do
116 if self % i == 0 then return false
121 # Returns the `self` raised to the power of `e
`.
126 return self.to_f.pow(e.to_f).to_i
129 # The factorial of `self` (aka `self!`)
131 # Returns `1 * 2 * 3 * ... * self-1
* self`
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
151 # Returns the result of a binary AND operation on `self` and `i
`
153 # assert 0x10.bin_and(0x01) == 0
154 fun bin_and(i: Byte): Byte `{ return self & i; `}
157 fun &(i
: Byte): Byte do return bin_and
(i
)
159 # Returns the result of a binary OR operation on `self` and `i`
161 # assert 0x10.bin_or(0x01) == 0x11
162 fun bin_or
(i
: Byte): Byte `{ return self | i; `}
165 fun |(i: Byte): Byte do return bin_or(i)
167 # Returns the result of a binary XOR operation on `self` and `i
`
169 # assert 0x101.bin_xor(0x110) == 0x11
170 fun bin_xor(i: Byte): Byte `{ return self ^ i; `}
173 fun ^
(i
: Byte): Byte do return bin_xor
(i
)
175 # Returns the 1's complement of `self`
177 # assert 0x2F.bin_not == -48
178 fun bin_not
: Byte `{ return ~self; `}
181 fun ~: Byte do return bin_not
186 # Returns the non-negative square root of `self`.
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); `}
194 # Computes the cosine of `self` (expressed in radians).
196 # #assert pi.cos == -1.0
197 fun cos
: Float `{ return cos(self); `}
199 # Computes the sine of `self` (expressed in radians).
201 # #assert pi.sin == 0.0
202 fun sin: Float `{ return sin(self); `}
204 # Computes the cosine of x (expressed in radians).
206 # #assert 0.0.tan == 0.0
207 fun tan
: Float `{ return tan(self); `}
209 # Computes the arc cosine of `self`.
211 # #assert 0.0.acos == pi / 2.0
212 fun acos: Float `{ return acos(self); `}
214 # Computes the arc sine of `self`.
216 # #assert 1.0.asin == pi / 2.0
217 fun asin
: Float `{ return asin(self); `}
219 # Computes the arc tangent of `self`.
221 # #assert 0.0.tan == 0.0
222 fun atan: Float `{ return atan(self); `}
224 # Returns the absolute value of `self`.
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); `}
231 # Returns `self` raised at `e
` power.
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); `}
238 # Natural logarithm of `self`.
240 # assert 0.0.log.is_inf == -1
241 # #assert 1.0.log == 0.0
242 fun log
: Float `{ return log(self); `}
244 # Logarithm of `self` to base `base
`.
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
250 # Returns *e* raised to `self`.
251 fun exp: Float `{ return exp(self); `}
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); `}
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); `}
265 # Rounds the value of a float to its nearest integer value
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); `}
273 # Returns a random `Float` in `[0.0 .. self[`.
274 fun rand: Float `{ return ((self)*rand())/(RAND_MAX+1.0); `}
276 # Returns the euclidean distance from `b`.
277 fun hypot_with
(b
: Float): Float `{ return hypotf(self, b); `}
279 # Returns true is self is not a number.
280 fun is_nan: Bool `{ return isnan(self); `}
282 # Is the float an infinite value
283 # this function returns:
285 # * 1 if self is positive infinity
286 # * -1 if self is negative infinity
289 if native_is_inf
then
290 if self < 0.0 then return -1
296 private fun native_is_inf
: Bool `{ return isinf(self); `}
298 # Linear interpolation between `a
` and `b
` using `self` as weight
301 # assert 0.0.lerp(0.0, 128.0) == 0.0
302 # assert 0.5.lerp(0.0, 128.0) == 64.0
303 # assert 1.0.lerp(0.0, 128.0) == 128.0
304 # assert -0.5.lerp(0.0, 128.0) == -64.0
306 fun lerp(a, b: Float): Float do return (1.0 - self) * a + self * b
309 redef class Collection[ E ]
310 # Return a random element form the collection
311 # There must be at least one element in the collection
314 # var x = [1,2,3].rand
315 # assert x == 1 or x == 2 or x == 3
319 if is_empty then abort
320 var rand_index = length.rand
323 if rand_index == 0 then return e
329 # Return a new array made of elements in a random order.
332 # var a = [1,2,1].to_shuffle
333 # assert a == [1,1,2] or a == [1,2,1] or a == [2,1,1]
335 fun to_shuffle: Array[E]
343 redef class SequenceRead[E]
344 # Optimized for large collections using `[]`
348 return self[length.rand]
352 redef class AbstractArray[E]
353 # Reorder randomly the elements in self.
356 # var a = new Array[Int]
367 # assert a == [1,2] or a == [2,1]
370 # ENSURE self.shuffle.has_exactly(old(self))
373 for i in [0..length[ do
374 var j = i + (length-i).rand
389 # Computes the arc tangent given `x
` and `y
`.
391 # assert atan2(-0.0, 1.0) == -0.0
392 # assert atan2(0.0, 1.0) == 0.0
393 fun atan2(x: Float, y: Float): Float `{ return atan2(x, y); `}
395 # Approximate value of **pi**.
396 fun pi
: Float do return 3.14159265
398 # Initialize the pseudo-random generator with the given seed.
399 # The pseudo-random generator is used by the method `rand` and other to generate sequence of numbers.
400 # These sequences are repeatable by calling `srand_from` with a same seed value.
407 # assert 10.rand == a
408 # assert 100.rand == b
410 fun srand_from
(x
: Int) `{ srand(x); `}
412 # Reinitialize the pseudo-random generator used by the method `rand
` and other.
413 # This method is automatically invoked at the begin of the program, so usually, there is no need to manually invoke it.
414 # The only exception is in conjunction with `srand_from
` to reset the pseudo-random generator.
415 fun srand `{ srand(time(NULL)); `}