402e1bb4ce6e2c6b829ccc30b67cc6e039e52087
1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2004-2008 Jean Privat <jean@pryen.org>
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
13 # Mathematical operations
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 # Returns the 1's complement of `self`
31 # assert 0x2F.bin_not == -48
32 fun bin_not
: Int is extern "kernel_Int_Int_binnot_0"
33 fun sqrt
: Int `{ return sqrt(recv); `}
34 # Returns the greatest common divisor of `self` and `o
`
36 # assert 54.gcd(24) == 6
37 # assert -54.gcd(-24) == 6
38 # assert 54.gcd(-24) == -6
39 # assert -54.gcd(24) == -6
40 # assert 12.gcd(6) == 6
43 if self < 0 then return -(-self).gcd(o)
44 if o < 0 then return -(self.gcd(-o))
45 if self == 0 or o == self then return o
46 if o == 0 then return self
47 if self.bin_and(1) == 0 then
48 if o.bin_and(1) == 1 then
49 return self.rshift(1).gcd(o)
51 return self.rshift(1).gcd(o.rshift(1)).lshift(1)
54 if o.bin_and(1) == 0 then return self.gcd(o.rshift(1))
55 if self > o then return (self - o).rshift(1).gcd(o)
56 return (o - self).rshift(1).gcd(self)
61 fun sqrt: Float is extern "kernel_Float_Float_sqrt_0"
62 fun cos: Float is extern "kernel_Float_Float_cos_0"
63 fun sin: Float is extern "kernel_Float_Float_sin_0"
64 fun tan: Float is extern "kernel_Float_Float_tan_0"
65 fun acos: Float is extern "kernel_Float_Float_acos_0"
66 fun asin: Float is extern "kernel_Float_Float_asin_0"
67 fun atan: Float is extern "kernel_Float_Float_atan_0"
68 fun abs: Float `{ return fabs(recv); `}
70 fun pow
(e
: Float): Float is extern "kernel_Float_Float_pow_1"
71 fun log
: Float is extern "kernel_Float_Float_log_0"
72 fun exp
: Float is extern "kernel_Float_Float_exp_0"
74 # assert 1.1.ceil == 2.0
75 # assert 1.9.ceil == 2.0
76 # assert 2.0.ceil == 2.0
77 # assert (-1.5).ceil == -1.0
78 fun ceil
: Float `{ return ceil(recv); `}
80 # assert 1.1.floor == 1.0
81 # assert 1.9.floor == 1.0
82 # assert 2.0.floor == 2.0
83 # assert (-1.5).floor == -2.0
84 fun floor: Float `{ return floor(recv); `}
86 # Returns a random `Float` in `[0.0 .. self[`.
87 fun rand
: Float is extern "kernel_Float_Float_rand_0"
88 fun hypot_with
( b
: Float ) : Float is extern "hypotf"
90 fun is_nan
: Bool is extern "isnan"
92 # Is the float an infinite value
93 # this function returns:
95 # * 1 if self is positive infinity
96 # * -1 if self is negative infinity
100 if self < 0.0 then return -1
106 private fun is_inf_extern
: Bool is extern "isinf"
109 redef class Collection[ E
]
110 # Return a random element form the collection
111 # There must be at least one element in the collection
114 if is_empty
then abort
115 var rand_index
= length
.rand
118 if rand_index
== 0 then return e
125 fun atan2
(x
: Float, y
: Float): Float is extern "kernel_Any_Any_atan2_2"
126 fun pi
: Float is extern "kernel_Any_Any_pi_0"
127 fun srand_from
(x
: Int) is extern "kernel_Any_Any_srand_from_1"
128 fun srand
is extern "kernel_Any_Any_srand_0"