示例#1
0
        //[Fact]
        public void Atan2Benchmark()
        {
            var deltas = new List <decimal>();

            var swf = new Stopwatch();
            var swd = new Stopwatch();

            foreach (var y in m_testCases)
            {
                foreach (var x in m_testCases)
                {
                    for (int k = 0; k < 1000; ++k)
                    {
                        var yf = (Fix64)y;
                        var xf = (Fix64)x;
                        swf.Start();
                        var actualF = MathFix.Atan2(yf, xf);
                        swf.Stop();
                        swd.Start();
                        var expected = Math.Atan2((double)yf, (double)xf);
                        swd.Stop();
                        deltas.Add(Math.Abs((decimal)actualF - (decimal)expected));
                    }
                }
            }
            Console.WriteLine("Max error: {0} ({1} times precision)", deltas.Max(), deltas.Max() / Fix64.Precision);
            Console.WriteLine("Average precision: {0} ({1} times precision)", deltas.Average(), deltas.Average() / Fix64.Precision);
            Console.WriteLine("Fix64.Atan2 time = {0}ms, Math.Atan2 time = {1}ms", swf.ElapsedMilliseconds, swd.ElapsedMilliseconds);
        }
示例#2
0
        public void Atan2()
        {
            var deltas = new List <decimal>();

            // Identities
            Assert.Equal(MathFix.Atan2(Fix64.Zero, -Fix64.One), MathFix.PI);
            Assert.Equal(MathFix.Atan2(Fix64.Zero, Fix64.Zero), Fix64.Zero);
            Assert.Equal(MathFix.Atan2(Fix64.Zero, Fix64.One), Fix64.Zero);
            Assert.Equal(MathFix.Atan2(Fix64.One, Fix64.Zero), MathFix.PIOver2);
            Assert.Equal(MathFix.Atan2(-Fix64.One, Fix64.Zero), -MathFix.PIOver2);

            // Precision
            for (var y = -1.0; y < 1.0; y += 0.01)
            {
                for (var x = -1.0; x < 1.0; x += 0.01)
                {
                    var yf       = (Fix64)y;
                    var xf       = (Fix64)x;
                    var actual   = MathFix.Atan2(yf, xf);
                    var expected = (decimal)Math.Atan2((double)yf, (double)xf);
                    var delta    = Math.Abs((decimal)actual - expected);
                    deltas.Add(delta);
                    Assert.True(delta <= 0.005M, string.Format("Precision: Atan2({0}, {1}): expected {2} but got {3}", yf, xf, expected, actual));
                }
            }

            // Scalability and edge cases
            foreach (var y in m_testCases)
            {
                foreach (var x in m_testCases)
                {
                    var yf       = (Fix64)y;
                    var xf       = (Fix64)x;
                    var actual   = (decimal)MathFix.Atan2(yf, xf);
                    var expected = (decimal)Math.Atan2((double)yf, (double)xf);
                    var delta    = Math.Abs(actual - expected);
                    deltas.Add(delta);
                    Assert.True(delta <= 0.005M, string.Format("Scalability: Atan2({0}, {1}): expected {2} but got {3}", yf, xf, expected, actual));
                }
            }
            Console.WriteLine("Max error: {0} ({1} times precision)", deltas.Max(), deltas.Max() / Fix64.Precision);
            Console.WriteLine("Average precision: {0} ({1} times precision)", deltas.Average(), deltas.Average() / Fix64.Precision);
        }