Пример #1
0
        public void TestAcos()
        {
            var a = Fixed.FromFloat(0.54646f);
            var r = Fixed.FromFloat(0.992664887f);

            AssertApproximately(r, FMath.Acos(a));

            a = Fixed.FromFloat(-0.154787f);
            r = Fixed.FromFloat(1.726208178f);
            AssertApproximately(r, FMath.Acos(a));
        }
Пример #2
0
        public void TestAsin()
        {
            var a = Fixed.FromFloat(0.54646f);
            var r = Fixed.FromFloat(0.578131f);

            AssertApproximately(r, FMath.Asin(a));

            a = Fixed.FromFloat(-0.154787f);
            r = Fixed.FromFloat(-0.155411851f);
            AssertApproximately(r, FMath.Asin(a));
        }
Пример #3
0
        public void TestAtan()
        {
            var a = Fixed.FromInt(14);
            var r = Fixed.FromFloat(1.4994888f);

            AssertApproximately(r, FMath.Atan(a));

            a = Fixed.FromFloat(-0.154787f);
            r = Fixed.FromFloat(-0.153568f);
            AssertApproximately(r, FMath.Atan(a));

            a = Fixed.FromFloat(0.7547870f);
            r = Fixed.FromFloat(0.6465577f);
            AssertApproximately(r, FMath.Atan(a));
        }
Пример #4
0
        public void Conversion()
        {
            var random = new Random(666);

            for (var i = 0; i < 100; i++)
            {
                var da = 666 * random.NextDouble() - 333;
                var sa = (float)da;

                var fda = Fixed.FromDouble(da);
                var fsa = Fixed.FromFloat(sa);

                Assert.AreEqual(da, fda.ToDouble(), delta);
                Assert.AreEqual(sa, fsa.ToFloat(), delta);
            }
        }
Пример #5
0
        public void TestTan()
        {
            var deg2Rad = Fixed.FromFloat((float)(Math.PI / 180));
            var a       = Fixed.FromInt(45) * deg2Rad;
            var r       = Fixed.FromInt(1);

            AssertApproximately(r, FMath.Tan(a));

            a = Fixed.FromInt(0) * deg2Rad;
            r = Fixed.FromInt(0);
            AssertApproximately(r, FMath.Tan(a));

            a = Fixed.FromInt(37) * deg2Rad;
            r = Fixed.Parse("0.75355405");
            AssertApproximately(r, FMath.Tan(a));
        }
Пример #6
0
        public void TestCos()
        {
            var deg2Rad = Fixed.FromFloat((float)(Math.PI / 180));
            var a       = Fixed.FromInt(90) * deg2Rad;
            var r       = Fixed.FromInt(0);

            AssertApproximately(r, FMath.Cos(a));

            a = Fixed.FromInt(0) * deg2Rad;
            r = Fixed.FromInt(1);
            AssertApproximately(r, FMath.Cos(a));

            a = Fixed.FromInt(37) * deg2Rad;
            r = Fixed.Parse("0.79863551");
            AssertApproximately(r, FMath.Cos(a));
        }
Пример #7
0
        public void TestSin()
        {
            var deg2Rad = Fixed.FromFloat((float)(Math.PI / 180));
            var a       = Fixed.FromInt(90) * deg2Rad;
            var r       = Fixed.FromInt(1);

            AssertApproximately(r, FMath.Sin(a));

            a = Fixed.FromInt(0) * deg2Rad;
            r = Fixed.FromInt(0);
            AssertApproximately(r, FMath.Sin(a));

            a = Fixed.FromInt(37) * deg2Rad;
            r = Fixed.Parse("0.60181502");
            AssertApproximately(r, FMath.Sin(a));
        }
Пример #8
0
        public void TestFactory()
        {
            var a = Fixed.FromInt(10);

            Assert.AreEqual(Fixed.FromInt(10), a);

            var b = Fixed.FromFloat(3.2f);

            Assert.AreEqual(Fixed.FromFloat(3.2f), b);

            var c = Fixed.Parse("6.3");

            AssertApproximately(Fixed.FromFraction(63, 10), c);

            var d = Fixed.FromFraction(3141592, 1000000);

            AssertApproximately(Fixed.FromFloat(3.141592f), d);
        }
Пример #9
0
        public void TestParseFloat()
        {
            var f = Fixed.Parse("000.0000");

            AssertApproximately(Fixed.Zero, f);

            f = Fixed.Parse("1.4567");
            AssertApproximately(Fixed.FromFloat(1.4567f), f);

            f = Fixed.Parse("-1.4831");
            AssertApproximately(Fixed.FromFloat(-1.4831f), f);

            f = Fixed.Parse("16777216.1345654");
            AssertApproximately(Fixed.FromFloat(16777216), f);

            f = Fixed.Parse("-16777216.1345654");
            AssertApproximately(Fixed.FromFloat(-16777216), f);
        }
Пример #10
0
        private void Run_Add_Int_Float(Random r)
        {
            var a      = r.Next(-10000, 10000);
            var b      = (float)(r.NextDouble() * r.Next(-10000, 10000));
            var result = a + b;

            LoopBase(() => result = a + b);

            var fa      = Fixed.FromInt(a);
            var fb      = Fixed.FromFloat(b);
            var fresult = fa + fb;

            LoopFixed(() => fresult = fa + fb);

            if (!Fixed.Approximately(fresult, Fixed.FromFloat(result)))
            {
                throw new InvalidOperationException();
            }
        }
Пример #11
0
        static void Test(int seed, Func <Fixed, Fixed> sin, int loop = 1000000)
        {
            var r     = new Random(seed);
            var timer = new Stopwatch();
            var error = 0.0;

            for (int i = 0; i < loop; i++)
            {
                var angle   = Fixed.FromFloat((float)(r.NextDouble() * r.Next(0, 1440) - 720.0));
                var answer0 = Sin0(angle);

                timer.Start();
                var answer1 = sin(angle);
                timer.Stop();

                error += Math.Abs(answer0.ToFloat() - answer1.ToFloat());
            }

            Console.WriteLine("Loop: {0}, Time: {1:f6}ms, Error: {2:f6}", loop, timer.ElapsedMilliseconds, error / loop);
        }
Пример #12
0
        public void TestAtan2()
        {
            var y = Fixed.FromInt(0);
            var x = Fixed.FromInt(1);
            var r = Fixed.FromFloat(0);

            AssertApproximately(r, FMath.Atan2(y, x));

            y = Fixed.FromInt(0);
            x = Fixed.FromInt(-1);
            r = Fixed.FromFloat((float)Math.PI);
            AssertApproximately(r, FMath.Atan2(y, x));

            y = Fixed.FromInt(1);
            x = Fixed.FromInt(0);
            r = Fixed.FromFloat((float)Math.PI / 2);
            AssertApproximately(r, FMath.Atan2(y, x));

            y = Fixed.FromFloat(1.54513f);
            x = Fixed.FromFloat(-1.65673f);
            r = Fixed.FromFloat(2.390926f);     // 2.3910351266f is more accurate value
            AssertApproximately(r, FMath.Atan2(y, x));
        }
Пример #13
0
        private void Run_Divide_Int_Int(Random r)
        {
            var a = r.Next(-1000, 1000);
            var b = r.Next(-1000, 1000);

            if (b == 0)
            {
                b = 1;
            }
            var result = (float)a / b;

            LoopBase(() => result = (float)a / b);

            var fa      = Fixed.FromInt(a);
            var fb      = Fixed.FromInt(b);
            var fresult = fa / fb;

            LoopFixed(() => fresult = fa / fb);

            if (!Fixed.Approximately(fresult, Fixed.FromFloat(result)))
            {
                throw new InvalidOperationException();
            }
        }
Пример #14
0
 static Fixed Sin0(Fixed angle)
 {
     return(Fixed.FromFloat((float)Math.Sin(angle.ToFloat())));
 }
Пример #15
0
 static Fixed Atan0(Fixed d)
 {
     return(Fixed.FromFloat((float)Math.Atan(d.ToFloat())));
 }