Beispiel #1
0
        private static DD SlowPow(DD x, int exp)
        {
            if (exp == 0)
            {
                return(DD.ValueOf(1.0));
            }

            int n = Math.Abs(exp);
            // MD - could use binary exponentiation for better precision & speed
            var pow = new DD(x);

            for (int i = 1; i < n; i++)
            {
                pow *= x;
            }
            if (exp < 0)
            {
                return(pow.Reciprocal());
            }
            return(pow);
        }
        /// <summary>
        /// This routine simply tests for robustness of the toString function.
        /// </summary>
        static void WriteRepeatedSqr(DD xdd)
        {
            if (xdd.GreaterOrEqualThan(DD.ValueOf(1)))
            {
                throw new ArgumentException("Argument must be < 1");
            }

            int count = 0;

            while (xdd.ToDoubleValue() > 1e-300)
            {
                count++;
                double x    = xdd.ToDoubleValue();
                var    xSqr = xdd.Sqr();
                string s    = xSqr.ToString();
                // System.Console.WriteLine(count + ": " + s);

                var xSqr2 = DD.Parse(s);

                xdd = xSqr;
            }
        }
        public void TestParse()
        {
            CheckParse("1.05e10", 1.05E10, 1e-32);
            CheckParse("-1.05e10", -1.05E10, 1e-32);
            CheckParse("1.05e-10", DD.ValueOf(105d).Divide(
                           DD.ValueOf(100d)).Divide(DD.ValueOf(1.0E10)), 1e-32);
            CheckParse("-1.05e-10", DD.ValueOf(105d).Divide(
                           DD.ValueOf(100d)).Divide(DD.ValueOf(1.0E10))
                       .Negate(), 1e-32);

            /**
             * The Java double-precision constant 1.4 gives rise to a value which
             * differs from the exact binary representation down around the 17th decimal
             * place. Thus it will not compare exactly to the DoubleDouble
             * representation of the same number. To avoid this, compute the expected
             * value using full DD precision.
             */
            CheckParse("1.4",
                       DD.ValueOf(14).Divide(DD.ValueOf(10)), 1e-30);

            // 39.5D can be converted to an exact FP representation
            CheckParse("39.5", 39.5, 1e-30);
            CheckParse("-39.5", -39.5, 1e-30);
        }
        public void TestWriteStandardNotation()
        {
            // standard cases
            CheckStandardNotation(1.0, "1.0");
            CheckStandardNotation(0.0, "0.0");

            // cases where hi is a power of 10 and lo is negative
            CheckStandardNotation(DD.ValueOf(1e12) - DD.ValueOf(1), "999999999999.0");
            CheckStandardNotation(DD.ValueOf(1e14) - DD.ValueOf(1), "99999999999999.0");
            CheckStandardNotation(DD.ValueOf(1e16) - DD.ValueOf(1), "9999999999999999.0");

            var num8Dec = DD.ValueOf(-379363639) / DD.ValueOf(100000000);

            CheckStandardNotation(num8Dec, "-3.79363639");

            CheckStandardNotation(new DD(-3.79363639, 8.039137357367426E-17),
                                  "-3.7936363900000000000000000");

            CheckStandardNotation(DD.ValueOf(34) / DD.ValueOf(1000), "0.034");
            CheckStandardNotation(1.05e3, "1050.0");
            CheckStandardNotation(0.34, "0.34000000000000002442490654175344");
            CheckStandardNotation(DD.ValueOf(34) / DD.ValueOf(100), "0.34");
            CheckStandardNotation(14, "14.0");
        }
Beispiel #5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <param name="c"></param>
        /// <param name="p"></param>
        /// <returns></returns>
        public static bool IsInCircleDDNormalized(
            Coordinate a, Coordinate b, Coordinate c,
            Coordinate p)
        {
            var adx = DD.ValueOf(a.X) - p.X;
            var ady = DD.ValueOf(a.Y) - p.Y;
            var bdx = DD.ValueOf(b.X) - p.X;
            var bdy = DD.ValueOf(b.Y) - p.Y;
            var cdx = DD.ValueOf(c.X) - p.X;
            var cdy = DD.ValueOf(c.Y) - p.Y;

            var abdet = adx * bdy - bdx * ady;
            var bcdet = bdx * cdy - cdx * bdy;
            var cadet = cdx * ady - adx * cdy;
            var alift = adx * adx + ady * ady;
            var blift = bdx * bdx + bdy * bdy;
            var clift = cdx * cdx + cdy * cdy;

            var sum = alift * bcdet + blift * cadet + clift * abdet;

            bool isInCircle = sum.ToDoubleValue() > 0;

            return(isInCircle);
        }
        /// <summary>
        /// Tests if a point is inside the circle defined by the points a, b, c.
        /// The computation uses <see cref="DD"/> arithmetic for robustness.
        /// </summary>
        /// <param name="a">A vertex of the triangle</param>
        /// <param name="b">A vertex of the triangle</param>
        /// <param name="c">A vertex of the triangle</param>
        /// <param name="p">The point to test</param>
        /// <returns><c>true</c> if this point is inside the circle defined by the points a, b, c</returns>
        public static bool IsInCircleDD3(
            Coordinate a, Coordinate b, Coordinate c,
            Coordinate p)
        {
            var adx = DD.ValueOf(a.X) - (p.X);
            var ady = DD.ValueOf(a.Y) - (p.Y);
            var bdx = DD.ValueOf(b.X) - (p.X);
            var bdy = DD.ValueOf(b.Y) - (p.Y);
            var cdx = DD.ValueOf(c.X) - (p.X);
            var cdy = DD.ValueOf(c.Y) - (p.Y);

            var abdet = adx * bdy - (bdx * ady);
            var bcdet = bdx * cdy - (cdx * bdy);
            var cadet = cdx * ady - (adx * cdy);
            var alift = adx * adx - (ady * ady);
            var blift = bdx * bdx - (bdy * bdy);
            var clift = cdx * cdx - (cdy * cdy);

            var sum = alift * bcdet + blift * cadet + clift * abdet;

            bool isInCircle = sum.ToDoubleValue() > 0;

            return(isInCircle);
        }
 private static void CheckSciNotation(double x, string expectedStr)
 {
     CheckSciNotation(DD.ValueOf(x), expectedStr);
 }
 public void testRepeatedSqr()
 {
     WriteRepeatedSqr(DD.ValueOf(.9));
     WriteRepeatedSqr(DD.PI.Divide(DD.ValueOf(10)));
 }
 public void TestRepeatedSqrt()
 {
     WriteRepeatedSqrt(DD.ValueOf(1.0));
     WriteRepeatedSqrt(DD.ValueOf(.999999999999));
     WriteRepeatedSqrt(DD.PI.Divide(DD.ValueOf(10)));
 }
 public void TestNaN()
 {
     Assert.IsTrue(DD.IsNaN(DD.ValueOf(1).Divide(DD.ValueOf(0))));
     Assert.IsTrue(DD.IsNaN(DD.ValueOf(1).Multiply(DD.NaN)));
 }
 public void TestNaN()
 {
     Assert.IsTrue(DD.IsNaN(DD.ValueOf(1) / DD.ValueOf(0)));
     Assert.IsTrue(DD.IsNaN(DD.ValueOf(1) * DD.NaN));
 }
        private void CheckDeterminant(double x1, double y1, double x2, double y2, double expected, double errBound)
        {
            var det = DD.Determinant(x1, y1, x2, y2);

            CheckErrorBound("Determinant", det, DD.ValueOf(expected), errBound);
        }
 private static void CheckStandardNotation(double x, String expectedStr)
 {
     CheckStandardNotation(DD.ValueOf(x), expectedStr);
 }
 public void TestWriteRepeatedSqr()
 {
     WriteRepeatedSqr(DD.ValueOf(0.9));
     WriteRepeatedSqr(DD.PI / DD.ValueOf(10));
 }
 private static DD ToDDAlt(double x)
 {
     // convert more accurately to DD from decimal representation
     // very slow though - should be a better way
     return(DD.ValueOf(x + ""));
 }