lib/standard/: Removed bin_and/or/xor/not from math
[nit.git] / lib / standard / math.nit
index 4787644..50b36e9 100644 (file)
@@ -22,43 +22,67 @@ in "C header" `{
        #include <time.h>
 `}
 
+in "C" `{
+       /* Is rand shortcut? */
+       static int nit_rand_seeded;
+       /* Current rand seed if seeded */
+       static unsigned int nit_rand_seed;
+
+       #define NIT_RAND_MAX 0x7fffffff
+
+       /* This algorithm is mentioned in the ISO C standard, here extended
+       for 32 bits.  */
+       static int
+       nit_rand(void) {
+               unsigned int next = nit_rand_seed;
+               int result;
+
+               next *= 1103515245;
+               next += 12345;
+               result = (unsigned int) (next / 65536) % 2048;
+
+               next *= 1103515245;
+               next += 12345;
+               result <<= 10;
+               result ^= (unsigned int) (next / 65536) % 1024;
+
+               next *= 1103515245;
+               next += 12345;
+               result <<= 10;
+               result ^= (unsigned int) (next / 65536) % 1024;
+
+               nit_rand_seed = next;
+
+               return result;
+       }
+`}
+
 redef class Int
        # Returns a random `Int` in `[0 .. self[`.
        fun rand: Int `{
+               if (nit_rand_seeded) return (long)(((double)self)*nit_rand()/(NIT_RAND_MAX+1.0));
                return (long)(((double)self)*rand()/(RAND_MAX+1.0));
        `}
 
        # Returns the result of a binary AND operation on `self` and `i`
        #
-       #     assert 0x10.bin_and(0x01) == 0
-       fun bin_and(i: Int): Int `{ return self & i; `}
-
-       # Alias of `bin_and`
-       fun &(i: Int): Int do return bin_and(i)
+       #     assert 0x10 & 0x01 == 0
+       fun &(i: Int): Int `{ return self & i; `}
 
        # Returns the result of a binary OR operation on `self` and `i`
        #
-       #     assert 0x10.bin_or(0x01) == 0x11
-       fun bin_or(i: Int): Int `{ return self | i; `}
-
-       # Alias of `bin_or`
-       fun |(i: Int): Int do return bin_or(i)
+       #     assert 0x10 | 0x01 == 0x11
+       fun |(i: Int): Int `{ return self | i; `}
 
        # Returns the result of a binary XOR operation on `self` and `i`
        #
-       #     assert 0x101.bin_xor(0x110) == 0x11
-       fun bin_xor(i: Int): Int `{ return self ^ i; `}
-
-       # Alias of `bin_xor`
-       fun ^(i: Int): Int do return bin_xor(i)
+       #     assert 0x101 ^ 0x110 == 0x11
+       fun ^(i: Int): Int `{ return self ^ i; `}
 
        # Returns the 1's complement of `self`
        #
-       #     assert 0x2F.bin_not == -48
-       fun bin_not: Int `{ return ~self; `}
-
-       # Alias of `bin_not`
-       fun ~: Int do return bin_not
+       #     assert ~0x2F == -48
+       fun ~: Int `{ return ~self; `}
 
        # Returns the square root of `self`
        #
@@ -78,16 +102,16 @@ redef class Int
                if o < 0 then return -(self.gcd(-o))
                if self == 0 or o == self then return o
                if o == 0 then return self
-               if self.bin_and(1) == 0 then
-                       if o.bin_and(1) == 1 then
-                               return self.rshift(1).gcd(o)
+               if self & 1 == 0 then
+                       if o & 1 == 1 then
+                               return (self >> 1).gcd(o)
                        else
-                               return self.rshift(1).gcd(o.rshift(1)).lshift(1)
+                               return (self >> 1).gcd(o >> 1) << 1
                        end
                end
-               if o.bin_and(1) == 0 then return self.gcd(o.rshift(1))
-               if self > o then return (self - o).rshift(1).gcd(o)
-               return (o - self).rshift(1).gcd(self)
+               if o & 1 == 0 then return self.gcd(o >> 1)
+               if self > o then return ((self - o) >> 1).gcd(o)
+               return ((o - self) >> 1).gcd(self)
        end
 
        # Is `self` even ?
@@ -150,35 +174,23 @@ end
 redef class Byte
        # Returns the result of a binary AND operation on `self` and `i`
        #
-       #     assert 0x10.bin_and(0x01) == 0
-       fun bin_and(i: Byte): Byte `{ return self & i; `}
-
-       # Alias of `bin_and`
-       fun &(i: Byte): Byte do return bin_and(i)
+       #     assert 0x10u8 & 0x01u8 == 0u8
+       fun &(i: Byte): Byte `{ return self & i; `}
 
        # Returns the result of a binary OR operation on `self` and `i`
        #
-       #     assert 0x10.bin_or(0x01) == 0x11
-       fun bin_or(i: Byte): Byte `{ return self | i; `}
-
-       # Alias of `bin_or`
-       fun |(i: Byte): Byte do return bin_or(i)
+       #     assert 0x10u8 | 0x01u8 == 0x11u8
+       fun |(i: Byte): Byte `{ return self | i; `}
 
        # Returns the result of a binary XOR operation on `self` and `i`
        #
-       #     assert 0x101.bin_xor(0x110) == 0x11
-       fun bin_xor(i: Byte): Byte `{ return self ^ i; `}
-
-       # Alias of `bin_xor`
-       fun ^(i: Byte): Byte do return bin_xor(i)
+       #     assert 0x101u8 ^ 0x110u8 == 0x11u8
+       fun ^(i: Byte): Byte `{ return self ^ i; `}
 
        # Returns the 1's complement of `self`
        #
-       #     assert 0x2F.bin_not == -48
-       fun bin_not: Byte `{ return ~self; `}
-
-       # Alias of `bin_not`
-       fun ~: Byte do return bin_not
+       #     assert ~0x2Fu8 == 0xD0u8
+       fun ~: Byte `{ return ~self; `}
 end
 
 redef class Float
@@ -271,12 +283,23 @@ redef class Float
        fun round: Float `{ return round(self); `}
 
        # Returns a random `Float` in `[0.0 .. self[`.
-       fun rand: Float `{ return ((self)*rand())/(RAND_MAX+1.0); `}
+       fun rand: Float `{
+               if (nit_rand_seeded) return ((self)*nit_rand())/(NIT_RAND_MAX+1.0);
+               return ((self)*rand())/(RAND_MAX+1.0);
+       `}
 
        # Returns the euclidean distance from `b`.
        fun hypot_with(b: Float): Float `{ return hypotf(self, b); `}
 
        # Returns true is self is not a number.
+       #
+       # As `nan != nan`, `is_nan` should be used to test if a float is the special *not a number* value.
+       #
+       # ~~~
+       # assert nan != nan # By IEEE 754
+       # assert nan.is_nan
+       # assert not 10.0.is_nan
+       # ~~~
        fun is_nan: Bool `{ return isnan(self); `}
 
        # Is the float an infinite value
@@ -285,6 +308,12 @@ redef class Float
        #  * 1 if self is positive infinity
        #  * -1 if self is negative infinity
        #  * 0 otherwise
+       #
+       # ~~~
+       # assert 10.0.is_inf == 0
+       # assert inf.is_inf == 1
+       # assert (-inf).is_inf == -1
+       # ~~~
        fun is_inf: Int do
                if native_is_inf then
                        if self < 0.0 then return -1
@@ -306,6 +335,42 @@ redef class Float
        fun lerp(a, b: Float): Float do return (1.0 - self) * a + self * b
 end
 
+# Positive float infinite (IEEE 754)
+#
+#     assert inf > 10.0
+#     assert inf.is_inf == 1
+#
+# `inf` follows the arithmetic of infinites
+#
+#     assert (inf - 1.0) == inf
+#     assert (inf - inf).is_nan
+#
+# The negative infinite can be used as `-inf`.
+#
+#     assert -inf < -10.0
+#     assert (-inf).is_inf == -1
+fun inf: Float do return 1.0 / 0.0
+
+# Not a Number, representation of an undefined or unrepresentable float (IEEE 754).
+#
+# `nan` is not comparable with itself, you should use `Float::is_nan` to test it.
+#
+# ~~~
+# assert nan.is_nan
+# assert nan != nan # By IEEE 754
+# ~~~
+#
+# `nan` is the quiet result of some undefined operations.
+#
+# ~~~
+# assert (1.0 + nan).is_nan
+# assert (0.0 / 0.0).is_nan
+# assert (inf - inf).is_nan
+# assert (inf / inf).is_nan
+# assert (-1.0).sqrt.is_nan
+# ~~~
+fun nan: Float do return 0.0 / 0.0
+
 redef class Collection[ E ]
        # Return a random element form the collection
        # There must be at least one element in the collection
@@ -407,9 +472,9 @@ fun pi: Float do return 3.14159265
 # assert 10.rand == a
 # assert 100.rand == b
 # ~~~~
-fun srand_from(x: Int) `{ srand(x); `}
+fun srand_from(x: Int) `{ nit_rand_seeded = 1; nit_rand_seed = x; `}
 
 # Reinitialize the pseudo-random generator used by the method `rand` and other.
 # This method is automatically invoked at the begin of the program, so usually, there is no need to manually invoke it.
 # The only exception is in conjunction with `srand_from` to reset the pseudo-random generator.
-fun srand `{ srand(time(NULL)); `}
+fun srand `{ nit_rand_seeded = 0; srand(time(NULL)); `}