當前位置: 首頁>>代碼示例>>C#>>正文


C# BigInteger.signum方法代碼示例

本文整理匯總了C#中BigInteger.signum方法的典型用法代碼示例。如果您正苦於以下問題:C# BigInteger.signum方法的具體用法?C# BigInteger.signum怎麽用?C# BigInteger.signum使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在BigInteger的用法示例。


在下文中一共展示了BigInteger.signum方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: gcdBinary

        /**
         * @param m a positive modulus
         * Return the greatest common divisor of op1 and op2,
         *
         * @param op1
         *            must be greater than zero
         * @param op2
         *            must be greater than zero
         * @see BigInteger#gcd(BigInteger)
         * @return {@code GCD(op1, op2)}
         */
        internal static BigInteger gcdBinary(BigInteger op1, BigInteger op2)
        {
            // PRE: (op1 > 0) and (op2 > 0)

            /*
             * Divide both number the maximal possible times by 2 without rounding
                     * gcd(2*a, 2*b) = 2 * gcd(a,b)
             */
            int lsb1 = op1.getLowestSetBit();
            int lsb2 = op2.getLowestSetBit();
            int pow2Count = java.lang.Math.min(lsb1, lsb2);

                BitLevel.inplaceShiftRight(op1, lsb1);
                BitLevel.inplaceShiftRight(op2, lsb2);

            BigInteger swap;
            // I want op2 > op1
            if (op1.compareTo(op2) == BigInteger.GREATER) {
                swap = op1;
                op1 = op2;
                op2 = swap;
            }

            do { // INV: op2 >= op1 && both are odd unless op1 = 0

                // Optimization for small operands
                // (op2.bitLength() < 64) implies by INV (op1.bitLength() < 64)
                if (( op2.numberLength == 1 )
                || ( ( op2.numberLength == 2 ) && ( op2.digits[1] > 0 ) )) {
                    op2 = BigInteger.valueOf(Division.gcdBinary(op1.longValue(),
                            op2.longValue()));
                    break;
            }

                // Implements one step of the Euclidean algorithm
                // To reduce one operand if it's much smaller than the other one
                if (op2.numberLength > op1.numberLength * 1.2) {
                    op2 = op2.remainder(op1);
                    if (op2.signum() != 0) {
                        BitLevel.inplaceShiftRight(op2, op2.getLowestSetBit());
                }
                } else {

                    // Use Knuth's algorithm of successive subtract and shifting
                    do {
                        Elementary.inplaceSubtract(op2, op1); // both are odd
                        BitLevel.inplaceShiftRight(op2, op2.getLowestSetBit()); // op2 is even
                    } while (op2.compareTo(op1) >= BigInteger.EQUALS);
                }
                // now op1 >= op2
                swap = op2;
                op2 = op1;
                op1 = swap;
            } while (op1.sign != 0);
            return op2.shiftLeft(pow2Count);
        }
開發者ID:sailesh341,項目名稱:JavApi,代碼行數:67,代碼來源:Division.cs

示例2: inplaceShiftRight

 /**
  * Performs {@code val >>= count} where {@code val} is a positive number.
  */
 internal static void inplaceShiftRight(BigInteger val, int count)
 {
     int sign = val.signum();
     if (count == 0 || val.signum() == 0)
         return;
     int intCount = count >> 5; // count of integers
     val.numberLength -= intCount;
     if (!shiftRight(val.digits, val.numberLength, val.digits, intCount,
             count & 31)
             && sign < 0) {
         // remainder not zero: add one to the result
         int i;
         for (i = 0; ( i < val.numberLength ) && ( val.digits[i] == -1 ); i++) {
             val.digits[i] = 0;
         }
         if (i == val.numberLength) {
             val.numberLength++;
         }
         val.digits[i]++;
     }
     val.cutOffLeadingZeroes();
     val.unCache();
 }
開發者ID:gadfly,項目名稱:nofs,代碼行數:26,代碼來源:java.math.BitLevel.cs


注:本文中的BigInteger.signum方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。