Merge branch 'wip2'
[nit.git] / lib / standard / math.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2004-2008 Jean Privat <jean@pryen.org>
4 #
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
11 # another product.
12
13 # Mathematical operations
14 package math
15
16 import kernel
17 import collection
18
19 redef class Int
20 fun rand: Int is extern "kernel_Int_Int_rand_0"
21 end
22
23 redef class Float
24 fun sqrt: Float is extern "kernel_Float_Float_sqrt_0"
25 fun cos: Float is extern "kernel_Float_Float_cos_0"
26 fun sin: Float is extern "kernel_Float_Float_sin_0"
27 fun tan: Float is extern "kernel_Float_Float_tan_0"
28 fun acos: Float is extern "kernel_Float_Float_acos_0"
29 fun asin: Float is extern "kernel_Float_Float_asin_0"
30 fun atan: Float is extern "kernel_Float_Float_atan_0"
31
32 fun pow(e: Float): Float is extern "kernel_Float_Float_pow_1"
33 fun log: Float is extern "kernel_Float_Float_log_0"
34 fun exp: Float is extern "kernel_Float_Float_exp_0"
35
36 fun rand: Float is extern "kernel_Float_Float_rand_0"
37 fun hypot_with( b : Float ) : Float is extern "hypotf"
38 end
39
40 redef class Collection[ E ]
41 # Return a random element in the collection
42 fun rand : nullable E
43 do
44 if is_empty then return null
45
46 var rand_index = length.rand
47 var picked : nullable E = null
48
49 iterate !each( e ) do
50 if rand_index == 0
51 then
52 picked = e
53 break
54 else
55 rand_index -= 1
56 end
57 end
58
59 return picked
60 end
61 end
62
63 fun atan2(x: Float, y: Float): Float is extern "kernel_Any_Any_atan2_2"
64 fun pi: Float is extern "kernel_Any_Any_pi_0"
65 fun srand_from(x: Int) is extern "kernel_Any_Any_srand_from_1"
66 fun srand is extern "kernel_Any_Any_srand_0"