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
24 # Returns a random `Int` in `[0 .. self[`.
25 fun rand
: Int is extern "kernel_Int_Int_rand_0"
27 # Returns the result of a binary AND operation on `self` and `i`
29 # assert 0x10.bin_and(0x01) == 0
30 fun bin_and
(i
: Int): Int is extern "kernel_Int_Int_binand_0"
32 # Returns the result of a binary OR operation on `self` and `i`
34 # assert 0x10.bin_or(0x01) == 0x11
35 fun bin_or
(i
: Int): Int is extern "kernel_Int_Int_binor_0"
37 # Returns the result of a binary XOR operation on `self` and `i`
39 # assert 0x101.bin_xor(0x110) == 0x11
40 fun bin_xor
(i
: Int): Int is extern "kernel_Int_Int_binxor_0"
42 # Returns the 1's complement of `self`
44 # assert 0x2F.bin_not == -48
45 fun bin_not
: Int is extern "kernel_Int_Int_binnot_0"
47 # Returns the square root of `self`
50 fun sqrt
: Int `{ return sqrt(self); `}
52 # Returns the greatest common divisor of `self` and `o
`
54 # assert 54.gcd(24) == 6
55 # assert -54.gcd(-24) == 6
56 # assert 54.gcd(-24) == -6
57 # assert -54.gcd(24) == -6
58 # assert 12.gcd(6) == 6
61 if self < 0 then return -(-self).gcd(o)
62 if o < 0 then return -(self.gcd(-o))
63 if self == 0 or o == self then return o
64 if o == 0 then return self
65 if self.bin_and(1) == 0 then
66 if o.bin_and(1) == 1 then
67 return self.rshift(1).gcd(o)
69 return self.rshift(1).gcd(o.rshift(1)).lshift(1)
72 if o.bin_and(1) == 0 then return self.gcd(o.rshift(1))
73 if self > o then return (self - o).rshift(1).gcd(o)
74 return (o - self).rshift(1).gcd(self)
80 fun is_even: Bool do return self % 2 == 0
84 # assert not 13.is_even
85 fun is_odd: Bool do return not is_even
87 # Returns the `self` raised to the power of `e
`.
92 return self.to_f.pow(e.to_f).to_i
95 # The factorial of `self` (aka `self!`)
97 # Returns `1 * 2 * 3 * ... * self-1
* self`
99 # assert 0.factorial == 1 # by convention for an empty product
100 # assert 1.factorial == 1
101 # assert 4.factorial == 24
102 # assert 9.factorial == 362880
118 # Returns the non-negative square root of `self`.
120 # assert 9.0.sqrt == 3.0
121 # #assert 3.0.sqrt == 1.732
122 # assert 1.0.sqrt == 1.0
123 # assert 0.0.sqrt == 0.0
124 fun sqrt: Float is extern "kernel_Float_Float_sqrt_0"
126 # Computes the cosine of `self` (expressed in radians).
128 # #assert pi.cos == -1.0
129 fun cos: Float is extern "kernel_Float_Float_cos_0"
131 # Computes the sine of `self` (expressed in radians).
133 # #assert pi.sin == 0.0
134 fun sin: Float is extern "kernel_Float_Float_sin_0"
136 # Computes the cosine of x (expressed in radians).
138 # #assert 0.0.tan == 0.0
139 fun tan: Float is extern "kernel_Float_Float_tan_0"
141 # Computes the arc cosine of `self`.
143 # #assert 0.0.acos == pi / 2.0
144 fun acos: Float is extern "kernel_Float_Float_acos_0"
146 # Computes the arc sine of `self`.
148 # #assert 1.0.asin == pi / 2.0
149 fun asin: Float is extern "kernel_Float_Float_asin_0"
151 # Computes the arc tangent of `self`.
153 # #assert 0.0.tan == 0.0
154 fun atan: Float is extern "kernel_Float_Float_atan_0"
156 # Returns the absolute value of `self`.
158 # assert 12.0.abs == 12.0
159 # assert (-34.56).abs == 34.56
160 # assert -34.56.abs == -34.56
161 fun abs: Float `{ return fabs(self); `}
163 # Returns `self` raised at `e` power.
165 # #assert 2.0.pow(0.0) == 1.0
166 # #assert 2.0.pow(3.0) == 8.0
167 # #assert 0.0.pow(9.0) == 0.0
168 fun pow
(e
: Float): Float is extern "kernel_Float_Float_pow_1"
170 # Natural logarithm of `self`.
172 # assert 0.0.log.is_inf == -1
173 # #assert 1.0.log == 0.0
174 fun log
: Float is extern "kernel_Float_Float_log_0"
176 # Logarithm of `self` to base `base`.
178 # assert 100.0.log_base(10.0) == 2.0
179 # assert 256.0.log_base(2.0) == 8.0
180 fun log_base
(base
: Float): Float do return log
/base
.log
182 # Returns *e* raised to `self`.
183 fun exp
: Float is extern "kernel_Float_Float_exp_0"
185 # assert 1.1.ceil == 2.0
186 # assert 1.9.ceil == 2.0
187 # assert 2.0.ceil == 2.0
188 # assert (-1.5).ceil == -1.0
189 fun ceil
: Float `{ return ceil(self); `}
191 # assert 1.1.floor == 1.0
192 # assert 1.9.floor == 1.0
193 # assert 2.0.floor == 2.0
194 # assert (-1.5).floor == -2.0
195 fun floor: Float `{ return floor(self); `}
197 # Rounds the value of a float to its nearest integer value
199 # assert 1.67.round == 2.0
200 # assert 1.34.round == 1.0
201 # assert -1.34.round == -1.0
202 # assert -1.67.round == -2.0
203 fun round
: Float is extern "round"
205 # Returns a random `Float` in `[0.0 .. self[`.
206 fun rand
: Float is extern "kernel_Float_Float_rand_0"
208 # Returns the euclidean distance from `b`.
209 fun hypot_with
(b
: Float): Float is extern "hypotf"
211 # Returns true is self is not a number.
212 fun is_nan
: Bool is extern "isnan"
214 # Is the float an infinite value
215 # this function returns:
217 # * 1 if self is positive infinity
218 # * -1 if self is negative infinity
221 if is_inf_extern
then
222 if self < 0.0 then return -1
228 private fun is_inf_extern
: Bool is extern "isinf"
230 # Linear interpolation between `a` and `b` using `self` as weight
233 # assert 0.0.lerp(0.0, 128.0) == 0.0
234 # assert 0.5.lerp(0.0, 128.0) == 64.0
235 # assert 1.0.lerp(0.0, 128.0) == 128.0
236 # assert -0.5.lerp(0.0, 128.0) == -64.0
238 fun lerp
(a
, b
: Float): Float do return (1.0 - self) * a
+ self * b
241 redef class Collection[ E
]
242 # Return a random element form the collection
243 # There must be at least one element in the collection
246 # var x = [1,2,3].rand
247 # assert x == 1 or x == 2 or x == 3
251 if is_empty
then abort
252 var rand_index
= length
.rand
255 if rand_index
== 0 then return e
261 # Return a new array made of elements in a random order.
264 # var a = [1,2,1].to_shuffle
265 # assert a == [1,1,2] or a == [1,2,1] or a == [2,1,1]
267 fun to_shuffle
: Array[E
]
275 redef class SequenceRead[E
]
276 # Optimized for large collections using `[]`
280 return self[length
.rand
]
284 redef class AbstractArray[E
]
285 # Reorder randomly the elements in self.
288 # var a = new Array[Int]
299 # assert a == [1,2] or a == [2,1]
302 # ENSURE self.shuffle.has_exactly(old(self))
305 for i
in [0..length
[ do
306 var j
= i
+ (length-i
).rand
321 # Computes the arc tangent given `x` and `y`.
323 # assert atan2(-0.0, 1.0) == -0.0
324 # assert atan2(0.0, 1.0) == 0.0
325 fun atan2
(x
: Float, y
: Float): Float is extern "kernel_Any_Any_atan2_2"
327 # Approximate value of **pi**.
328 fun pi
: Float is extern "kernel_Any_Any_pi_0"
330 # Initialize the pseudo-random generator with the given seed.
331 # The pseudo-random generator is used by the method `rand` and other to generate sequence of numbers.
332 # These sequences are repeatable by calling `srand_from` with a same seed value.
339 # assert 10.rand == a
340 # assert 100.rand == b
342 fun srand_from
(x
: Int) is extern "kernel_Any_Any_srand_from_1"
344 # Reinitialize the pseudo-random generator used by the method `rand` and other.
345 # This method is automatically invoked at the begin of the program, so usually, there is no need to manually invoke it.
346 # The only exception is in conjunction with `srand_from` to reset the pseudo-random generator.
347 fun srand
is extern "kernel_Any_Any_srand_0"