lib/core/stream: LineIterator use CachedIterator
[nit.git] / lib / core / math.nit
index 7b51e2a..a7eefbe 100644 (file)
@@ -126,9 +126,9 @@ redef class Int
 
        # Is self a prime number ?
        #
-       # assert 3.is_prime
-       # assert not 1.is_prime
-       # assert not 15.is_prime
+       #       assert 3.is_prime
+       #       assert not 1.is_prime
+       #       assert not 15.is_prime
        fun is_prime: Bool
        do
                if self == 2 then
@@ -252,15 +252,15 @@ redef class Float
 
        # Returns `self` raised at `e` power.
        #
-       #     #assert 2.0.pow(0.0) == 1.0
-       #     #assert 2.0.pow(3.0) == 8.0
-       #     #assert 0.0.pow(9.0) == 0.0
+       #     assert 2.0.pow(0.0) == 1.0
+       #     assert 2.0.pow(3.0) == 8.0
+       #     assert 0.0.pow(9.0) == 0.0
        fun pow(e: Float): Float `{ return pow(self, e); `}
 
        # Natural logarithm of `self`.
        #
        #     assert 0.0.log.is_inf == -1
-       #     #assert 1.0.log == 0.0
+       #     assert 1.0.log == 0.0
        fun log: Float `{ return log(self); `}
 
        # Logarithm of `self` to base `base`.
@@ -420,6 +420,17 @@ fun inf: Float do return 1.0 / 0.0
 # ~~~
 fun nan: Float do return 0.0 / 0.0
 
+redef class Comparable
+       # Constraint `self` within `[min..max]`
+       #
+       #     assert 1.clamp(5, 10) == 5
+       #     assert 7.clamp(5, 10) == 7
+       #     assert 15.clamp(5, 10) == 10
+       #     assert 1.5.clamp(1.0, 2.0) == 1.5
+       #     assert "a".clamp("b", "c") == "b"
+       fun clamp(min, max: OTHER): OTHER do return self.max(min).min(max)
+end
+
 redef class Collection[ E ]
        # Return a random element form the collection
        # There must be at least one element in the collection