public DivideResult Div(double x, double y)
 {
     if (y == 0)
     {
         return(DivideResult.ErrorResult());
     }
     else
     {
         return(DivideResult.SuccessResult(x / y));
     }
 }
Beispiel #2
0
        static DivideResult Devide(int n1, int n2) // 2번 메소드
        {
            DivideResult ret = new DivideResult(); // 아이디어는 좋은데

            if (n2.Equals(0))                      // 구조체를 할당하므로
            {
                ret.Success = false;               // 메모리를 낭비한다.
                return(ret);                       // 어차피 곧바로 쓰고
            }
            ret.Success = true;                    // 버려질 구조체인데
            ret.Result  = n1 / n2;                 // 메소드도 복잡해진다.
            return(ret);
        }
        public static DivideResult PolyLongDivision(BigRational[] dividend, BigRational[] divisor)
        {
            DivideResult divideResult = new DivideResult();

            BigRational[] bufferDividend = dividend;

            int divisorMaxPower = divisor.Length - 1;

            BigRational divisorMaxCoeff = divisor.Last();

            BigRational[] result = new BigRational[dividend.Length];

            while (true)
            {
                long dividentMaxPower = bufferDividend.Length - 1;

                long resultXPower = dividentMaxPower - divisorMaxPower;

                if (resultXPower < 0)
                {
                    divideResult.Quotient = RemoveUselessZeros(result);
                    divideResult.Remainder = RemoveUselessZeros(bufferDividend);
                    return divideResult;
                }

                BigRational dividentCoeff = bufferDividend.Last();

                BigRational resultCoeff = dividentCoeff/divisorMaxCoeff;

                result[resultXPower] = resultCoeff;

                BigRational[] buffSubtract = new BigRational[bufferDividend.Length].Select(h => new BigRational(0,1)).ToArray();

                Array.Copy(divisor, 0, buffSubtract, resultXPower, divisor.Length);

                Parallel.For(0, buffSubtract.Length, i => buffSubtract[i] *= resultCoeff);

                bufferDividend = ArraySubtrInts(bufferDividend, buffSubtract);

                bufferDividend = RemoveUselessZeros(bufferDividend);

            }
        }
        public static DivideResult PolyLongDivision(BigRational[] dividend, BigRational[] divisor)
        {
            DivideResult divideResult = new DivideResult();

            BigRational[] bufferDividend = dividend;

            int divisorMaxPower = divisor.Length - 1;

            BigRational divisorMaxCoeff = divisor.Last();

            BigRational[] result = new BigRational[dividend.Length];

            while (true)
            {
                long dividentMaxPower = bufferDividend.Length - 1;

                long resultXPower = dividentMaxPower - divisorMaxPower;

                if (resultXPower < 0)
                {
                    divideResult.Quotient  = RemoveUselessZeros(result);
                    divideResult.Remainder = RemoveUselessZeros(bufferDividend);
                    return(divideResult);
                }

                BigRational dividentCoeff = bufferDividend.Last();

                BigRational resultCoeff = dividentCoeff / divisorMaxCoeff;


                result[resultXPower] = resultCoeff;

                BigRational[] buffSubtract = new BigRational[bufferDividend.Length].Select(h => new BigRational(0, 1)).ToArray();

                Array.Copy(divisor, 0, buffSubtract, resultXPower, divisor.Length);

                Parallel.For(0, buffSubtract.Length, i => buffSubtract[i] *= resultCoeff);

                bufferDividend = ArraySubtrInts(bufferDividend, buffSubtract);

                bufferDividend = RemoveUselessZeros(bufferDividend);
            }
        }