Ejemplo n.º 1
0
        public void TestRange()
        {
            var reset = new AutoResetEvent(false);
            var step  = 1m;
            var i     = 0m;

            while (true)
            {
                Task.Factory.StartNew(() =>
                {
                    Debug.WriteLine("Sqrt({0})={1}", i, DecimalEx.Sqrt(i));
                    reset.Set();
                });
                Assert.IsTrue(reset.WaitOne(30000));

                step *= 1.01m;
                try { i += step; }
                catch (OverflowException)
                {
                    if (i == Decimal.MaxValue)
                    {
                        break;
                    }
                    i = decimal.MaxValue;
                }
            }
        }
Ejemplo n.º 2
0
        public static decimal Sqrt(this decimal value)
        {
            if (value.IsNaN())
            {
                return(Decimals.NaN);
            }

            return(DecimalEx.Sqrt(value));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// The great circle distance in radians between two spherical coordinates.
        /// This function uses the Haversine formula.
        /// For math details, see:
        ///     https://en.wikipedia.org/wiki/Haversine_formula
        ///     https://www.movable-type.co.uk/scripts/latlong.html
        /// </summary>
        /// <param name="a">the first lat/lng pair (in radians)</param>
        /// <param name="b">the second lat/lng pair (in radians)</param>
        /// <returns>
        /// the great circle distance in radians between a and b
        /// </returns>
        /// <!--
        /// geoCoord.c
        /// double H3_EXPORT(pointDistRads)
        /// -->
        public static decimal DistanceToRadians(this GeoCoord a, GeoCoord b)
        {
            decimal sinLat = DecimalEx.Sin((b.Latitude - a.Latitude) / 2.0m);
            decimal sinLng = DecimalEx.Sin((b.Longitude - a.Longitude) / 2.0m);
            decimal p      = sinLat * sinLat + DecimalEx.Cos(a.Latitude) *
                             DecimalEx.Cos(b.Latitude) * sinLng * sinLng;

            return(2 * DecimalEx.ATan2(DecimalEx.Sqrt(p), DecimalEx.Sqrt(1 - p)));
        }
Ejemplo n.º 4
0
        public void pythaGorTheorem()
        {
            Console.Write("Enter A:\n");
            decimal ASquared = decimal.Parse(Console.ReadLine());

            Console.Write("Enter B:\n");
            decimal BSquared = decimal.Parse(Console.ReadLine());

            Answer = DecimalEx.Sqrt((ASquared * ASquared) + (BSquared * BSquared));
            Console.Write($"C is {Answer}.\n");
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Surface area in radians^2 of spherical triangle on unit sphere.
        ///
        /// For the math, see:
        /// https://en.wikipedia.org/wiki/Spherical_trigonometry#Area_and_spherical_excess
        /// </summary>
        /// <param name="a">length of triangle side A in radians</param>
        /// <param name="b">length of triangle side B in radians</param>
        /// <param name="c">length of triangle side C in radians</param>
        /// <returns>area in radians^2 of triangle on unit sphere</returns>
        /// <!--
        /// geoCoord.c
        /// double triangleEdgeLengthsToArea
        /// -->
        private static decimal TriangleEdgeLengthToArea(decimal a, decimal b, decimal c)
        {
            decimal s = (a + b + c) / 2;

            a  = (s - a) / 2;
            b  = (s - b) / 2;
            c  = (s - c) / 2;
            s /= 2;

            return(4 * DecimalEx.ATan
                       (DecimalEx.Sqrt(DecimalEx.Tan(s) *
                                       DecimalEx.Tan(a) *
                                       DecimalEx.Tan(b) *
                                       DecimalEx.Tan(c))));
        }
Ejemplo n.º 6
0
 public void RejectNegativeValue()
 {
     Assert.Throws <ArgumentException>(() => DecimalEx.Sqrt(-1));
 }
Ejemplo n.º 7
0
 public void Test(decimal s, decimal expected)
 {
     Assert.That(DecimalEx.Sqrt(s), Is.EqualTo(expected).Within(0m));
 }
Ejemplo n.º 8
0
        public void UpdateFilter()
        {
            var     A             = DecimalEx.Pow(10, Gain / 40m);
            var     w             = DecimalEx.TwoPi * Frequency / SampleRate;
            var     cosw          = DecimalEx.Cos(w);
            var     sinw          = DecimalEx.Sin(w);
            var     alpha         = sinw / (2 * Q);
            var     Am1cosw       = (A - 1m) * cosw;
            var     Ap1cosw       = (A + 1m) * cosw;
            var     TwoSqrtAalpha = 2m * DecimalEx.Sqrt(A) * alpha;
            decimal a0            = 1m;

            switch (Type)
            {
            case FilterType.Disabled:
                b0    = 1;
                b1    = b2 = a1 = a2 = 0;
                FP_b0 = 0x00800000;
                FP_b1 = FP_b2 = FP_a1 = FP_a2 = 0;
                return;

            case FilterType.EQ:
                b0 = 1m + alpha * A;
                b1 = a1 = -2m * cosw;
                b2 = 1m - alpha * A;
                a0 = 1m + alpha / A;
                a2 = 1m - alpha / A;
                break;

            case FilterType.Lowpass:
                b1 = 1m - cosw;
                b2 = b0 = b1 / 2m;
                a0 = 1m + alpha;
                a1 = -2m * cosw;
                a2 = 1m - alpha;
                break;

            case FilterType.Highpass:
                b1 = -1m - cosw;
                b0 = b2 = -b1 / 2m;
                a0 = 1m + alpha;
                a1 = -2m * cosw;
                a2 = 1m - alpha;
                break;

            case FilterType.Bandpass:
                b0 = alpha;
                b1 = 0;
                b2 = -alpha;
                a0 = 1m + alpha;
                a1 = -2m * cosw;
                a2 = 1m - alpha;
                break;

            case FilterType.Notch:
                b0 = b2 = 1m;
                a0 = 1m + alpha;
                a1 = b1 = -2m * cosw;
                a2 = 1m - alpha;
                break;

            case FilterType.LowShelf:
                b0 = A * (A + 1m - Am1cosw + TwoSqrtAalpha);
                b1 = 2m * A * (A - 1m - Ap1cosw);
                b2 = A * (A + 1m - Am1cosw - TwoSqrtAalpha);
                a0 = A + 1m + Am1cosw + TwoSqrtAalpha;
                a1 = -2m * (A - 1m + Ap1cosw);
                a2 = A + 1m + Am1cosw - TwoSqrtAalpha;
                break;

            case FilterType.HighShelf:
                b0 = A * (A + 1m + Am1cosw + TwoSqrtAalpha);
                b1 = -2m * A * (A - 1m + Ap1cosw);
                b2 = A * (A + 1m + Am1cosw - TwoSqrtAalpha);
                a0 = A + 1m - Am1cosw + TwoSqrtAalpha;
                a1 = 2m * (A - 1m - Ap1cosw);
                a2 = A + 1m - Am1cosw - TwoSqrtAalpha;
                break;

            case FilterType.Custom:
                break;
            }
            b0 /= a0;
            b1 /= a0;
            b2 /= a0;
            a1 /= a0;
            a2 /= a0;
            if (NegateA && Type != FilterType.Custom)
            {
                a1 *= -1m;
                a2 *= -1m;
            }
            FP_b0 = ToFP(b0);
            FP_b1 = ToFP(b1);
            FP_b2 = ToFP(b2);
            FP_a1 = ToFP(a1);
            FP_a2 = ToFP(a2);
            b0    = FromFP(FP_b0);
            b1    = FromFP(FP_b1);
            b2    = FromFP(FP_b2);
            a1    = FromFP(FP_a1);
            a2    = FromFP(FP_a2);
        }