Merge: Give top-level methods some rules
[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 module math
15
16 import kernel
17 import collection
18
19 in "C header" `{
20 #include <math.h>
21 `}
22
23 redef class Int
24 # Returns a random `Int` in `[0 .. self[`.
25 fun rand: Int is extern "kernel_Int_Int_rand_0"
26 fun bin_and(i: Int): Int is extern "kernel_Int_Int_binand_0"
27 fun bin_or(i: Int): Int is extern "kernel_Int_Int_binor_0"
28 fun bin_xor(i: Int): Int is extern "kernel_Int_Int_binxor_0"
29 fun sqrt: Int `{ return sqrt(recv); `}
30 # Returns the greatest common divisor of `self` and `o`
31 #
32 # assert 54.gcd(24) == 6
33 # assert -54.gcd(-24) == 6
34 # assert 54.gcd(-24) == -6
35 # assert -54.gcd(24) == -6
36 # assert 12.gcd(6) == 6
37 fun gcd(o: Int): Int
38 do
39 if self < 0 then return -(-self).gcd(o)
40 if o < 0 then return -(self.gcd(-o))
41 if self == 0 or o == self then return o
42 if o == 0 then return self
43 if self.bin_and(1) == 0 then
44 if o.bin_and(1) == 1 then
45 return self.rshift(1).gcd(o)
46 else
47 return self.rshift(1).gcd(o.rshift(1)).lshift(1)
48 end
49 end
50 if o.bin_and(1) == 0 then return self.gcd(o.rshift(1))
51 if self > o then return (self - o).rshift(1).gcd(o)
52 return (o - self).rshift(1).gcd(self)
53 end
54 end
55
56 redef class Float
57 fun sqrt: Float is extern "kernel_Float_Float_sqrt_0"
58 fun cos: Float is extern "kernel_Float_Float_cos_0"
59 fun sin: Float is extern "kernel_Float_Float_sin_0"
60 fun tan: Float is extern "kernel_Float_Float_tan_0"
61 fun acos: Float is extern "kernel_Float_Float_acos_0"
62 fun asin: Float is extern "kernel_Float_Float_asin_0"
63 fun atan: Float is extern "kernel_Float_Float_atan_0"
64 fun abs: Float `{ return fabs(recv); `}
65
66 fun pow(e: Float): Float is extern "kernel_Float_Float_pow_1"
67 fun log: Float is extern "kernel_Float_Float_log_0"
68 fun exp: Float is extern "kernel_Float_Float_exp_0"
69
70 # assert 1.1.ceil == 2.0
71 # assert 1.9.ceil == 2.0
72 # assert 2.0.ceil == 2.0
73 # assert (-1.5).ceil == -1.0
74 fun ceil: Float `{ return ceil(recv); `}
75
76 # assert 1.1.floor == 1.0
77 # assert 1.9.floor == 1.0
78 # assert 2.0.floor == 2.0
79 # assert (-1.5).floor == -2.0
80 fun floor: Float `{ return floor(recv); `}
81
82 # Returns a random `Float` in `[0.0 .. self[`.
83 fun rand: Float is extern "kernel_Float_Float_rand_0"
84 fun hypot_with( b : Float ) : Float is extern "hypotf"
85
86 fun is_nan: Bool is extern "isnan"
87
88 # Is the float an infinite value
89 # this function returns:
90 #
91 # * 1 if self is positive infinity
92 # * -1 if self is negative infinity
93 # * 0 otherwise
94 fun is_inf: Int do
95 if is_inf_extern then
96 if self < 0.0 then return -1
97 return 1
98 end
99 return 0
100 end
101
102 private fun is_inf_extern: Bool is extern "isinf"
103 end
104
105 redef class Collection[ E ]
106 # Return a random element form the collection
107 # There must be at least one element in the collection
108 fun rand: E
109 do
110 if is_empty then abort
111 var rand_index = length.rand
112
113 for e in self do
114 if rand_index == 0 then return e
115 rand_index -= 1
116 end
117 abort
118 end
119 end
120
121 fun atan2(x: Float, y: Float): Float is extern "kernel_Any_Any_atan2_2"
122 fun pi: Float is extern "kernel_Any_Any_pi_0"
123 fun srand_from(x: Int) is extern "kernel_Any_Any_srand_from_1"
124 fun srand is extern "kernel_Any_Any_srand_0"