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"
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"
33 fun &(i
: Int): Int do return bin_and
(i
)
35 # Returns the result of a binary OR operation on `self` and `i`
37 # assert 0x10.bin_or(0x01) == 0x11
38 fun bin_or
(i
: Int): Int is extern "kernel_Int_Int_binor_0"
41 fun |(i
: Int): Int do return bin_or
(i
)
43 # Returns the result of a binary XOR operation on `self` and `i`
45 # assert 0x101.bin_xor(0x110) == 0x11
46 fun bin_xor
(i
: Int): Int is extern "kernel_Int_Int_binxor_0"
49 fun ^
(i
: Int): Int do return bin_xor
(i
)
51 # Returns the 1's complement of `self`
53 # assert 0x2F.bin_not == -48
54 fun bin_not
: Int is extern "kernel_Int_Int_binnot_0"
57 fun ~
: Int do return bin_not
59 # Returns the square root of `self`
62 fun sqrt
: Int `{ return sqrt(self); `}
64 # Returns the greatest common divisor of `self` and `o
`
66 # assert 54.gcd(24) == 6
67 # assert -54.gcd(-24) == 6
68 # assert 54.gcd(-24) == -6
69 # assert -54.gcd(24) == -6
70 # assert 12.gcd(6) == 6
73 if self < 0 then return -(-self).gcd(o)
74 if o < 0 then return -(self.gcd(-o))
75 if self == 0 or o == self then return o
76 if o == 0 then return self
77 if self.bin_and(1) == 0 then
78 if o.bin_and(1) == 1 then
79 return self.rshift(1).gcd(o)
81 return self.rshift(1).gcd(o.rshift(1)).lshift(1)
84 if o.bin_and(1) == 0 then return self.gcd(o.rshift(1))
85 if self > o then return (self - o).rshift(1).gcd(o)
86 return (o - self).rshift(1).gcd(self)
92 fun is_even: Bool do return self % 2 == 0
96 # assert not 13.is_even
97 fun is_odd: Bool do return not is_even
99 # Is self a prime number ?
102 # assert not 1.is_prime
103 # assert not 12.is_prime
108 else if self <= 1 or self.is_even then
111 for i in [3..self.sqrt[ do
112 if self % i == 0 then return false
117 # Returns the `self` raised to the power of `e
`.
122 return self.to_f.pow(e.to_f).to_i
125 # The factorial of `self` (aka `self!`)
127 # Returns `1 * 2 * 3 * ... * self-1
* self`
129 # assert 0.factorial == 1 # by convention for an empty product
130 # assert 1.factorial == 1
131 # assert 4.factorial == 24
132 # assert 9.factorial == 362880
147 # Returns the result of a binary AND operation on `self` and `i
`
149 # assert 0x10.bin_and(0x01) == 0
150 fun bin_and(i: Byte): Byte `{ return self & i; `}
153 fun &(i
: Byte): Byte do return bin_and
(i
)
155 # Returns the result of a binary OR operation on `self` and `i`
157 # assert 0x10.bin_or(0x01) == 0x11
158 fun bin_or
(i
: Byte): Byte `{ return self | i; `}
161 fun |(i: Byte): Byte do return bin_or(i)
163 # Returns the result of a binary XOR operation on `self` and `i
`
165 # assert 0x101.bin_xor(0x110) == 0x11
166 fun bin_xor(i: Byte): Byte `{ return self ^ i; `}
169 fun ^
(i
: Byte): Byte do return bin_xor
(i
)
171 # Returns the 1's complement of `self`
173 # assert 0x2F.bin_not == -48
174 fun bin_not
: Byte `{ return ~self; `}
177 fun ~: Byte do return bin_not
182 # Returns the non-negative square root of `self`.
184 # assert 9.0.sqrt == 3.0
185 # #assert 3.0.sqrt == 1.732
186 # assert 1.0.sqrt == 1.0
187 # assert 0.0.sqrt == 0.0
188 fun sqrt: Float is extern "kernel_Float_Float_sqrt_0"
190 # Computes the cosine of `self` (expressed in radians).
192 # #assert pi.cos == -1.0
193 fun cos: Float is extern "kernel_Float_Float_cos_0"
195 # Computes the sine of `self` (expressed in radians).
197 # #assert pi.sin == 0.0
198 fun sin: Float is extern "kernel_Float_Float_sin_0"
200 # Computes the cosine of x (expressed in radians).
202 # #assert 0.0.tan == 0.0
203 fun tan: Float is extern "kernel_Float_Float_tan_0"
205 # Computes the arc cosine of `self`.
207 # #assert 0.0.acos == pi / 2.0
208 fun acos: Float is extern "kernel_Float_Float_acos_0"
210 # Computes the arc sine of `self`.
212 # #assert 1.0.asin == pi / 2.0
213 fun asin: Float is extern "kernel_Float_Float_asin_0"
215 # Computes the arc tangent of `self`.
217 # #assert 0.0.tan == 0.0
218 fun atan: Float is extern "kernel_Float_Float_atan_0"
220 # Returns the absolute value of `self`.
222 # assert 12.0.abs == 12.0
223 # assert (-34.56).abs == 34.56
224 # assert -34.56.abs == -34.56
225 fun abs: Float `{ return fabs(self); `}
227 # Returns `self` raised at `e` power.
229 # #assert 2.0.pow(0.0) == 1.0
230 # #assert 2.0.pow(3.0) == 8.0
231 # #assert 0.0.pow(9.0) == 0.0
232 fun pow
(e
: Float): Float is extern "kernel_Float_Float_pow_1"
234 # Natural logarithm of `self`.
236 # assert 0.0.log.is_inf == -1
237 # #assert 1.0.log == 0.0
238 fun log
: Float is extern "kernel_Float_Float_log_0"
240 # Logarithm of `self` to base `base`.
242 # assert 100.0.log_base(10.0) == 2.0
243 # assert 256.0.log_base(2.0) == 8.0
244 fun log_base
(base
: Float): Float do return log
/base
.log
246 # Returns *e* raised to `self`.
247 fun exp
: Float is extern "kernel_Float_Float_exp_0"
249 # assert 1.1.ceil == 2.0
250 # assert 1.9.ceil == 2.0
251 # assert 2.0.ceil == 2.0
252 # assert (-1.5).ceil == -1.0
253 fun ceil
: Float `{ return ceil(self); `}
255 # assert 1.1.floor == 1.0
256 # assert 1.9.floor == 1.0
257 # assert 2.0.floor == 2.0
258 # assert (-1.5).floor == -2.0
259 fun floor: Float `{ return floor(self); `}
261 # Rounds the value of a float to its nearest integer value
263 # assert 1.67.round == 2.0
264 # assert 1.34.round == 1.0
265 # assert -1.34.round == -1.0
266 # assert -1.67.round == -2.0
267 fun round
: Float is extern "round"
269 # Returns a random `Float` in `[0.0 .. self[`.
270 fun rand
: Float is extern "kernel_Float_Float_rand_0"
272 # Returns the euclidean distance from `b`.
273 fun hypot_with
(b
: Float): Float is extern "hypotf"
275 # Returns true is self is not a number.
276 fun is_nan
: Bool is extern "isnan"
278 # Is the float an infinite value
279 # this function returns:
281 # * 1 if self is positive infinity
282 # * -1 if self is negative infinity
285 if is_inf_extern
then
286 if self < 0.0 then return -1
292 private fun is_inf_extern
: Bool is extern "isinf"
294 # Linear interpolation between `a` and `b` using `self` as weight
297 # assert 0.0.lerp(0.0, 128.0) == 0.0
298 # assert 0.5.lerp(0.0, 128.0) == 64.0
299 # assert 1.0.lerp(0.0, 128.0) == 128.0
300 # assert -0.5.lerp(0.0, 128.0) == -64.0
302 fun lerp
(a
, b
: Float): Float do return (1.0 - self) * a
+ self * b
305 redef class Collection[ E
]
306 # Return a random element form the collection
307 # There must be at least one element in the collection
310 # var x = [1,2,3].rand
311 # assert x == 1 or x == 2 or x == 3
315 if is_empty
then abort
316 var rand_index
= length
.rand
319 if rand_index
== 0 then return e
325 # Return a new array made of elements in a random order.
328 # var a = [1,2,1].to_shuffle
329 # assert a == [1,1,2] or a == [1,2,1] or a == [2,1,1]
331 fun to_shuffle
: Array[E
]
339 redef class SequenceRead[E
]
340 # Optimized for large collections using `[]`
344 return self[length
.rand
]
348 redef class AbstractArray[E
]
349 # Reorder randomly the elements in self.
352 # var a = new Array[Int]
363 # assert a == [1,2] or a == [2,1]
366 # ENSURE self.shuffle.has_exactly(old(self))
369 for i
in [0..length
[ do
370 var j
= i
+ (length-i
).rand
385 # Computes the arc tangent given `x` and `y`.
387 # assert atan2(-0.0, 1.0) == -0.0
388 # assert atan2(0.0, 1.0) == 0.0
389 fun atan2
(x
: Float, y
: Float): Float is extern "kernel_Any_Any_atan2_2"
391 # Approximate value of **pi**.
392 fun pi
: Float is extern "kernel_Any_Any_pi_0"
394 # Initialize the pseudo-random generator with the given seed.
395 # The pseudo-random generator is used by the method `rand` and other to generate sequence of numbers.
396 # These sequences are repeatable by calling `srand_from` with a same seed value.
403 # assert 10.rand == a
404 # assert 100.rand == b
406 fun srand_from
(x
: Int) is extern "kernel_Any_Any_srand_from_1"
408 # Reinitialize the pseudo-random generator used by the method `rand` and other.
409 # This method is automatically invoked at the begin of the program, so usually, there is no need to manually invoke it.
410 # The only exception is in conjunction with `srand_from` to reset the pseudo-random generator.
411 fun srand
is extern "kernel_Any_Any_srand_0"