0

I have simple GUI a program where the real time plot(using WinForms chart) crashes due to non-double values like infinity or NaN. So I want to filter values such as infinity,NaN or ect. And only plot valid double values. It can also be any other non-double type.

I try the following:

if(!double.IsInfinity(value)){

chart1.Series["mySerie"].Points.AddY(value);

}

But the above only checks if the value is not infinity not other non-double possibilities. In my case Double. TryParse also would not work because it is used to check whether text is a double.

So in my case I receive data from a device normally a double. But sometimes it outputs non-double values and is there a quick fix for it?

Dominique
  • 16,450
  • 15
  • 56
  • 112
user1999
  • 199
  • 8
  • Does this answer your question? [Shortest way of checking if Double is "NaN"](https://stackoverflow.com/questions/24697495/shortest-way-of-checking-if-double-is-nan) and [How to check if the value of string variable is double](https://stackoverflow.com/questions/48228494/how-to-check-if-the-value-of-string-variable-is-double) –  Jun 16 '21 at 08:49
  • Some hack would be to just check `value > MIN_ACCEPTABLE && value < MAX_ACCEPTABLE`, with the boundaries chosen as the borderline meaningful values for your plot (depends on application). E.g. between 0 and 1e6 – Alexey S. Larionov Jun 16 '21 at 08:52
  • What are "non-double" values? As far as I (and IEEE 754) am concerned, infinities and NaNs are all valid `double` values. – Sweeper Jun 16 '21 at 08:53
  • @AlexeyLarionov I think that would inherently filter any non-double values correct? – user1999 Jun 16 '21 at 08:54
  • Also perhaps you can use [double.MinValue](https://learn.microsoft.com/dotnet/api/system.double.minvalue) and MaxValue. but using axis min and max should be better if it is the chart that refuses data. –  Jun 16 '21 at 08:56
  • @Sweeper Im trying to fix this issue: https://social.msdn.microsoft.com/Forums/vstudio/en-US/ca55daf4-182e-4cab-8e04-ebf7fd601a8f/systeminvalidoperationexception-axis-object-8211-auto-interval-error-due-to-invalid-point?forum=MSWinWebChart – user1999 Jun 16 '21 at 08:56
  • What about creating an extension method that simply checks whether your value is valid by calling ```Double.IsNaN``` and ```Double.IsInfinity```? – devsmn Jun 16 '21 at 09:07

3 Answers3

0

Double has a few static methods that could help you do this. If you want to check the different states individually you can use the following methods:

Double.IsInfinite(value); //returns true if the value is infinite
Double.IsNaN(value); //returns true if the value is NaN

As you want to disregard other non-double values, I would suggest using one of the following:

Double.IsNormal(value); //returns true if the value is normal
Double.IsSubnormal(value); //returns true if the value is subnormal
Double.IsFinite(value) //returns true if the value is zero, normal, or subnormal

Any of these will help filter your input, so use the one that matches the input that you need. IsFinite is the only one of the 3 I believe that will return true if the value is 0

The entire set of methods for the Double type can be found here

Glenn Keates
  • 121
  • 8
-1

To illustrate my comment with a range of acceptable values:

using System;
                    
public class Program
{
    public static bool Check(double x) {
        double min = -1e6, max = 1e6;
        return x > min && x < max;  
    }
        
    public static void Main()
    {
        Console.WriteLine($"NaN {Check(double.NaN)}");               // False
        Console.WriteLine($"-inf {Check(double.NegativeInfinity)}"); // False
        Console.WriteLine($"-1e7 {Check(-1e7)}");                    // False
        Console.WriteLine($"0 {Check(0)}");                          // True
        Console.WriteLine($"1e7 {Check(1e7)}");                      // False
        Console.WriteLine($"+inf {Check(double.PositiveInfinity)}"); // False
    }
}
Alexey S. Larionov
  • 6,555
  • 1
  • 18
  • 37
-1

This should work

    if(Double.IsNaN(value)||Double.IsInfinity(value) ){
        //do nothing
    }else{
        chart1.Series["mySerie"].Points.AddY(value);
    }
Sekhar
  • 5,614
  • 9
  • 38
  • 44