Exemplo n.º 1
0
 public static object ReverseMod(BigInteger x, object other)
 {
     if (other is int)
     {
         return(IntOps.Mod((int)other, x));
     }
     if (other is Complex64)
     {
         Complex64 y = (Complex64)other;
         if (y.IsZero)
         {
             throw Ops.ZeroDivisionError();
         }
         return(ComplexOps.Mod(y, Complex64.MakeReal(x)));
     }
     if (other is double)
     {
         return(FloatOps.Mod((double)other, x));
     }
     if (other is bool)
     {
         return(Mod((bool)other ? 1 : 0, x));
     }
     if (other is long)
     {
         return(Mod((long)other, x));
     }
     if (other is BigInteger)
     {
         return(Mod((BigInteger)other, x));
     }
     if (other is ExtensibleInt)
     {
         return(Mod(((ExtensibleInt)other).value, x));
     }
     if (other is ExtensibleComplex)
     {
         Complex64 y = ((ExtensibleComplex)other).value;
         if (y.IsZero)
         {
             throw Ops.ZeroDivisionError();
         }
         return(ComplexOps.Mod(y, Complex64.MakeReal(x)));
     }
     if (other is byte)
     {
         return(IntOps.Mod((int)((byte)other), x));
     }
     if (other is ExtensibleFloat)
     {
         return(FloatOps.Mod(((ExtensibleFloat)other).value, x));
     }
     return(Ops.NotImplemented);
 }
Exemplo n.º 2
0
 public static object FloorDivide(BigInteger x, object other)
 {
     if (other is int)
     {
         return(Divide(x, (int)other));
     }
     if (other is Complex64)
     {
         Complex64 y = (Complex64)other;
         if (y.IsZero)
         {
             throw Ops.ZeroDivisionError();
         }
         return(ComplexOps.FloorDivide(Complex64.MakeReal(x), y));
     }
     if (other is double)
     {
         return(FloatOps.FloorDivide(x, (double)other));
     }
     if (other is bool)
     {
         return(Divide(x, (bool)other ? 1 : 0));
     }
     if (other is long)
     {
         return(Divide(x, (long)other));
     }
     if (other is BigInteger)
     {
         return(Divide(x, (BigInteger)other));
     }
     if (other is ExtensibleInt)
     {
         return(Divide(x, ((ExtensibleInt)other).value));
     }
     if (other is ExtensibleComplex)
     {
         Complex64 y = ((ExtensibleComplex)other).value;
         if (y.IsZero)
         {
             throw Ops.ZeroDivisionError();
         }
         return(ComplexOps.FloorDivide(Complex64.MakeReal(x), y));
     }
     if (other is byte)
     {
         return(Divide(x, (int)((byte)other)));
     }
     if (other is ExtensibleFloat)
     {
         return(FloatOps.FloorDivide(x, ((ExtensibleFloat)other).value));
     }
     return(Ops.NotImplemented);
 }
Exemplo n.º 3
0
        public static Complex64 ParseComplex64(string text)
        {
            // remove no-meaning spaces
            text = text.Trim();
            if (text == string.Empty)
            {
                throw Ops.ValueError("complex() arg is an empty string");
            }
            if (text.IndexOf(' ') != -1)
            {
                throw Ops.ValueError("complex() arg is a malformed string");
            }

            try {
                int len = text.Length;

                char lastChar = text[len - 1];
                if (lastChar != 'j' && lastChar != 'J')
                {
                    return(Complex64.MakeReal(ParseFloatNoCatch(text)));
                }

                // search for sign char for the imaginary part
                int signPos = text.LastIndexOfAny(new char[] { '+', '-' });

                // it is possible the sign belongs to 'e'
                if (signPos > 1)
                {
                    char prevChar = text[signPos - 1];
                    if (prevChar == 'e' || prevChar == 'E')
                    {
                        signPos = text.Substring(0, signPos - 1).LastIndexOfAny(new char[] { '+', '-' });
                    }
                }

                if (signPos == -1)
                {
                    // special: "j"
                    return(Complex64.MakeImaginary(len == 1 ? 1 : ParseFloatNoCatch(text.Substring(0, len - 1))));
                }
                else
                {
                    // special: "+j", "-j"
                    return(new Complex64(
                               signPos == 0 ? 0 : ParseFloatNoCatch(text.Substring(0, signPos)),
                               (len == signPos + 2) ? (text[signPos] == '+' ? 1 : -1) : ParseFloatNoCatch(text.Substring(signPos, len - signPos - 1))));
                }
            } catch (OverflowException) {
                throw Ops.ValueError("complex() literal too large to convert");
            } catch {
                throw Ops.ValueError("complex() arg is a malformed string");
            }
        }
 public static object Subtract(double x, object other)
 {
     if (other is double)
     {
         return(x - ((double)other));
     }
     if (other is int)
     {
         return(x - ((int)other));
     }
     if (other is Complex64)
     {
         return(ComplexOps.Subtract(Complex64.MakeReal(x), (Complex64)other));
     }
     if (other is BigInteger)
     {
         return(x - ((BigInteger)other));
     }
     if (other is float)
     {
         return(x - ((float)other));
     }
     if (other is ExtensibleFloat)
     {
         return(x - ((ExtensibleFloat)other).value);
     }
     if (other is string)
     {
         return(Ops.NotImplemented);
     }
     if (other is IConvertible)
     {
         double y = ((IConvertible)other).ToDouble(null);
         return(x - y);
     }
     if (other is long)
     {
         return(x - ((long)other));
     }
     if (other is ExtensibleInt)
     {
         return(x - ((ExtensibleInt)other).value);
     }
     if (other is ExtensibleComplex)
     {
         return(ComplexOps.Subtract(Complex64.MakeReal(x), ((ExtensibleComplex)other).value));
     }
     return(Ops.NotImplemented);
 }
Exemplo n.º 5
0
 static Complex64 ConvertToComplex(object o)
 {
     if (o is Complex64)
     {
         return((Complex64)o);
     }
     else if (o is ComplexFraction)
     {
         return((ComplexFraction)o);
     }
     else
     {
         return(Complex64.MakeReal(ConvertToReal(o)));
     }
 }
 public static object TrueDivide(double x, object other)
 {
     if (other is double)
     {
         return(TrueDivide(x, ((double)other)));
     }
     if (other is int)
     {
         return(TrueDivide(x, ((int)other)));
     }
     if (other is Complex64)
     {
         return(ComplexOps.TrueDivide(Complex64.MakeReal(x), (Complex64)other));
     }
     if (other is BigInteger)
     {
         return(TrueDivide(x, ((BigInteger)other)));
     }
     if (other is bool)
     {
         return(TrueDivide(x, (bool)other ? 1.0 : 0.0));
     }
     if (other is float)
     {
         return(TrueDivide(x, ((float)other)));
     }
     if (other is ExtensibleFloat)
     {
         return(TrueDivide(x, ((ExtensibleFloat)other).value));
     }
     if (other is long)
     {
         return(TrueDivide(x, ((long)other)));
     }
     if (other is ExtensibleComplex)
     {
         return(ComplexOps.TrueDivide(Complex64.MakeReal(x), ((ExtensibleComplex)other).value));
     }
     if (other is ExtensibleInt)
     {
         return(TrueDivide(x, ((ExtensibleInt)other).value));
     }
     if (other is byte)
     {
         return(TrueDivide(x, (int)((byte)other)));
     }
     return(Ops.NotImplemented);
 }
Exemplo n.º 7
0
        public static object Mod(double x, object other)
        {
            if (other is int)
            {
                int y = (int)other;
                if (y == 0)
                {
                    throw Ops.ZeroDivisionError();
                }
                return(Modulo(x, y));
            }
            if (other is long)
            {
                long y = (long)other;
                if (y == 0)
                {
                    throw Ops.ZeroDivisionError();
                }
                return(Modulo(x, y));
            }
            if (other is Complex64)
            {
                Complex64 y = (Complex64)other;
                if (y.IsZero)
                {
                    throw Ops.ZeroDivisionError();
                }
                return(ComplexOps.Mod(Complex64.MakeReal(x), y));
            }
            if (other is double)
            {
                double y = (double)other;
                if (y == 0)
                {
                    throw Ops.ZeroDivisionError();
                }
                return(Modulo(x, y));
            }
            if (other is BigInteger)
            {
                BigInteger y = (BigInteger)other;
                if (y == BigInteger.Zero)
                {
                    throw Ops.ZeroDivisionError();
                }
                return(Modulo(x, y));
            }
            if (other is ExtensibleFloat)
            {
                ExtensibleFloat y = (ExtensibleFloat)other;
                if (y.value == 0)
                {
                    throw Ops.ZeroDivisionError();
                }
                return(Modulo(x, y.value));
            }
            if (other is ExtensibleInt)
            {
                int y = ((ExtensibleInt)other).value;
                if (y == 0)
                {
                    throw Ops.ZeroDivisionError();
                }
                return(Modulo(x, y));
            }
            if (other is ExtensibleComplex)
            {
                Complex64 y = ((ExtensibleComplex)other).value;
                if (y.IsZero)
                {
                    throw Ops.ZeroDivisionError();
                }
                return(ComplexOps.Mod(Complex64.MakeReal(x), y));
            }
            if (other is IConvertible)
            {
                double y = ((IConvertible)other).ToDouble(null);
                if (y == 0)
                {
                    throw Ops.ZeroDivisionError();
                }
                return(Modulo(x, y));
            }

            return(Ops.NotImplemented);
        }
        public static object Mod(long x, object other)
        {
            if (other is int)
            {
                int y = (int)other;
                try {
                    return(Ops.Long2Object(Mod(x, y)));
                } catch (OverflowException) {
                    return(BigInteger.Create(x) % y);
                }
            }
            else if (other is BigInteger)
            {
                return(LongOps.Mod(BigInteger.Create(x), (BigInteger)other));
            }
            else if (other is double)
            {
                return(FloatOps.Mod(x, (double)other));
            }
            else if (other is Complex64)
            {
                return(ComplexOps.Mod(Complex64.MakeReal(x), (Complex64)other));
            }
            else if (other is bool)
            {
                int y = (bool)other ? 1 : 0;
                try {
                    return(Ops.Long2Object(Mod(x, y)));
                } catch (OverflowException) {
                    return(BigInteger.Create(x) % y);
                }
            }
            else if (other is long)
            {
                long y = (long)other;
                try {
                    return(Mod(x, y));
                } catch (OverflowException) {
                    return(BigInteger.Create(x) % y);
                }
            }
            else if (other is float)
            {
                return(FloatOps.Mod(x, (float)other));
            }
            else if (other is ExtensibleInt)
            {
                int y = ((ExtensibleInt)other).value;
                try {
                    return(Ops.Long2Object(Mod(x, y)));
                } catch (OverflowException) {
                    return(BigInteger.Create(x) % y);
                }
            }
            else if (other is ExtensibleFloat)
            {
                return(FloatOps.Mod(x, ((ExtensibleFloat)other).value));
            }
            else if (other is ExtensibleComplex)
            {
                return(ComplexOps.Mod(Complex64.MakeReal(x), ((ExtensibleComplex)other).value));
            }
            else if (other is byte)
            {
                int y = (int)((byte)other);
                try {
                    return(Ops.Long2Object(Mod(x, y)));
                } catch (OverflowException) {
                    return(BigInteger.Create(x) % y);
                }
            }

            return(Ops.NotImplemented);
        }
 public static object Multiply(long x, object other)
 {
     if (other is int)
     {
         int y = (int)other;
         try {
             return(Ops.Long2Object(checked (x * y)));
         } catch (OverflowException) {
             return(BigInteger.Create(x) * y);
         }
     }
     else if (other is BigInteger)
     {
         return(BigInteger.Create(x) * (BigInteger)other);
     }
     else if (other is double)
     {
         return(x * (double)other);
     }
     else if (other is Complex64)
     {
         return(Complex64.MakeReal(x) * (Complex64)other);
     }
     else if (other is bool)
     {
         int y = (bool)other ? 1 : 0;
         try {
             return(Ops.Long2Object(checked (x * y)));
         } catch (OverflowException) {
             return(BigInteger.Create(x) * y);
         }
     }
     else if (other is long)
     {
         long y = (long)other;
         try {
             return(checked (x * y));
         } catch (OverflowException) {
             return(BigInteger.Create(x) * y);
         }
     }
     else if (other is float)
     {
         return(x * (float)other);
     }
     else if (other is ExtensibleInt)
     {
         int y = ((ExtensibleInt)other).value;
         try {
             return(Ops.Long2Object(checked (x * y)));
         } catch (OverflowException) {
             return(BigInteger.Create(x) * y);
         }
     }
     else if (other is ExtensibleFloat)
     {
         return(x * ((ExtensibleFloat)other).value);
     }
     else if (other is ExtensibleComplex)
     {
         return(Complex64.MakeReal(x) * ((ExtensibleComplex)other).value);
     }
     else if (other is byte)
     {
         int y = (int)((byte)other);
         try {
             return(Ops.Long2Object(checked (x * y)));
         } catch (OverflowException) {
             return(BigInteger.Create(x) * y);
         }
     }
     return(Ops.NotImplemented);
 }
Exemplo n.º 10
0
 private static object TrueDivide(int x, Complex64 y)
 {
     return(ComplexOps.TrueDivide(Complex64.MakeReal(x), y));
 }
Exemplo n.º 11
0
 public static object Mod(int x, object other)
 {
     if (other is int)
     {
         int y = (int)other;
         try {
             return(Ops.Int2Object(Mod(x, y)));
         } catch (OverflowException) {
             return(LongOps.Mod(BigInteger.Create(x), y));
         }
     }
     else if (other is BigInteger)
     {
         return(LongOps.Mod(BigInteger.Create(x), (BigInteger)other));
     }
     else if (other is double)
     {
         return(FloatOps.Mod(x, (double)other));
     }
     else if (other is Complex64)
     {
         Complex64 y = (Complex64)other;
         if (y.IsZero)
         {
             throw Ops.ZeroDivisionError();
         }
         return(ComplexOps.Mod(Complex64.MakeReal(x), y));
     }
     else if (other is bool)
     {
         bool b = (bool)other;
         return(x % (b ? 1 : 0));
     }
     else if (other is long)
     {
         long y = (long)other;
         try {
             return(Mod(x, y));
         } catch (OverflowException) {
             return(LongOps.Mod(BigInteger.Create(x), y));
         }
     }
     else if (other is float)
     {
         return(FloatOps.Mod(x, (float)other));
     }
     else if (other is byte)
     {
         return(Ops.Int2Object(Mod(x, (int)((byte)other))));
     }
     else if (other is ExtensibleInt)
     {
         int y = ((ExtensibleInt)other).value;
         try {
             return(Ops.Int2Object(Mod(x, y)));
         } catch (OverflowException) {
             return(LongOps.Mod(BigInteger.Create(x), y));
         }
     }
     else if (other is ExtensibleFloat)
     {
         return(FloatOps.Mod(x, ((ExtensibleFloat)other).value));
     }
     else if (other is ExtensibleComplex)
     {
         Complex64 y = ((ExtensibleComplex)other).value;
         if (y.IsZero)
         {
             throw Ops.ZeroDivisionError();
         }
         return(ComplexOps.Mod(Complex64.MakeReal(x), y));
     }
     return(Ops.NotImplemented);
 }
Exemplo n.º 12
0
 public static object Add(int x, object other)
 {
     if (other is int)
     {
         int y = (int)other;
         try {
             return(Ops.Int2Object(checked (x + y)));
         } catch (OverflowException) {
             return(BigInteger.Create(x) + y);
         }
     }
     else if (other is BigInteger)
     {
         return(BigInteger.Create(x) + (BigInteger)other);
     }
     else if (other is double)
     {
         return(x + (double)other);
     }
     else if (other is Complex64)
     {
         return(ComplexOps.Add(Complex64.MakeReal(x), other));
     }
     else if (other is bool)
     {
         bool b = (bool)other;
         return(x + (b ? 1 : 0));
     }
     else if (other is long)
     {
         long y = (long)other;
         try {
             return(checked (x + y));
         } catch (OverflowException) {
             return(BigInteger.Create(x) + y);
         }
     }
     else if (other is float)
     {
         return(x + (float)other);
     }
     else if (other is byte)
     {
         return(x + (byte)other);
     }
     else if (other is ExtensibleInt)
     {
         int y = ((ExtensibleInt)other).value;
         try {
             return(Ops.Int2Object(checked (x + y)));
         } catch (OverflowException) {
             return(BigInteger.Create(x) + y);
         }
     }
     else if (other is ExtensibleFloat)
     {
         return(x + ((ExtensibleFloat)other).value);
     }
     else if (other is ExtensibleComplex)
     {
         return(ComplexOps.Add(Complex64.MakeReal(x), (ExtensibleComplex)other));
     }
     else if (other is byte)
     {
         int y = (byte)other;
         try {
             return(Ops.Int2Object(checked (x + y)));
         } catch (OverflowException) {
             return(BigInteger.Create(x) + y);
         }
     }
     return(Ops.NotImplemented);
 }