how can i filter out NaN values and replace it with another value
Asked Answered
R

4

5

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");
}
Raft answered 19/1, 2014 at 7:1 Comment(1)
possible duplicate of Why is double.NaN not equal to itself?Rhebarhee
R
5

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

Regalado answered 19/1, 2014 at 7:9 Comment(0)
P
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).

Peon answered 19/1, 2014 at 7:8 Comment(0)
C
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))
{
   ...
}
Concentrated answered 19/1, 2014 at 7:10 Comment(0)
F
0

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

Fructiferous answered 18/3, 2021 at 16:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.