4

iv'e tried using a for loop to filter out the null and nan values but still the nan value is added to the listview. this is just a part where the calculation is done.

double rs1 = (qz1 * c11) + (asg1 * c22) + (sw1 * c33) + (prj1 * c44) + (pxm1 * c55) + (atti1 * c66);
if(rs1 != double.NaN || rs1 != null)
{
lst.SubItems.Add(Math.Round(rs1, 2).ToString());
}
else
{
lst.SubItems.Add("0");
}
  • possible duplicate of [Why is double.NaN not equal to itself?](http://stackoverflow.com/questions/1145443/why-is-double-nan-not-equal-to-itself) – codeling Jan 19 '14 at 08:51

4 Answers4

4

use IsNan static function , read about it here

Your code should look like this

if(!Double.IsNaN(rs1)  || rs1 != null)

NaN with NaN will always return false, this is MSDN about NaN

Two NaN values are considered unequal to one another. Therefore, it is not possible to determine whether a value is not a number by using the equality operator to compare it to another value that is equal to NaN

Mzf
  • 5,210
  • 2
  • 24
  • 37
1

Unfortunately, when you test for inequality with double.NaN, it always returns true (the value itself is undefined). It's better to use double.IsNaN(rs1).

TJ27
  • 308
  • 1
  • 7
1

First, it can't be null because double is value type not reference type.So rs1 != null is redundant.Second you should use IsNaN method instead of check equality with ==

if(!double.IsNaN(rs1))
{
   ...
}
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
0

if(!Double.IsNaN(rs1)(float x = 0; x = (float)rs1;}