lib: update standard library to latest FFI spec
[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 fun rand: Int is extern "kernel_Int_Int_rand_0"
25 fun bin_and(i: Int): Int is extern "kernel_Int_Int_binand_0"
26 fun bin_or(i: Int): Int is extern "kernel_Int_Int_binor_0"
27 fun bin_xor(i: Int): Int is extern "kernel_Int_Int_binxor_0"
28 fun sqrt : Int is extern `{ return sqrtl(recv); `}
29 fun sin : Int is extern `{ return sinl(recv); `}
30 fun cos : Int is extern `{ return cosl(recv); `}
31 end
32
33 redef class Float
34 fun sqrt: Float is extern "kernel_Float_Float_sqrt_0"
35 fun cos: Float is extern "kernel_Float_Float_cos_0"
36 fun sin: Float is extern "kernel_Float_Float_sin_0"
37 fun tan: Float is extern "kernel_Float_Float_tan_0"
38 fun acos: Float is extern "kernel_Float_Float_acos_0"
39 fun asin: Float is extern "kernel_Float_Float_asin_0"
40 fun atan: Float is extern "kernel_Float_Float_atan_0"
41 fun abs: Float `{ return abs(recv); `}
42
43 fun pow(e: Float): Float is extern "kernel_Float_Float_pow_1"
44 fun log: Float is extern "kernel_Float_Float_log_0"
45 fun exp: Float is extern "kernel_Float_Float_exp_0"
46
47 fun rand: Float is extern "kernel_Float_Float_rand_0"
48 fun hypot_with( b : Float ) : Float is extern "hypotf"
49 end
50
51 redef class Collection[ E ]
52 # Return a random element form the collection
53 # There must be at least one element in the collection
54 fun rand: E
55 do
56 if is_empty then abort
57 var rand_index = length.rand
58
59 for e in self do
60 if rand_index == 0 then return e
61 rand_index -= 1
62 end
63 abort
64 end
65 end
66
67 fun atan2(x: Float, y: Float): Float is extern "kernel_Any_Any_atan2_2"
68 fun pi: Float is extern "kernel_Any_Any_pi_0"
69 fun srand_from(x: Int) is extern "kernel_Any_Any_srand_from_1"
70 fun srand is extern "kernel_Any_Any_srand_0"