lib/standard: Remove lshift and rshift from Int and Byte
[nit.git] / lib / standard / math.nit
index d503b43..3e02ebe 100644 (file)
@@ -4,30 +4,70 @@
 #
 # This file is free software, which comes along with NIT.  This software is
 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
-# without  even  the implied warranty of  MERCHANTABILITY or  FITNESS FOR A 
+# without  even  the implied warranty of  MERCHANTABILITY or  FITNESS FOR A
 # PARTICULAR PURPOSE.  You can modify it is you want,  provided this header
 # is kept unaltered, and a notification of the changes is added.
 # You  are  allowed  to  redistribute it and sell it, alone or is a part of
 # another product.
 
 # Mathematical operations
-module math
+module math is ldflags "-lm"
 
 import kernel
 import collection
 
 in "C header" `{
-#include <math.h>
+       #include <stdlib.h>
+       #include <math.h>
+       #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 is extern "kernel_Int_Int_rand_0"
+       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 is extern "kernel_Int_Int_binand_0"
+       fun bin_and(i: Int): Int `{ return self & i; `}
 
        # Alias of `bin_and`
        fun &(i: Int): Int do return bin_and(i)
@@ -35,7 +75,7 @@ redef class Int
        # Returns the result of a binary OR operation on `self` and `i`
        #
        #     assert 0x10.bin_or(0x01) == 0x11
-       fun bin_or(i: Int): Int is extern "kernel_Int_Int_binor_0"
+       fun bin_or(i: Int): Int `{ return self | i; `}
 
        # Alias of `bin_or`
        fun |(i: Int): Int do return bin_or(i)
@@ -43,7 +83,7 @@ redef class Int
        # Returns the result of a binary XOR operation on `self` and `i`
        #
        #     assert 0x101.bin_xor(0x110) == 0x11
-       fun bin_xor(i: Int): Int is extern "kernel_Int_Int_binxor_0"
+       fun bin_xor(i: Int): Int `{ return self ^ i; `}
 
        # Alias of `bin_xor`
        fun ^(i: Int): Int do return bin_xor(i)
@@ -51,7 +91,7 @@ redef class Int
        # Returns the 1's complement of `self`
        #
        #     assert 0x2F.bin_not == -48
-       fun bin_not: Int is extern "kernel_Int_Int_binnot_0"
+       fun bin_not: Int `{ return ~self; `}
 
        # Alias of `bin_not`
        fun ~: Int do return bin_not
@@ -76,14 +116,14 @@ redef class Int
                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)
+                               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 ?
@@ -96,6 +136,24 @@ redef class Int
        #     assert not 13.is_even
        fun is_odd: Bool do return not is_even
 
+       # Is self a prime number ?
+       #
+       # assert 3.is_prime
+       # assert not 1.is_prime
+       # assert not 12.is_prime
+       fun is_prime: Bool
+       do
+               if self == 2 then
+                       return true
+               else if self <= 1 or self.is_even then
+                       return false
+               end
+               for i in [3..self.sqrt[ do
+                       if self % i == 0 then return false
+               end
+               return true
+       end
+
        # Returns the `self` raised to the power of `e`.
        #
        #     assert 2 ** 3 == 8
@@ -125,6 +183,40 @@ redef class Int
        end
 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)
+
+       # 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)
+
+       # 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)
+
+       # 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
+end
+
 redef class Float
 
        # Returns the non-negative square root of `self`.
@@ -133,37 +225,37 @@ redef class Float
        #     #assert 3.0.sqrt == 1.732
        #     assert 1.0.sqrt == 1.0
        #     assert 0.0.sqrt == 0.0
-       fun sqrt: Float is extern "kernel_Float_Float_sqrt_0"
+       fun sqrt: Float `{ return sqrt(self); `}
 
        # Computes the cosine of `self` (expressed in radians).
        #
        #     #assert pi.cos == -1.0
-       fun cos: Float is extern "kernel_Float_Float_cos_0"
+       fun cos: Float `{ return cos(self); `}
 
        # Computes the sine of `self` (expressed in radians).
        #
        #     #assert pi.sin == 0.0
-       fun sin: Float is extern "kernel_Float_Float_sin_0"
+       fun sin: Float `{ return sin(self); `}
 
        # Computes the cosine of x (expressed in radians).
        #
        #     #assert 0.0.tan == 0.0
-       fun tan: Float is extern "kernel_Float_Float_tan_0"
+       fun tan: Float `{ return tan(self); `}
 
        # Computes the arc cosine of `self`.
        #
        #     #assert 0.0.acos == pi / 2.0
-       fun acos: Float is extern "kernel_Float_Float_acos_0"
+       fun acos: Float `{ return acos(self); `}
 
        # Computes the arc sine of `self`.
        #
        #     #assert 1.0.asin == pi / 2.0
-       fun asin: Float is extern "kernel_Float_Float_asin_0"
+       fun asin: Float `{ return asin(self); `}
 
        # Computes the arc tangent of `self`.
        #
        #     #assert 0.0.tan == 0.0
-       fun atan: Float is extern "kernel_Float_Float_atan_0"
+       fun atan: Float `{ return atan(self); `}
 
        # Returns the absolute value of `self`.
        #
@@ -177,13 +269,13 @@ redef class Float
        #     #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 is extern "kernel_Float_Float_pow_1"
+       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
-       fun log: Float is extern "kernel_Float_Float_log_0"
+       fun log: Float `{ return log(self); `}
 
        # Logarithm of `self` to base `base`.
        #
@@ -192,7 +284,7 @@ redef class Float
        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"
+       fun exp: Float `{ return exp(self); `}
 
        #     assert 1.1.ceil == 2.0
        #     assert 1.9.ceil == 2.0
@@ -212,16 +304,27 @@ redef class Float
        #     assert 1.34.round == 1.0
        #     assert -1.34.round == -1.0
        #     assert -1.67.round == -2.0
-       fun round: Float is extern "round"
+       fun round: Float `{ return round(self); `}
 
        # Returns a random `Float` in `[0.0 .. self[`.
-       fun rand: Float is extern "kernel_Float_Float_rand_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 is extern "hypotf"
+       fun hypot_with(b: Float): Float `{ return hypotf(self, b); `}
 
        # Returns true is self is not a number.
-       fun is_nan: Bool is extern "isnan"
+       #
+       # 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
        # this function returns:
@@ -229,15 +332,21 @@ 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 is_inf_extern then
+               if native_is_inf then
                        if self < 0.0 then return -1
                        return 1
                end
                return 0
        end
 
-       private fun is_inf_extern: Bool is extern "isinf"
+       private fun native_is_inf: Bool `{ return isinf(self); `}
 
        # Linear interpolation between `a` and `b` using `self` as weight
        #
@@ -250,6 +359,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
@@ -334,10 +479,10 @@ end
 #
 #     assert atan2(-0.0, 1.0) == -0.0
 #     assert atan2(0.0, 1.0) == 0.0
-fun atan2(x: Float, y: Float): Float is extern "kernel_Any_Any_atan2_2"
+fun atan2(x: Float, y: Float): Float `{ return atan2(x, y); `}
 
 # Approximate value of **pi**.
-fun pi: Float is extern "kernel_Any_Any_pi_0"
+fun pi: Float do return 3.14159265
 
 # Initialize the pseudo-random generator with the given seed.
 # The pseudo-random generator is used by the method `rand` and other to generate sequence of numbers.
@@ -351,9 +496,9 @@ fun pi: Float is extern "kernel_Any_Any_pi_0"
 # assert 10.rand == a
 # assert 100.rand == b
 # ~~~~
-fun srand_from(x: Int) is extern "kernel_Any_Any_srand_from_1"
+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 is extern "kernel_Any_Any_srand_0"
+fun srand `{ nit_rand_seeded = 0; srand(time(NULL)); `}