예제 #1
0
        public void UnequalsF()
        {
            float f1 = 1.0999f;
            float f2 = 1.1f;

            int r = Numeric.Compare(f1, f2);

            Assert.AreEqual(-1, r);

            r = Numeric.Compare(f2, f1);
            Assert.AreEqual(1, r);

            r = Numeric.Compare(f1, f2, 0.00001f);
            Assert.AreEqual(-1, r);

            r = Numeric.Compare(f2, f1, 0.00001f);
            Assert.AreEqual(1, r);

            bool b = Numeric.AreEqual(f1, f2);

            Assert.IsFalse(b);

            b = Numeric.AreEqual(f1, f2, 0.00001f);
            Assert.IsFalse(b);
        }
예제 #2
0
        public void ComparisionsWithZeroF()
        {
            // Test case for bug fix
            Assert.IsTrue(Numeric.AreEqual(0.0f, 0.0f));
            Assert.AreEqual(0, Numeric.Compare(0.0f, 0.0f));

            // Values near zero are treated as zero
            Assert.IsTrue(Numeric.AreEqual(0.0f, 1e-10f));
            Assert.IsTrue(Numeric.AreEqual(0.0f, -1e-10f));
            Assert.IsTrue(Numeric.AreEqual(-1e-10f, 0.0f));
            Assert.IsTrue(Numeric.AreEqual(1e-10f, 0.0f));

            Assert.IsFalse(Numeric.AreEqual(0.0f, 1e-4f));
            Assert.IsFalse(Numeric.AreEqual(0.0f, -1e-4f));
            Assert.IsFalse(Numeric.AreEqual(-1e-4f, 0.0f));
            Assert.IsFalse(Numeric.AreEqual(1e-4f, 0.0f));

            // Values near zero are treated as zero
            Assert.AreEqual(0, Numeric.Compare(0.0f, 1e-10f));
            Assert.AreEqual(0, Numeric.Compare(0.0f, -1e-10f));
            Assert.AreEqual(0, Numeric.Compare(-1e-10f, 0.0f));
            Assert.AreEqual(0, Numeric.Compare(1e-10f, 0.0f));

            Assert.AreEqual(-1, Numeric.Compare(0.0f, 1e-4f));
            Assert.AreEqual(+1, Numeric.Compare(0.0f, -1e-4f));
            Assert.AreEqual(-1, Numeric.Compare(-1e-4f, 0.0f));
            Assert.AreEqual(+1, Numeric.Compare(1e-4f, 0.0f));
        }
예제 #3
0
        public void UnequalsD()
        {
            double d1 = 1.0999;
            double d2 = 1.1;

            int r = Numeric.Compare(d1, d2);

            Assert.AreEqual(-1, r);

            r = Numeric.Compare(d2, d1);
            Assert.AreEqual(1, r);

            r = Numeric.Compare(d1, d2, 0.00001);
            Assert.AreEqual(-1, r);

            r = Numeric.Compare(d2, d1, 0.00001);
            Assert.AreEqual(1, r);

            bool b = Numeric.AreEqual(d1, d2);

            Assert.IsFalse(b);

            b = Numeric.AreEqual(d1, d2, 0.00001);
            Assert.IsFalse(b);
        }
예제 #4
0
        public void NegativeValuesF()
        {
            // Test case for bug fix
            Assert.IsTrue(Numeric.AreEqual(-1000.0f, -1000.0f));
            Assert.IsFalse(Numeric.AreEqual(-1000.0f, 1000.0f));
            Assert.IsFalse(Numeric.AreEqual(1000.0f, -1000.0f));

            Assert.AreEqual(0, Numeric.Compare(-1000.0f, -1000.0f));
            Assert.AreEqual(-1, Numeric.Compare(-1000.0f, 1000.0f));
            Assert.AreEqual(1, Numeric.Compare(1000.0f, -1000.0f));
        }
예제 #5
0
        public void SmallNumbersF()
        {
            Assert.AreEqual(-1, Numeric.Compare(1e-10f, 1.002e-10f, 0.001e-10f));
            Assert.AreEqual(0, Numeric.Compare(1e-10f, 1.0001e-10f, 0.001e-10f));
            Assert.AreEqual(1, Numeric.Compare(1.002e-10f, 1e-10f, 0.001e-10f));

            // Values near zero are treated as zero
            Assert.IsTrue(Numeric.AreEqual(1e-10f, 1.0000001e-10f));
            Assert.IsTrue(Numeric.AreEqual(1e-10f, 1.000002e-10f));

            // Values near zero can only be compared by specifying an epsilon value.
            Assert.IsTrue(Numeric.AreEqual(1e-10f, 1.0001e-10f, 0.001e-10f));
            Assert.IsFalse(Numeric.AreEqual(1e-10f, 1.0002e-10f, 0.0001e-10f));
        }
예제 #6
0
        public void EqualsF()
        {
            float f1 = 1.1f;
            float f2 = 1.0999999f;

            int r = Numeric.Compare(f1, f2);

            Assert.AreEqual(0, r);

            r = Numeric.Compare(f1, f2, 0.000001f);
            Assert.AreEqual(0, r);

            bool b = Numeric.AreEqual(f1, f2);

            Assert.IsTrue(b);

            b = Numeric.AreEqual(f1, f2, 0.0001f);
            Assert.IsTrue(b);
        }
예제 #7
0
        public void EqualsD()
        {
            double d1 = 1.1;
            double d2 = 1.099999999999999999;

            int r = Numeric.Compare(d1, d2);

            Assert.AreEqual(0, r);

            r = Numeric.Compare(d1, d2, 0.00000001);
            Assert.AreEqual(0, r);

            bool b = Numeric.AreEqual(d1, d2);

            Assert.IsTrue(b);

            b = Numeric.AreEqual(d1, d2, 0.0001);
            Assert.IsTrue(b);
        }
예제 #8
0
        public void BigNumbersF()
        {
            float originalEpsilon = Numeric.EpsilonF;

            Numeric.EpsilonF = 1e-8f;

            Assert.AreEqual(-1, Numeric.Compare(1e10f, 1.0000002e10f));
            Assert.AreEqual(0, Numeric.Compare(1e10f, 1.00000001e10f));
            Assert.AreEqual(1, Numeric.Compare(1.0000002e10f, 1e10f));

            Assert.AreEqual(-1, Numeric.Compare(1e10f, 1.002e10f, 0.001e10f));
            Assert.AreEqual(0, Numeric.Compare(1e10f, 1.0001e10f, 0.001e10f));
            Assert.AreEqual(1, Numeric.Compare(1.002e10f, 1e10f, 0.001e10f));

            Assert.IsTrue(Numeric.AreEqual(1e10f, 1.00000001e10f));
            Assert.IsFalse(Numeric.AreEqual(1e10f, 1.0000002e10f));
            Assert.IsTrue(Numeric.AreEqual(1e10f, 1.0001e10f, 0.001e10f));

            Numeric.EpsilonF = originalEpsilon;
        }
예제 #9
0
        public void BigNumbersD()
        {
            double originalEpsilon = Numeric.EpsilonD;

            Numeric.EpsilonD = 1e-8;

            Assert.AreEqual(-1, Numeric.Compare(1e20, 1.0000002e20));
            Assert.AreEqual(0, Numeric.Compare(1e20, 1.00000001e20));
            Assert.AreEqual(1, Numeric.Compare(1.0000002e20, 1e20));

            Assert.AreEqual(-1, Numeric.Compare(1e20, 1.002e20, 0.001e20));
            Assert.AreEqual(0, Numeric.Compare(1e20, 1.0001e20, 0.001e20));
            Assert.AreEqual(1, Numeric.Compare(1.002e20, 1e20, 0.001e20));

            Assert.IsTrue(Numeric.AreEqual(1e20, 1.00000001e20));
            Assert.IsFalse(Numeric.AreEqual(1e20, 1.0000002e20));
            Assert.IsTrue(Numeric.AreEqual(1e20, 1.0001e20, 0.001e20));

            Numeric.EpsilonD = originalEpsilon;
        }
예제 #10
0
        public void InfinityEqualityD()
        {
            Assert.IsTrue(Numeric.AreEqual(double.PositiveInfinity, double.PositiveInfinity));
            Assert.IsTrue(Numeric.AreEqual(double.PositiveInfinity, double.PositiveInfinity, 0.1d));
            Assert.IsTrue(Numeric.AreEqual(double.NegativeInfinity, double.NegativeInfinity));
            Assert.IsTrue(Numeric.AreEqual(double.NegativeInfinity, double.NegativeInfinity, 0.1d));

            Assert.IsFalse(Numeric.AreEqual(double.PositiveInfinity, double.NegativeInfinity));
            Assert.IsFalse(Numeric.AreEqual(double.NegativeInfinity, double.PositiveInfinity, 0.1d));

            Assert.IsFalse(Numeric.AreEqual(0d, double.PositiveInfinity));
            Assert.IsFalse(Numeric.AreEqual(double.NegativeInfinity, 0d));

            Assert.IsTrue(Numeric.Compare(double.PositiveInfinity, double.PositiveInfinity) == 0);
            Assert.IsTrue(Numeric.Compare(double.NegativeInfinity, double.NegativeInfinity) == 0);
            Assert.IsTrue(Numeric.Compare(double.NegativeInfinity, double.PositiveInfinity) < 0);
            Assert.IsTrue(Numeric.Compare(double.PositiveInfinity, double.NegativeInfinity) > 0);
            Assert.IsTrue(Numeric.Compare(0d, double.NegativeInfinity) > 0);
            Assert.IsTrue(Numeric.Compare(0d, double.PositiveInfinity) < 0);
            Assert.IsTrue(Numeric.Compare(0d, double.NegativeInfinity) > 0);
        }
예제 #11
0
        public void InfinityEqualityF()
        {
            Assert.IsTrue(Numeric.AreEqual(float.PositiveInfinity, float.PositiveInfinity));
            Assert.IsTrue(Numeric.AreEqual(float.PositiveInfinity, float.PositiveInfinity, 0.1f));
            Assert.IsTrue(Numeric.AreEqual(float.NegativeInfinity, float.NegativeInfinity));
            Assert.IsTrue(Numeric.AreEqual(float.NegativeInfinity, float.NegativeInfinity, 0.1f));

            Assert.IsFalse(Numeric.AreEqual(float.PositiveInfinity, float.NegativeInfinity));
            Assert.IsFalse(Numeric.AreEqual(float.NegativeInfinity, float.PositiveInfinity, 0.1f));

            Assert.IsFalse(Numeric.AreEqual(0f, float.PositiveInfinity));
            Assert.IsFalse(Numeric.AreEqual(float.NegativeInfinity, 0f));

            Assert.IsTrue(Numeric.Compare(float.PositiveInfinity, float.PositiveInfinity) == 0);
            Assert.IsTrue(Numeric.Compare(float.NegativeInfinity, float.NegativeInfinity) == 0);
            Assert.IsTrue(Numeric.Compare(float.NegativeInfinity, float.PositiveInfinity) < 0);
            Assert.IsTrue(Numeric.Compare(float.PositiveInfinity, float.NegativeInfinity) > 0);
            Assert.IsTrue(Numeric.Compare(0f, float.NegativeInfinity) > 0);
            Assert.IsTrue(Numeric.Compare(0f, float.PositiveInfinity) < 0);
            Assert.IsTrue(Numeric.Compare(0f, float.NegativeInfinity) > 0);
        }
예제 #12
0
 public void CompareFException()
 {
     Numeric.Compare(0.0f, 0.0f, -0.1f);
 }
예제 #13
0
 public void CompareDException()
 {
     Numeric.Compare(0.0, 0.0, -0.1);
 }
예제 #14
0
        private PaletteEntry GetPaletteEntry(double value)
        {
            if (Count == 0)
            {
                throw new PaletteException("The palette is empty.");
            }

            // Try direct index access. This is faster for palettes where the index of a
            // PaletteEntry is equal to the value of the entry.
            if (value >= 0 && (int)value < Count)
            {
                PaletteEntry entry = this[(int)value];
                if (Numeric.AreEqual(entry.Value, value))
                {
                    return(entry);
                }
            }

            BuildInternalSortedList();

            PaletteEntry?lessEntry    = null;    // Entry whose data value is less than value.
            PaletteEntry?greaterEntry = null;    // Entry whose data value is greater than value.
            PaletteEntry?equalEntry   = null;    // Entry whose data value exactly matches value.

            for (int i = 0; i < _sortedPaletteEntries.Count; i++)
            {
#if SILVERLIGHT || WINDOWS_PHONE
                PaletteEntry entry = _sortedPaletteEntries[i];
#else
                PaletteEntry entry = _sortedPaletteEntries.Values[i];
#endif
                int comparisonResult = Numeric.Compare(entry.Value, value);
                if (comparisonResult < 0)
                {
                    lessEntry = entry;
                }
                else if (comparisonResult == 0)
                {
                    equalEntry = entry;
                }
                else
                {
                    Debug.Assert(comparisonResult > 0, "Sanity check.");
                    greaterEntry = entry;
                    break;
                }
            }

            if (equalEntry != null)
            {
                // Exact match!
                return(equalEntry.Value);
            }

            if (lessEntry != null && greaterEntry != null)
            {
                // value lies between two colors.
                switch (Mode)
                {
                case PaletteMode.LessOrEqual:
                    return(lessEntry.Value);

                case PaletteMode.GreaterOrEqual:
                    return(greaterEntry.Value);

                case PaletteMode.Closest:
                    return((value - lessEntry.Value.Value < greaterEntry.Value.Value - value) ? lessEntry.Value : greaterEntry.Value);

                case PaletteMode.Interpolate:
                    Color        interpolatedColor = GetInterpolatedColor(value, lessEntry.Value, greaterEntry.Value);
                    PaletteEntry interpolatedEntry = new PaletteEntry(value, interpolatedColor);
                    return(interpolatedEntry);
                }
            }

            if (lessEntry != null && greaterEntry == null)
            {
                // value is greater than the colors in the palette.
                switch (Mode)
                {
                case PaletteMode.LessOrEqual:
                case PaletteMode.Closest:
                case PaletteMode.Interpolate:
                    return(lessEntry.Value);

                case PaletteMode.GreaterOrEqual:
                    throw new PaletteException("No suitable color found. Specified value is greater than all color entries in the palette.");
                }
            }

            if (lessEntry == null && greaterEntry != null)
            {
                // value is less than the colors in the palette.
                switch (Mode)
                {
                case PaletteMode.GreaterOrEqual:
                case PaletteMode.Closest:
                case PaletteMode.Interpolate:
                    return(greaterEntry.Value);

                case PaletteMode.LessOrEqual:
                    throw new PaletteException("No suitable color found. Specified value is less than all color entries in the palette.");
                }
            }

            throw new PaletteException("No suitable color found.");
        }
예제 #15
0
        protected override Size ArrangeOverride(Size finalSize)
        {
            // Ensure that all axes are up to date.
            foreach (Axis axis in _monitoredAxes)
            {
                axis.Update();
            }

            UIElementCollection children = Children;

            for (int i = 0; i < children.Count; ++i)
            {
                UIElement child = children[i];
                if (child == null)
                {
                    continue;
                }

                // Get the child or one of its descendants that needs to be positioned.
                DependencyObject elementThatRequiresArrange = GetElementThatRequiresArrange(child);
                if (elementThatRequiresArrange == null)
                {
                    // This child ignores the attached properties (X1, Y1, X2, Y2).
                    Rect childBounds = new Rect(new Point(), finalSize);

                    // Arrange element,
                    child.Arrange(childBounds);

                    // Clip to chart area.
                    if (GetClipToChartArea(child))
                    {
                        ClipElementToChartArea(child, childBounds);
                    }
                }
                else
                {
                    // Determine alignment.
                    HorizontalAlignment horizontalAlignment = HorizontalAlignment.Left;
                    VerticalAlignment   verticalAlignment   = VerticalAlignment.Top;
                    if (elementThatRequiresArrange is FrameworkElement)
                    {
                        FrameworkElement frameworkElement = (FrameworkElement)elementThatRequiresArrange;
                        horizontalAlignment = frameworkElement.HorizontalAlignment;
                        verticalAlignment   = frameworkElement.VerticalAlignment;
                    }

                    // Get associated axes.
                    Axis xAxis = GetXAxis(elementThatRequiresArrange);
                    Axis yAxis = GetYAxis(elementThatRequiresArrange);

                    // Check whether we have to position the element relative to a point (X1, Y1)
                    // or inside a region (X1, Y1) - (X2, Y2).
                    // Convert positions given in data values to pixel positions.
                    double x1 = xAxis.GetPosition(GetX1(elementThatRequiresArrange));
                    double y1 = yAxis.GetPosition(GetY1(elementThatRequiresArrange));
                    double x2 = xAxis.GetPosition(GetX2(elementThatRequiresArrange));
                    double y2 = yAxis.GetPosition(GetY2(elementThatRequiresArrange));

                    bool isX1Valid = Numeric.IsFinite(x1);
                    bool isY1Valid = Numeric.IsFinite(y1);
                    bool isX2Valid = Numeric.IsFinite(x2);
                    bool isY2Valid = Numeric.IsFinite(y2);

                    double availableWidth  = Double.PositiveInfinity;
                    double availableHeight = Double.PositiveInfinity;

                    if (!isX1Valid && !isX2Valid)
                    {
                        // No coordinates set. Use 0 and 'left' alignment.
                        x1 = 0;
                        x2 = Double.NaN;
                        horizontalAlignment = HorizontalAlignment.Left;
                    }
                    else if (!isX1Valid)
                    {
                        // Only X2 set. Use X2 as position.
                        x1 = x2;
                        x2 = Double.NaN;
                    }
                    else if (isX2Valid)
                    {
                        // X1 and X2 are set.
                        int result = Numeric.Compare(x1, x2);
                        if (result < 0)
                        {
                            // X1 < X2: Horizontal region.
                            availableWidth = x2 - x1;
                        }
                        else if (result == 0)
                        {
                            // X1 == X2: Use only X1.
                            x2 = Double.NaN;
                        }
                        else
                        {
                            // X2 > X1: Horizontal region, but swapped.
                            ChartHelper.Swap(ref x1, ref x2);
                            availableWidth = x2 - x1;
                        }
                    }

                    if (!isY1Valid && !isY2Valid)
                    {
                        // No coordinates set. Use 0 and 'top' alignment.
                        y1 = 0;
                        y2 = Double.NaN;
                        verticalAlignment = VerticalAlignment.Top;
                    }
                    else if (!isY1Valid)
                    {
                        // Only Y2 set. Use Y2 as position.
                        y1 = y2;
                        y2 = Double.NaN;
                    }
                    else if (isY2Valid)
                    {
                        // Y1 and Y2 are set.
                        int result = Numeric.Compare(y1, y2);
                        if (result < 0)
                        {
                            // Y1 < Y2: Horizontal region.
                            availableHeight = y2 - y1;
                        }
                        else if (result == 0)
                        {
                            // Y1 == Y2: Use only Y1.
                            y2 = Double.NaN;
                        }
                        else
                        {
                            // Y2 > Y1: Horizontal region, but swapped.
                            ChartHelper.Swap(ref y1, ref y2);
                            availableHeight = y2 - y1;
                        }
                    }

                    // Get size of child.
                    double elementWidth  = child.DesiredSize.Width;
                    double elementHeight = child.DesiredSize.Height;

                    if (elementWidth == 0.0 && elementHeight == 0.0 && child is FrameworkElement)
                    {
                        // Fix for Silverlight.
                        FrameworkElement frameworkElement = (FrameworkElement)child;
                        elementWidth  = frameworkElement.ActualWidth;
                        elementHeight = frameworkElement.ActualHeight;
                    }

                    // Compute bounds of the child element.
                    Rect childBounds = new Rect();

                    // Position child horizontally.
                    if (Numeric.IsNaN(x2))
                    {
                        // Position child relative to point.
                        switch (horizontalAlignment)
                        {
                        case HorizontalAlignment.Left:
                        case HorizontalAlignment.Stretch:
                            childBounds.X     = x1;
                            childBounds.Width = elementWidth;
                            break;

                        case HorizontalAlignment.Center:
                            childBounds.X     = x1 - elementWidth / 2.0;
                            childBounds.Width = elementWidth;
                            break;

                        case HorizontalAlignment.Right:
                            childBounds.X     = x1 - elementWidth;
                            childBounds.Width = elementWidth;
                            break;
                        }
                    }
                    else
                    {
                        // Position child inside horizontal region.
                        switch (horizontalAlignment)
                        {
                        case HorizontalAlignment.Left:
                            childBounds.X     = x1;
                            childBounds.Width = elementWidth;
                            break;

                        case HorizontalAlignment.Center:
                            childBounds.X     = x1 + availableWidth / 2.0 - elementWidth / 2.0;
                            childBounds.Width = elementWidth;
                            break;

                        case HorizontalAlignment.Right:
                            childBounds.X     = x2 - elementWidth;
                            childBounds.Width = elementWidth;
                            break;

                        case HorizontalAlignment.Stretch:
                            childBounds.X     = x1;
                            childBounds.Width = availableWidth;
                            break;
                        }
                    }

                    // Position child vertically.
                    if (Numeric.IsNaN(y2))
                    {
                        // Position child relative to point.
                        switch (verticalAlignment)
                        {
                        case VerticalAlignment.Top:
                        case VerticalAlignment.Stretch:
                            childBounds.Y      = y1;
                            childBounds.Height = elementHeight;
                            break;

                        case VerticalAlignment.Center:
                            childBounds.Y      = y1 - elementHeight / 2.0;
                            childBounds.Height = elementHeight;
                            break;

                        case VerticalAlignment.Bottom:
                            childBounds.Y      = y1 - elementHeight;
                            childBounds.Height = elementHeight;
                            break;
                        }
                    }
                    else
                    {
                        // Position child inside vertical region.
                        switch (verticalAlignment)
                        {
                        case VerticalAlignment.Top:
                            childBounds.Y      = y1;
                            childBounds.Height = elementHeight;
                            break;

                        case VerticalAlignment.Center:
                            childBounds.Y      = y1 + availableHeight / 2.0 - elementHeight / 2.0;
                            childBounds.Height = elementHeight;
                            break;

                        case VerticalAlignment.Bottom:
                            childBounds.Y      = y2 - elementHeight;
                            childBounds.Height = elementHeight;
                            break;

                        case VerticalAlignment.Stretch:
                            childBounds.Y      = y1;
                            childBounds.Height = availableHeight;
                            break;
                        }
                    }

                    // Arrange element.
                    child.Arrange(childBounds);

                    // Clip to chart area.
                    if (elementThatRequiresArrange is UIElement && GetClipToChartArea(elementThatRequiresArrange))
                    {
                        UIElement element = (UIElement)elementThatRequiresArrange;
                        ClipElementToChartArea(element, childBounds);
                    }
                }
            }

            return(finalSize);
        }