X-Git-Url: http://nitlanguage.org diff --git a/lib/standard/math.nit b/lib/standard/math.nit index 14c0e40..8b5fecb 100644 --- a/lib/standard/math.nit +++ b/lib/standard/math.nit @@ -47,7 +47,7 @@ redef class Int # Returns the square root of `self` # # assert 16.sqrt == 4 - fun sqrt: Int `{ return sqrt(recv); `} + fun sqrt: Int `{ return sqrt(self); `} # Returns the greatest common divisor of `self` and `o` # @@ -158,7 +158,7 @@ redef class Float # assert 12.0.abs == 12.0 # assert (-34.56).abs == 34.56 # assert -34.56.abs == -34.56 - fun abs: Float `{ return fabs(recv); `} + fun abs: Float `{ return fabs(self); `} # Returns `self` raised at `e` power. # @@ -167,12 +167,18 @@ redef class Float # #assert 0.0.pow(9.0) == 0.0 fun pow(e: Float): Float is extern "kernel_Float_Float_pow_1" - # Returns the logarithm of `self`. + # Natural logarithm of `self`. # # assert 0.0.log.is_inf == -1 # #assert 1.0.log == 0.0 fun log: Float is extern "kernel_Float_Float_log_0" + # Logarithm of `self` to base `base`. + # + # assert 100.0.log_base(10.0) == 2.0 + # assert 256.0.log_base(2.0) == 8.0 + fun log_base(base: Float): Float do return log/base.log + # Returns *e* raised to `self`. fun exp: Float is extern "kernel_Float_Float_exp_0" @@ -180,13 +186,13 @@ redef class Float # assert 1.9.ceil == 2.0 # assert 2.0.ceil == 2.0 # assert (-1.5).ceil == -1.0 - fun ceil: Float `{ return ceil(recv); `} + fun ceil: Float `{ return ceil(self); `} # assert 1.1.floor == 1.0 # assert 1.9.floor == 1.0 # assert 2.0.floor == 2.0 # assert (-1.5).floor == -2.0 - fun floor: Float `{ return floor(recv); `} + fun floor: Float `{ return floor(self); `} # Rounds the value of a float to its nearest integer value # @@ -195,7 +201,7 @@ redef class Float # assert -1.34.round == -1.0 # assert -1.67.round == -2.0 fun round: Float is extern "round" - + # Returns a random `Float` in `[0.0 .. self[`. fun rand: Float is extern "kernel_Float_Float_rand_0" @@ -220,11 +226,26 @@ redef class Float end private fun is_inf_extern: Bool is extern "isinf" + + # Linear interpolation between `a` and `b` using `self` as weight + # + # ~~~ + # assert 0.0.lerp(0.0, 128.0) == 0.0 + # assert 0.5.lerp(0.0, 128.0) == 64.0 + # assert 1.0.lerp(0.0, 128.0) == 128.0 + # assert -0.5.lerp(0.0, 128.0) == -64.0 + # ~~~ + fun lerp(a, b: Float): Float do return (1.0 - self) * a + self * b end redef class Collection[ E ] # Return a random element form the collection # There must be at least one element in the collection + # + # ~~~ + # var x = [1,2,3].rand + # assert x == 1 or x == 2 or x == 3 + # ~~~ fun rand: E do if is_empty then abort @@ -236,6 +257,19 @@ redef class Collection[ E ] end abort end + + # Return a new array made of elements in a random order. + # + # ~~~ + # var a = [1,2,1].to_shuffle + # assert a == [1,1,2] or a == [1,2,1] or a == [2,1,1] + # ~~~ + fun to_shuffle: Array[E] + do + var res = self.to_a + res.shuffle + return res + end end redef class SequenceRead[E] @@ -247,6 +281,36 @@ redef class SequenceRead[E] end end +redef class AbstractArray[E] + # Reorder randomly the elements in self. + # + # ~~~ + # var a = new Array[Int] + # + # a.shuffle + # assert a.is_empty + # + # a.add 1 + # a.shuffle + # assert a == [1] + # + # a.add 2 + # a.shuffle + # assert a == [1,2] or a == [2,1] + # ~~~ + # + # ENSURE self.shuffle.has_exactly(old(self)) + fun shuffle + do + for i in [0..length[ do + var j = i + (length-i).rand + var tmp = self[i] + self[i] = self[j] + self[j] = tmp + end + end +end + redef class Sys init do