Example #1
0
    public void TestAtan2()
    {
        m_max_precision_times = 0m;
        m_max_delta_percent   = 0m;

        Assert.Equal(FixPoint.Atan2(FixPoint.Zero, FixPoint.One), FixPoint.Zero);
        Assert.Equal(FixPoint.Atan2(FixPoint.One, FixPoint.One), FixPoint.QuarterPi);
        Assert.Equal(FixPoint.Atan2(FixPoint.One, FixPoint.Zero), FixPoint.HalfPi);
        Assert.Equal(FixPoint.Atan2(FixPoint.One, -FixPoint.One), FixPoint.HalfPi + FixPoint.QuarterPi);
        Assert.Equal(FixPoint.Atan2(FixPoint.Zero, -FixPoint.One), FixPoint.Pi);
        Assert.Equal(FixPoint.Atan2(-FixPoint.One, -FixPoint.One), FixPoint.Pi + FixPoint.QuarterPi);
        Assert.Equal(FixPoint.Atan2(-FixPoint.One, FixPoint.Zero), FixPoint.OneAndHalfPi);
        Assert.Equal(FixPoint.Atan2(-FixPoint.One, FixPoint.One), FixPoint.TwoPi - FixPoint.QuarterPi);

        System.Random ran = new System.Random();
        for (int i = 0; i < 10000; ++i)
        {
            double   y         = (float)ran.NextDouble() * int.MaxValue;
            double   x         = (float)ran.NextDouble() * int.MaxValue;
            double   radian    = Math.Atan2(y, x);
            FixPoint fp_y      = (FixPoint)y;
            FixPoint fp_x      = (FixPoint)x;
            FixPoint fp_radian = FixPoint.Atan2(fp_y, fp_x);

            decimal delta           = (decimal)Math.Abs(radian - (double)fp_radian);
            decimal precision_times = delta / FixPoint.Precision;
            decimal delta_percent   = delta * 100.0m / (decimal)Math.PI;
            if (precision_times > m_max_precision_times)
            {
                m_max_precision_times = precision_times;
            }
            if (delta_percent > m_max_delta_percent)
            {
                m_max_delta_percent = delta_percent;
            }
        }
        for (int i = 0; i < 10000; ++i)
        {
            double   y         = (float)ran.NextDouble() * int.MaxValue;
            double   x         = -(float)ran.NextDouble() * int.MaxValue;
            double   radian    = Math.Atan2(y, x);
            FixPoint fp_y      = (FixPoint)y;
            FixPoint fp_x      = (FixPoint)x;
            FixPoint fp_radian = FixPoint.Atan2(fp_y, fp_x);

            decimal delta           = (decimal)Math.Abs(radian - (double)fp_radian);
            decimal precision_times = delta / FixPoint.Precision;
            decimal delta_percent   = delta * 100.0m / (decimal)Math.PI;
            if (precision_times > m_max_precision_times)
            {
                m_max_precision_times = precision_times;
            }
            if (delta_percent > m_max_delta_percent)
            {
                m_max_delta_percent = delta_percent;
            }
        }
        for (int i = 0; i < 10000; ++i)
        {
            double   y         = -(float)ran.NextDouble() * int.MaxValue;
            double   x         = -(float)ran.NextDouble() * int.MaxValue;
            double   radian    = Math.Atan2(y, x) + Math.PI * 2;
            FixPoint fp_y      = (FixPoint)y;
            FixPoint fp_x      = (FixPoint)x;
            FixPoint fp_radian = FixPoint.Atan2(fp_y, fp_x);

            decimal delta           = (decimal)Math.Abs(radian - (double)fp_radian);
            decimal precision_times = delta / FixPoint.Precision;
            decimal delta_percent   = delta * 100.0m / (decimal)Math.PI;
            if (precision_times > m_max_precision_times)
            {
                m_max_precision_times = precision_times;
            }
            if (delta_percent > m_max_delta_percent)
            {
                m_max_delta_percent = delta_percent;
            }
        }
        for (int i = 0; i < 10000; ++i)
        {
            double   y         = -(float)ran.NextDouble() * int.MaxValue;
            double   x         = (float)ran.NextDouble() * int.MaxValue;
            double   radian    = Math.Atan2(y, x) + Math.PI * 2;
            FixPoint fp_y      = (FixPoint)y;
            FixPoint fp_x      = (FixPoint)x;
            FixPoint fp_radian = FixPoint.Atan2(fp_y, fp_x);

            decimal delta           = (decimal)Math.Abs(radian - (double)fp_radian);
            decimal precision_times = delta / FixPoint.Precision;
            decimal delta_percent   = delta * 100.0m / (decimal)Math.PI;
            if (precision_times > m_max_precision_times)
            {
                m_max_precision_times = precision_times;
            }
            if (delta_percent > m_max_delta_percent)
            {
                m_max_delta_percent = delta_percent;
            }
        }

        Console.WriteLine("TestAtan2() m_max_precision_times = " + m_max_precision_times + ", m_max_delta_percent = " + m_max_delta_percent);
    }