public static Rational64 operator /(Rational64 first, Rational64 second)
        {
            var d = unchecked ((UInt32)first.bits);
            var c = unchecked ((Int32)(first.bits >> 32));
            var f = unchecked ((UInt32)second.bits);
            var e = unchecked ((Int32)(second.bits >> 32));

            if (0 == f)
            {
                unchecked (f / 0).Ignore();
            }
            if (0 == d)
            {
                return(Zero);
            }
            var a = EuclideanAlgorithm.GreatestCommonDivisor(d, f);

            d /= a;
            f /= a;
            bool s;

            if (s = 0 <= c)
            {
                ++c;
            }
            else
            {
                c = -c;
            }
            if (0 <= e)
            {
                s = !s;
                ++e;
            }
            else
            {
                e = -e;
            }
            var b = EuclideanAlgorithm.GreatestCommonDivisor(c.ToUnsignedUnchecked(), e.ToUnsignedUnchecked());

            c = unchecked (c.ToUnsignedUnchecked() / b).ToSignedUnchecked();
            e = unchecked (e.ToUnsignedUnchecked() / b).ToSignedUnchecked();
            var p = checked (d * e.ToUnsignedUnchecked());
            var q = unchecked ((Int64)c * f);

            if (!s)
            {
                unchecked {
                    --q;
                }
            }
            else
            {
                unchecked {
                    q = -q;
                }
            }
            q = ((Int64) checked ((Int32)q) << 32) | (Int64)p;
            return(new Rational64(unchecked ((UInt64)q)));
        }
 public static Rational64 GetNextRational64 <TRandomNumberGenerator>(ref this TRandomNumberGenerator random) where TRandomNumberGenerator : struct, IRandomNumberGenerator
 {
     for (; ;)
     {
         var a = random.GetNextInt64Bits();
         var b = a >> 32;
         if (0 <= b)
         {
             unchecked {
                 ++b;
             }
         }
         else
         {
             unchecked {
                 b = -b;
             }
         }
         if (1 == EuclideanAlgorithm.GreatestCommonDivisor(unchecked ((UInt32)a), unchecked ((UInt32)b)))
         {
             return(new Rational64(a.ToUnsignedUnchecked()));
         }
     }
 }
Пример #3
0
        public void GreatestCommonDivisor_Long(long x, long y, long gcd)
        {
            long actual = EuclideanAlgorithm.GreatestCommonDivisor(x, y);

            Assert.Equal(gcd, actual);
        }
Пример #4
0
        public void GreatestCommonDivisor_Int(int x, int y, int gcd)
        {
            int actual = EuclideanAlgorithm.GreatestCommonDivisor(x, y);

            Assert.Equal(gcd, actual);
        }