nitdoc: Escape paths passed to the shell.
[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
27 # Returns the result of a binary AND operation on `self` and `i`
28 #
29 # assert 0x10.bin_and(0x01) == 0
30 fun bin_and(i: Int): Int is extern "kernel_Int_Int_binand_0"
31
32 # Returns the result of a binary OR operation on `self` and `i`
33 #
34 # assert 0x10.bin_or(0x01) == 0x11
35 fun bin_or(i: Int): Int is extern "kernel_Int_Int_binor_0"
36
37 # Returns the result of a binary XOR operation on `self` and `i`
38 #
39 # assert 0x101.bin_xor(0x110) == 0x11
40 fun bin_xor(i: Int): Int is extern "kernel_Int_Int_binxor_0"
41
42 # Returns the 1's complement of `self`
43 #
44 # assert 0x2F.bin_not == -48
45 fun bin_not: Int is extern "kernel_Int_Int_binnot_0"
46
47 # Returns the square root of `self`
48 #
49 # assert 16.sqrt == 4
50 fun sqrt: Int `{ return sqrt(recv); `}
51
52 # Returns the greatest common divisor of `self` and `o`
53 #
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
59 fun gcd(o: Int): Int
60 do
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)
68 else
69 return self.rshift(1).gcd(o.rshift(1)).lshift(1)
70 end
71 end
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)
75 end
76
77 # Is `self` even ?
78 #
79 # assert 12.is_even
80 fun is_even: Bool do return self % 2 == 0
81
82 # Is `self` odd ?
83 #
84 # assert not 13.is_even
85 fun is_odd: Bool do return not is_even
86 end
87
88 redef class Float
89 fun sqrt: Float is extern "kernel_Float_Float_sqrt_0"
90 fun cos: Float is extern "kernel_Float_Float_cos_0"
91 fun sin: Float is extern "kernel_Float_Float_sin_0"
92 fun tan: Float is extern "kernel_Float_Float_tan_0"
93 fun acos: Float is extern "kernel_Float_Float_acos_0"
94 fun asin: Float is extern "kernel_Float_Float_asin_0"
95 fun atan: Float is extern "kernel_Float_Float_atan_0"
96 fun abs: Float `{ return fabs(recv); `}
97
98 fun pow(e: Float): Float is extern "kernel_Float_Float_pow_1"
99 fun log: Float is extern "kernel_Float_Float_log_0"
100 fun exp: Float is extern "kernel_Float_Float_exp_0"
101
102 # assert 1.1.ceil == 2.0
103 # assert 1.9.ceil == 2.0
104 # assert 2.0.ceil == 2.0
105 # assert (-1.5).ceil == -1.0
106 fun ceil: Float `{ return ceil(recv); `}
107
108 # assert 1.1.floor == 1.0
109 # assert 1.9.floor == 1.0
110 # assert 2.0.floor == 2.0
111 # assert (-1.5).floor == -2.0
112 fun floor: Float `{ return floor(recv); `}
113
114 # Returns a random `Float` in `[0.0 .. self[`.
115 fun rand: Float is extern "kernel_Float_Float_rand_0"
116 fun hypot_with( b : Float ) : Float is extern "hypotf"
117
118 fun is_nan: Bool is extern "isnan"
119
120 # Is the float an infinite value
121 # this function returns:
122 #
123 # * 1 if self is positive infinity
124 # * -1 if self is negative infinity
125 # * 0 otherwise
126 fun is_inf: Int do
127 if is_inf_extern then
128 if self < 0.0 then return -1
129 return 1
130 end
131 return 0
132 end
133
134 private fun is_inf_extern: Bool is extern "isinf"
135 end
136
137 redef class Collection[ E ]
138 # Return a random element form the collection
139 # There must be at least one element in the collection
140 fun rand: E
141 do
142 if is_empty then abort
143 var rand_index = length.rand
144
145 for e in self do
146 if rand_index == 0 then return e
147 rand_index -= 1
148 end
149 abort
150 end
151 end
152
153 fun atan2(x: Float, y: Float): Float is extern "kernel_Any_Any_atan2_2"
154 fun pi: Float is extern "kernel_Any_Any_pi_0"
155 fun srand_from(x: Int) is extern "kernel_Any_Any_srand_from_1"
156 fun srand is extern "kernel_Any_Any_srand_0"