예제 #1
0
 protected void onTestDrawPosition(BW31.SP2D.FixVector2 position, FixMath.NET.Fix64 distance)
 {
     this.position = new Vector3 {
         x = (float)position.x, z = (float)position.y
     };
     this.distance = Mathf.Max(0.5f, (float)distance);
     //Debug.Log(this.position, distance);
 }
예제 #2
0
        public void Log2()
        {
            double maxDelta = (double)(Fix64.Precision * 4);

            for (int j = 0; j < m_testCases.Length; ++j)
            {
                var b = Fix64.FromRaw(m_testCases[j]);

                if (b <= Fix64.Zero)
                {
                    Assert.Throws <ArgumentOutOfRangeException>(() => Fix64.Log2(b));
                }
                else
                {
                    var expected = Math.Log((double)b) / Math.Log(2);
                    var actual   = (double)Fix64.Log2(b);
                    var delta    = Math.Abs(expected - actual);

                    Assert.True(delta <= maxDelta, string.Format("Ln({0}) = expected {1} but got {2}", b, expected, actual));
                }
            }
        }
예제 #3
0
        public void MultiplicationTestCases()
        {
            var sw       = new Stopwatch();
            int failures = 0;

            for (int i = 0; i < m_testCases.Length; ++i)
            {
                for (int j = 0; j < m_testCases.Length; ++j)
                {
                    var x        = Fix64.FromRaw(m_testCases[i]);
                    var y        = Fix64.FromRaw(m_testCases[j]);
                    var xM       = (decimal)x;
                    var yM       = (decimal)y;
                    var expected = xM * yM;
                    expected =
                        expected > (decimal)Fix64.MaxValue
                            ? (decimal)Fix64.MaxValue
                            : expected < (decimal)Fix64.MinValue
                                  ? (decimal)Fix64.MinValue
                                  : expected;
                    sw.Start();
                    var actual = x * y;
                    sw.Stop();
                    var actualM  = (decimal)actual;
                    var maxDelta = (decimal)Fix64.FromRaw(1);
                    if (Math.Abs(actualM - expected) > maxDelta)
                    {
                        Console.WriteLine("Failed for FromRaw({0}) * FromRaw({1}): expected {2} but got {3}",
                                          m_testCases[i],
                                          m_testCases[j],
                                          (Fix64)expected,
                                          actualM);
                        ++failures;
                    }
                }
            }
            Console.WriteLine("{0} total, {1} per multiplication", sw.ElapsedMilliseconds, (double)sw.Elapsed.Milliseconds / (m_testCases.Length * m_testCases.Length));
            Assert.True(failures < 1);
        }
예제 #4
0
        public void Acos()
        {
            decimal maxDelta = Fix64.Precision * 20;
            var     deltas   = new List <decimal>();

            // Precision
            for (var x = -1.0; x < 1.0; x += 0.001)
            {
                var xf       = (Fix64)x;
                var actual   = (decimal)Fix64.Acos(xf);
                var expected = (decimal)Math.Acos((double)xf);
                var delta    = Math.Abs(actual - expected);
                deltas.Add(delta);
                Assert.True(delta <= maxDelta, string.Format("Precision: Acos({0}): expected {1} but got {2}", xf, expected, actual));
            }

            for (int i = 0; i < m_testCases.Length; ++i)
            {
                var b = Fix64.FromRaw(m_testCases[i]);

                if (b < -Fix64.One)
                {
                    continue;
                }
                if (b > Fix64.One)
                {
                    continue;
                }

                var expected = (decimal)Math.Acos((double)b);
                var actual   = (decimal)Fix64.Acos(b);
                var delta    = Math.Abs(expected - actual);
                deltas.Add(delta);
                Assert.True(delta <= maxDelta, string.Format("Acos({0}) = expected {1} but got {2}", b, expected, actual));
            }
            output.WriteLine("Max error: {0} ({1} times precision)", deltas.Max(), deltas.Max() / Fix64.Precision);
            output.WriteLine("Average precision: {0} ({1} times precision)", deltas.Average(), deltas.Average() / Fix64.Precision);
        }
예제 #5
0
 /// <summary>
 /// Acos turns out to be an extremely complex number.
 /// There is no fixed point equivalent I know of so I fall back to the default
 /// implementation available to the hardware and C#.
 /// </summary>
 public static Fix64 Acos(Fix64 value)
 {
     return((Fix64)Math.Acos((double)value));
 }
예제 #6
0
 /// <summary>
 /// Returns a number indicating the sign of a Fix64 number.
 /// Returns 1 if the value is positive, 0 if is 0, and -1 if it is negative.
 /// </summary>
 public static Fix64 FixSign(Fix64 value)
 {
     return(value <Fix64.Zero? - Fix64.One : value> Fix64.Zero ? Fix64.One : Fix64.Zero);
 }
예제 #7
0
 //[Test]
 public void GenerateLuts()
 {
     Fix64.GenerateSinLut();
     Fix64.GenerateTanLut();
 }