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 /* Is rand shortcut? */
27 static int nit_rand_seeded;
28 /* Current rand seed if seeded */
29 static unsigned int nit_rand_seed;
31 #define NIT_RAND_MAX 0x7fffffff
33 /* This algorithm is mentioned in the ISO C standard, here extended
37 unsigned int next = nit_rand_seed;
42 result = (unsigned int) (next / 65536) % 2048;
47 result ^= (unsigned int) (next / 65536) % 1024;
52 result ^= (unsigned int) (next / 65536) % 1024;
61 # Returns a random `Int` in `[0 .. self[`.
63 if (nit_rand_seeded) return (long)(((double)self)*nit_rand()/(NIT_RAND_MAX+1.0));
64 return (long)(((double)self)*rand()/(RAND_MAX+1.0));
67 # Returns the result of a binary AND operation on `self` and `i`
69 # assert 0x10 & 0x01 == 0
70 fun &(i
: Int): Int is intern do return band
(i
)
72 private fun band
(i
: Int): Int `{ return self & i; `}
74 # Returns the result of a binary OR operation on `self` and `i
`
76 # assert 0x10 | 0x01 == 0x11
77 fun |(i: Int): Int is intern do return bor(i)
79 private fun bor(i: Int): Int `{ return self | i; `}
81 # Returns the result of a binary XOR operation on `self` and `i`
83 # assert 0x101 ^ 0x110 == 0x11
84 fun ^
(i
: Int): Int `{ return self ^ i; `}
86 # Returns the 1's complement of `self`
89 fun ~: Int `{ return ~self; `}
91 # Returns the square root of `self`
94 fun sqrt
: Int `{ return sqrt(self); `}
96 # Returns the greatest common divisor of `self` and `o
`
98 # assert 54.gcd(24) == 6
99 # assert -54.gcd(-24) == 6
100 # assert 54.gcd(-24) == -6
101 # assert -54.gcd(24) == -6
102 # assert 12.gcd(6) == 6
105 if self < 0 then return -(-self).gcd(o)
106 if o < 0 then return -(self.gcd(-o))
107 if self == 0 or o == self then return o
108 if o == 0 then return self
109 if self & 1 == 0 then
111 return (self >> 1).gcd(o)
113 return (self >> 1).gcd(o >> 1) << 1
116 if o & 1 == 0 then return self.gcd(o >> 1)
117 if self > o then return ((self - o) >> 1).gcd(o)
118 return ((o - self) >> 1).gcd(self)
124 fun is_even: Bool do return self % 2 == 0
128 # assert not 13.is_even
129 fun is_odd: Bool do return not is_even
131 # Is self a prime number ?
134 # assert not 1.is_prime
135 # assert not 12.is_prime
140 else if self <= 1 or self.is_even then
143 for i in [3..self.sqrt[ do
144 if self % i == 0 then return false
149 # Returns the `self` raised to the power of `e
`.
154 return self.to_f.pow(e.to_f).to_i
157 # The factorial of `self` (aka `self!`)
159 # Returns `1 * 2 * 3 * ... * self-1
* self`
161 # assert 0.factorial == 1 # by convention for an empty product
162 # assert 1.factorial == 1
163 # assert 4.factorial == 24
164 # assert 9.factorial == 362880
179 # Returns the result of a binary AND operation on `self` and `i
`
181 # assert 0x10u8 & 0x01u8 == 0u8
182 fun &(i: Byte): Byte is intern do return band(i)
184 private fun band(i: Byte): Byte `{ return self & i; `}
186 # Returns the result of a binary OR operation on `self` and `i`
188 # assert 0x10u8 | 0x01u8 == 0x11u8
189 fun |(i
: Byte): Byte `{ return self | i; `}
191 # Returns the result of a binary XOR operation on `self` and `i
`
193 # assert 0x101u8 ^ 0x110u8 == 0x11u8
194 fun ^(i: Byte): Byte `{ return self ^ i; `}
196 # Returns the 1's complement of `self`
198 # assert ~0x2Fu8 == 0xD0u8
199 fun ~
: Byte `{ return ~self; `}
204 # Returns the non-negative square root of `self`.
206 # assert 9.0.sqrt == 3.0
207 # #assert 3.0.sqrt == 1.732
208 # assert 1.0.sqrt == 1.0
209 # assert 0.0.sqrt == 0.0
210 fun sqrt: Float `{ return sqrt(self); `}
212 # Computes the cosine of `self` (expressed in radians).
214 # #assert pi.cos == -1.0
215 fun cos
: Float `{ return cos(self); `}
217 # Computes the sine of `self` (expressed in radians).
219 # #assert pi.sin == 0.0
220 fun sin: Float `{ return sin(self); `}
222 # Computes the cosine of x (expressed in radians).
224 # #assert 0.0.tan == 0.0
225 fun tan
: Float `{ return tan(self); `}
227 # Computes the arc cosine of `self`.
229 # #assert 0.0.acos == pi / 2.0
230 fun acos: Float `{ return acos(self); `}
232 # Computes the arc sine of `self`.
234 # #assert 1.0.asin == pi / 2.0
235 fun asin
: Float `{ return asin(self); `}
237 # Computes the arc tangent of `self`.
239 # #assert 0.0.tan == 0.0
240 fun atan: Float `{ return atan(self); `}
242 # Returns the absolute value of `self`.
244 # assert 12.0.abs == 12.0
245 # assert (-34.56).abs == 34.56
246 # assert -34.56.abs == -34.56
247 fun abs
: Float `{ return fabs(self); `}
249 # Returns `self` raised at `e
` power.
251 # #assert 2.0.pow(0.0) == 1.0
252 # #assert 2.0.pow(3.0) == 8.0
253 # #assert 0.0.pow(9.0) == 0.0
254 fun pow(e: Float): Float `{ return pow(self, e); `}
256 # Natural logarithm of `self`.
258 # assert 0.0.log.is_inf == -1
259 # #assert 1.0.log == 0.0
260 fun log
: Float `{ return log(self); `}
262 # Logarithm of `self` to base `base
`.
264 # assert 100.0.log_base(10.0) == 2.0
265 # assert 256.0.log_base(2.0) == 8.0
266 fun log_base(base: Float): Float do return log/base.log
268 # Returns *e* raised to `self`.
269 fun exp: Float `{ return exp(self); `}
271 # assert 1.1.ceil == 2.0
272 # assert 1.9.ceil == 2.0
273 # assert 2.0.ceil == 2.0
274 # assert (-1.5).ceil == -1.0
275 fun ceil
: Float `{ return ceil(self); `}
277 # assert 1.1.floor == 1.0
278 # assert 1.9.floor == 1.0
279 # assert 2.0.floor == 2.0
280 # assert (-1.5).floor == -2.0
281 fun floor: Float `{ return floor(self); `}
283 # Rounds the value of a float to its nearest integer value
285 # assert 1.67.round == 2.0
286 # assert 1.34.round == 1.0
287 # assert -1.34.round == -1.0
288 # assert -1.67.round == -2.0
289 fun round
: Float `{ return round(self); `}
291 # Returns a random `Float` in `[0.0 .. self[`.
293 if (nit_rand_seeded
) return ((self)*nit_rand
())/(NIT_RAND_MAX+1.0);
294 return ((self)*rand
())/(RAND_MAX+1.0);
297 # Returns the euclidean distance from `b
`.
298 fun hypot_with(b: Float): Float `{ return hypotf(self, b); `}
300 # Returns true is self is not a number.
302 # As `nan != nan`, `is_nan` should be used to test if a float is the special *not a number* value.
305 # assert nan != nan # By IEEE 754
307 # assert not 10.0.is_nan
309 fun is_nan
: Bool `{ return isnan(self); `}
311 # Is the float an infinite value
312 # this function returns:
314 # * 1 if self is positive infinity
315 # * -1 if self is negative infinity
319 # assert 10.0.is_inf == 0
320 # assert inf.is_inf == 1
321 # assert (-inf).is_inf == -1
324 if native_is_inf then
325 if self < 0.0 then return -1
331 private fun native_is_inf: Bool `{ return isinf(self); `}
333 # Linear interpolation between `a` and `b` using `self` as weight
336 # assert 0.0.lerp(0.0, 128.0) == 0.0
337 # assert 0.5.lerp(0.0, 128.0) == 64.0
338 # assert 1.0.lerp(0.0, 128.0) == 128.0
339 # assert -0.5.lerp(0.0, 128.0) == -64.0
341 fun lerp
(a
, b
: Float): Float do return (1.0 - self) * a
+ self * b
343 # Quadratic Bézier interpolation between `a` and `b` with an `handle` using `self` as weight
346 # assert 0.00.qerp(0.0, 32.0, 128.0) == 0.0
347 # assert 0.25.qerp(0.0, 32.0, 128.0) == 20.0
348 # assert 0.50.qerp(0.0, 32.0, 128.0) == 48.0
349 # assert 0.75.qerp(0.0, 32.0, 128.0) == 84.0
350 # assert 1.00.qerp(0.0, 32.0, 128.0) == 128.0
352 fun qerp
(a
, handle
, b
: Float): Float do
361 # Cubic Bézier interpolation between `a` and `b` with two handles using `self` as weight
363 # The Cubic Bézier interpolation is the most common one and use two control points.
366 # assert 0.00.cerp(0.0, 32.0, 128.0, 64.0) == 0.0
367 # assert 0.25.cerp(0.0, 32.0, 128.0, 64.0) == 32.5
368 # assert 0.50.cerp(0.0, 32.0, 128.0, 64.0) == 68.0
369 # assert 0.75.cerp(0.0, 32.0, 128.0, 64.0) == 85.5
370 # assert 1.00.cerp(0.0, 32.0, 128.0, 64.0) == 64.0
372 fun cerp
(a
, a_handle
, b_handle
, b
: Float): Float do
376 3.0*i
*i
*p
* a_handle
+
377 3.0*i
*p
*p
* b_handle
+
383 # Positive float infinite (IEEE 754)
386 # assert inf.is_inf == 1
388 # `inf` follows the arithmetic of infinites
390 # assert (inf - 1.0) == inf
391 # assert (inf - inf).is_nan
393 # The negative infinite can be used as `-inf`.
395 # assert -inf < -10.0
396 # assert (-inf).is_inf == -1
397 fun inf
: Float do return 1.0 / 0.0
399 # Not a Number, representation of an undefined or unrepresentable float (IEEE 754).
401 # `nan` is not comparable with itself, you should use `Float::is_nan` to test it.
405 # assert nan != nan # By IEEE 754
408 # `nan` is the quiet result of some undefined operations.
411 # assert (1.0 + nan).is_nan
412 # assert (0.0 / 0.0).is_nan
413 # assert (inf - inf).is_nan
414 # assert (inf / inf).is_nan
415 # assert (-1.0).sqrt.is_nan
417 fun nan
: Float do return 0.0 / 0.0
419 redef class Collection[ E
]
420 # Return a random element form the collection
421 # There must be at least one element in the collection
424 # var x = [1,2,3].rand
425 # assert x == 1 or x == 2 or x == 3
429 if is_empty
then abort
430 var rand_index
= length
.rand
433 if rand_index
== 0 then return e
439 # Return a new array made of elements in a random order.
442 # var a = [1,2,1].to_shuffle
443 # assert a == [1,1,2] or a == [1,2,1] or a == [2,1,1]
445 fun to_shuffle
: Array[E
]
453 redef class SequenceRead[E
]
454 # Optimized for large collections using `[]`
458 return self[length
.rand
]
462 redef class AbstractArray[E
]
463 # Reorder randomly the elements in self.
466 # var a = new Array[Int]
477 # assert a == [1,2] or a == [2,1]
480 # ENSURE self.shuffle.has_exactly(old(self))
483 for i
in [0..length
[ do
484 var j
= i
+ (length-i
).rand
499 # Computes the arc tangent given `x` and `y`.
501 # assert atan2(-0.0, 1.0) == -0.0
502 # assert atan2(0.0, 1.0) == 0.0
503 fun atan2
(x
: Float, y
: Float): Float `{ return atan2(x, y); `}
505 # Approximate value of **pi**.
506 fun pi: Float do return 3.14159265
508 # Initialize the pseudo-random generator with the given seed.
509 # The pseudo-random generator is used by the method `rand
` and other to generate sequence of numbers.
510 # These sequences are repeatable by calling `srand_from
` with a same seed value.
517 # assert 10.rand == a
518 # assert 100.rand == b
520 fun srand_from(x: Int) `{ nit_rand_seeded = 1; nit_rand_seed = x; `}
522 # Reinitialize the pseudo-random generator used by the method `rand` and other.
523 # This method is automatically invoked at the begin of the program, so usually, there is no need to manually invoke it.
524 # The only exception is in conjunction with `srand_from` to reset the pseudo-random generator.
525 fun srand
`{ nit_rand_seeded = 0; srand(time(NULL)); `}