Jurassic.BigInteger.Compare C# (CSharp) Method

Compare() public static method

Returns -1 if a < b, 0 if they are the same, or 1 if a > b.
public static Compare ( BigInteger a, BigInteger b ) : int
a BigInteger
b BigInteger
return int
        public static int Compare(BigInteger a, BigInteger b)
        {
            if (a.sign != b.sign)
                return a.sign < b.sign ? -1 : 1;
            if (a.sign > 0)
            {
                // Comparison of positive numbers.
                if (a.wordCount < b.wordCount)
                    return -1;
                if (a.wordCount > b.wordCount)
                    return 1;
                for (int i = a.wordCount - 1; i >= 0; i--)
                {
                    if (a.bits[i] != b.bits[i])
                        return a.bits[i] < b.bits[i] ? -1 : 1;
                }
            }
            else if (a.sign < 0)
            {
                // Comparison of negative numbers.
                if (a.wordCount < b.wordCount)
                    return 1;
                if (a.wordCount > b.wordCount)
                    return -1;
                for (int i = a.wordCount - 1; i >= 0; i--)
                {
                    if (a.bits[i] != b.bits[i])
                        return a.bits[i] < b.bits[i] ? 1 : -1;
                }
            }
            return 0;
        }

Usage Example

Beispiel #1
0
        /// <summary>
        /// Converts the numeric value of the current BigInteger object to its equivalent string
        /// representation.
        /// </summary>
        /// <returns> The string representation of the current BigInteger value. </returns>
        public override string ToString()
        {
            if (BigInteger.Equals(this, Zero))
            {
                return("0");
            }

            var result = new System.Text.StringBuilder();
            var value  = this;

            if (value.Sign < 0)
            {
                result.Append('-');
                value.sign = 1;
            }

            // Overestimate of Floor(Log10(value))
            int log10 = (int)Math.Floor(Log(value, 10)) + 1;

            var divisor = Pow(10, log10);

            // Adjust the values so that Quorum works.
            SetupQuorum(ref value, ref divisor);

            // Check for overestimate of log10.
            if (BigInteger.Compare(divisor, value) > 0)
            {
                value = BigInteger.MultiplyAdd(value, 10, 0);
                log10--;
            }

            for (int i = 0; i <= log10; i++)
            {
                // value = value / divisor
                int digit = Quorem(ref value, divisor);

                // Append the digit.
                result.Append((char)(digit + '0'));

                // value = value * 10;
                value = BigInteger.MultiplyAdd(value, 10, 0);
            }
            return(result.ToString());
        }
All Usage Examples Of Jurassic.BigInteger::Compare