Esempio n. 1
0
        /// <summary>
        /// Computer the quotient and remainder of two 128-bit integers.
        /// </summary>
        /// <param name="x">The dividend.</param>
        /// <param name="y">The divisor.</param>
        /// <param name="r">The remainder <paramref name="x"/> % <paramref name="y"/>.</param>
        /// <returns>The quotient <paramref name="x"/> / <paramref name="y"/>.</returns>
        public static Int128 DivRem(Int128 x, Int128 y, out Int128 r)
        {
            bool    xNegative = false;
            UInt128 xu        = x.u;

            if (xu.IsNegative)
            {
                xu        = xu.Negate();
                xNegative = true;
            }

            bool    yNegative = false;
            UInt128 yu        = y.u;

            if (yu.IsNegative)
            {
                yu        = yu.Negate();
                yNegative = true;
            }

            UInt128 qu = UInt128.DivRem(xu, yu, out UInt128 ru);

            if (xNegative ^ yNegative)
            {
                qu = qu.Negate();
            }
            if (xNegative)
            {
                ru = ru.Negate();
            }

            r = new Int128(ru);
            return(new Int128(qu));
        }
Esempio n. 2
0
        /// <summary>
        /// Computes the quotient of two 128 bit integers.
        /// </summary>
        /// <param name="x">The dividend.</param>
        /// <param name="y">The divisor.</param>
        /// <returns>The quotient <paramref name="x"/> / <paramref name="y"/>.</returns>
        public static Int128 operator /(Int128 x, Int128 y)
        {
            bool negate = false;

            UInt128 xu = x.u;

            if (xu.IsNegative)
            {
                xu     = xu.Negate();
                negate = !negate;
            }

            UInt128 yu = y.u;

            if (yu.IsNegative)
            {
                yu     = yu.Negate();
                negate = !negate;
            }

            UInt128 qu = xu / yu;

            if (negate)
            {
                qu = qu.Negate();
            }

            return(new Int128(qu));
        }
Esempio n. 3
0
        // Serialization and deserialization

        /// <summary>
        /// Produces a string representation of the 128-bit integer.
        /// </summary>
        /// <returns>A string containing the base-10 representation of the 128-bit integer value.</returns>
        public override string ToString()
        {
            if (u.IsNegative)
            {
                UInt128 t = u.Negate();
                return("-" + t.ToString());
            }
            else
            {
                return(u.ToString());
            }
        }
Esempio n. 4
0
        // Absolute Value

        /// <summary>
        /// Gets the absolute value of a 128 bit integer.
        /// </summary>
        /// <param name="x">The argument.</param>
        /// <returns>The absolute value of the argument.</returns>
        /// <remarks><para>Because <see cref="Int128.MinValue"/> is one unit smaller in absolute value than <see cref="Int128.MaxValue"/>, this
        /// method throws an <see cref="OverflowException"/> when passed <see cref="Int128.MinValue"/>. Built in types such as <see cref="Int32"/>
        /// have analogous behavior. All other values are supported.</para></remarks>
        /// <exception cref="OverflowException"><paramref name="x"/> was <see cref="Int128.MinValue"/>, which has no corresponding positive
        /// value in the range of the type.</exception>
        public static Int128 Abs(Int128 x)
        {
            UInt128 xu = x.u;

            if (xu.IsNegative)
            {
                xu = xu.Negate();
                if (xu.IsNegative)
                {
                    throw new OverflowException();
                }
            }
            Debug.Assert(!xu.IsNegative);
            return(new Int128(xu));
        }