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
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; `}
36 # Returns the result of a binary OR operation on `self` and `i
`
38 # assert 0x10.bin_or(0x01) == 0x11
39 fun bin_or(i: Int): Int `{ return self | i; `}
41 # Returns the result of a binary XOR operation on `self` and `i`
43 # assert 0x101.bin_xor(0x110) == 0x11
44 fun bin_xor
(i
: Int): Int `{ return self ^ i; `}
46 # Returns the 1's complement of `self`
48 # assert 0x2F.bin_not == -48
49 fun bin_not: Int `{ return ~self; `}
51 # Returns the square root of `self`
54 fun sqrt
: Int `{ return sqrt(self); `}
56 # Returns the greatest common divisor of `self` and `o
`
58 # assert 54.gcd(24) == 6
59 # assert -54.gcd(-24) == 6
60 # assert 54.gcd(-24) == -6
61 # assert -54.gcd(24) == -6
62 # assert 12.gcd(6) == 6
65 if self < 0 then return -(-self).gcd(o)
66 if o < 0 then return -(self.gcd(-o))
67 if self == 0 or o == self then return o
68 if o == 0 then return self
69 if self.bin_and(1) == 0 then
70 if o.bin_and(1) == 1 then
71 return self.rshift(1).gcd(o)
73 return self.rshift(1).gcd(o.rshift(1)).lshift(1)
76 if o.bin_and(1) == 0 then return self.gcd(o.rshift(1))
77 if self > o then return (self - o).rshift(1).gcd(o)
78 return (o - self).rshift(1).gcd(self)
84 fun is_even: Bool do return self % 2 == 0
88 # assert not 13.is_even
89 fun is_odd: Bool do return not is_even
91 # Returns the `self` raised to the power of `e
`.
96 return self.to_f.pow(e.to_f).to_i
99 # The factorial of `self` (aka `self!`)
101 # Returns `1 * 2 * 3 * ... * self-1
* self`
103 # assert 0.factorial == 1 # by convention for an empty product
104 # assert 1.factorial == 1
105 # assert 4.factorial == 24
106 # assert 9.factorial == 362880
122 # Returns the non-negative square root of `self`.
124 # assert 9.0.sqrt == 3.0
125 # #assert 3.0.sqrt == 1.732
126 # assert 1.0.sqrt == 1.0
127 # assert 0.0.sqrt == 0.0
128 fun sqrt: Float `{ return sqrt(self); `}
130 # Computes the cosine of `self` (expressed in radians).
132 # #assert pi.cos == -1.0
133 fun cos
: Float `{ return cos(self); `}
135 # Computes the sine of `self` (expressed in radians).
137 # #assert pi.sin == 0.0
138 fun sin: Float `{ return sin(self); `}
140 # Computes the cosine of x (expressed in radians).
142 # #assert 0.0.tan == 0.0
143 fun tan
: Float `{ return tan(self); `}
145 # Computes the arc cosine of `self`.
147 # #assert 0.0.acos == pi / 2.0
148 fun acos: Float `{ return acos(self); `}
150 # Computes the arc sine of `self`.
152 # #assert 1.0.asin == pi / 2.0
153 fun asin
: Float `{ return asin(self); `}
155 # Computes the arc tangent of `self`.
157 # #assert 0.0.tan == 0.0
158 fun atan: Float `{ return atan(self); `}
160 # Returns the absolute value of `self`.
162 # assert 12.0.abs == 12.0
163 # assert (-34.56).abs == 34.56
164 # assert -34.56.abs == -34.56
165 fun abs
: Float `{ return fabs(self); `}
167 # Returns `self` raised at `e
` power.
169 # #assert 2.0.pow(0.0) == 1.0
170 # #assert 2.0.pow(3.0) == 8.0
171 # #assert 0.0.pow(9.0) == 0.0
172 fun pow(e: Float): Float `{ return pow(self, e); `}
174 # Natural logarithm of `self`.
176 # assert 0.0.log.is_inf == -1
177 # #assert 1.0.log == 0.0
178 fun log
: Float `{ return log(self); `}
180 # Logarithm of `self` to base `base
`.
182 # assert 100.0.log_base(10.0) == 2.0
183 # assert 256.0.log_base(2.0) == 8.0
184 fun log_base(base: Float): Float do return log/base.log
186 # Returns *e* raised to `self`.
187 fun exp: Float `{ return exp(self); `}
189 # assert 1.1.ceil == 2.0
190 # assert 1.9.ceil == 2.0
191 # assert 2.0.ceil == 2.0
192 # assert (-1.5).ceil == -1.0
193 fun ceil
: Float `{ return ceil(self); `}
195 # assert 1.1.floor == 1.0
196 # assert 1.9.floor == 1.0
197 # assert 2.0.floor == 2.0
198 # assert (-1.5).floor == -2.0
199 fun floor: Float `{ return floor(self); `}
201 # Rounds the value of a float to its nearest integer value
203 # assert 1.67.round == 2.0
204 # assert 1.34.round == 1.0
205 # assert -1.34.round == -1.0
206 # assert -1.67.round == -2.0
207 fun round
: Float `{ return round(self); `}
209 # Returns a random `Float` in `[0.0 .. self[`.
210 fun rand: Float `{ return ((self)*rand())/(RAND_MAX+1.0); `}
212 # Returns the euclidean distance from `b`.
213 fun hypot_with
(b
: Float): Float `{ return hypotf(self, b); `}
215 # Returns true is self is not a number.
216 fun is_nan: Bool `{ return isnan(self); `}
218 # Is the float an infinite value
219 # this function returns:
221 # * 1 if self is positive infinity
222 # * -1 if self is negative infinity
225 if native_is_inf
then
226 if self < 0.0 then return -1
232 private fun native_is_inf
: Bool `{ return isinf(self); `}
234 # Linear interpolation between `a
` and `b
` using `self` as weight
237 # assert 0.0.lerp(0.0, 128.0) == 0.0
238 # assert 0.5.lerp(0.0, 128.0) == 64.0
239 # assert 1.0.lerp(0.0, 128.0) == 128.0
240 # assert -0.5.lerp(0.0, 128.0) == -64.0
242 fun lerp(a, b: Float): Float do return (1.0 - self) * a + self * b
245 redef class Collection[ E ]
246 # Return a random element form the collection
247 # There must be at least one element in the collection
250 # var x = [1,2,3].rand
251 # assert x == 1 or x == 2 or x == 3
255 if is_empty then abort
256 var rand_index = length.rand
259 if rand_index == 0 then return e
265 # Return a new array made of elements in a random order.
268 # var a = [1,2,1].to_shuffle
269 # assert a == [1,1,2] or a == [1,2,1] or a == [2,1,1]
271 fun to_shuffle: Array[E]
279 redef class SequenceRead[E]
280 # Optimized for large collections using `[]`
284 return self[length.rand]
288 redef class AbstractArray[E]
289 # Reorder randomly the elements in self.
292 # var a = new Array[Int]
303 # assert a == [1,2] or a == [2,1]
306 # ENSURE self.shuffle.has_exactly(old(self))
309 for i in [0..length[ do
310 var j = i + (length-i).rand
325 # Computes the arc tangent given `x
` and `y
`.
327 # assert atan2(-0.0, 1.0) == -0.0
328 # assert atan2(0.0, 1.0) == 0.0
329 fun atan2(x: Float, y: Float): Float `{ return atan2(x, y); `}
331 # Approximate value of **pi**.
332 fun pi
: Float do return 3.14159265
334 # Initialize the pseudo-random generator with the given seed.
335 # The pseudo-random generator is used by the method `rand` and other to generate sequence of numbers.
336 # These sequences are repeatable by calling `srand_from` with a same seed value.
343 # assert 10.rand == a
344 # assert 100.rand == b
346 fun srand_from
(x
: Int) `{ srand(x); `}
348 # Reinitialize the pseudo-random generator used by the method `rand
` and other.
349 # This method is automatically invoked at the begin of the program, so usually, there is no need to manually invoke it.
350 # The only exception is in conjunction with `srand_from
` to reset the pseudo-random generator.
351 fun srand `{ srand(time(NULL)); `}