tests: add error_toplevel.nit
[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 end
31
32 redef class Float
33 fun sqrt: Float is extern "kernel_Float_Float_sqrt_0"
34 fun cos: Float is extern "kernel_Float_Float_cos_0"
35 fun sin: Float is extern "kernel_Float_Float_sin_0"
36 fun tan: Float is extern "kernel_Float_Float_tan_0"
37 fun acos: Float is extern "kernel_Float_Float_acos_0"
38 fun asin: Float is extern "kernel_Float_Float_asin_0"
39 fun atan: Float is extern "kernel_Float_Float_atan_0"
40 fun abs: Float `{ return fabs(recv); `}
41
42 fun pow(e: Float): Float is extern "kernel_Float_Float_pow_1"
43 fun log: Float is extern "kernel_Float_Float_log_0"
44 fun exp: Float is extern "kernel_Float_Float_exp_0"
45
46 # assert 1.1.ceil == 2.0
47 # assert 1.9.ceil == 2.0
48 # assert 2.0.ceil == 2.0
49 # assert (-1.5).ceil == -1.0
50 fun ceil: Float `{ return ceil(recv); `}
51
52 # assert 1.1.floor == 1.0
53 # assert 1.9.floor == 1.0
54 # assert 2.0.floor == 2.0
55 # assert (-1.5).floor == -2.0
56 fun floor: Float `{ return floor(recv); `}
57
58 # Returns a random `Float` in `[0.0 .. self[`.
59 fun rand: Float is extern "kernel_Float_Float_rand_0"
60 fun hypot_with( b : Float ) : Float is extern "hypotf"
61
62 fun is_nan: Bool is extern "isnan"
63
64 # Is the float an infinite value
65 # this function returns:
66 #
67 # * 1 if self is positive infinity
68 # * -1 if self is negative infinity
69 # * 0 otherwise
70 fun is_inf: Int do
71 if is_inf_extern then
72 if self < 0.0 then return -1
73 return 1
74 end
75 return 0
76 end
77
78 private fun is_inf_extern: Bool is extern "isinf"
79 end
80
81 redef class Collection[ E ]
82 # Return a random element form the collection
83 # There must be at least one element in the collection
84 fun rand: E
85 do
86 if is_empty then abort
87 var rand_index = length.rand
88
89 for e in self do
90 if rand_index == 0 then return e
91 rand_index -= 1
92 end
93 abort
94 end
95 end
96
97 fun atan2(x: Float, y: Float): Float is extern "kernel_Any_Any_atan2_2"
98 fun pi: Float is extern "kernel_Any_Any_pi_0"
99 fun srand_from(x: Int) is extern "kernel_Any_Any_srand_from_1"
100 fun srand is extern "kernel_Any_Any_srand_0"