Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to represent NaN in array of numbers?

Tags:

java

nan

The question says it all. I have an array of doubles and am doing something with them.

double expectedOutput[] = { 6.38792, 12.91079, 14.33333, 13.44517,
                12.34539, 12.05397, 8.34061, 2.07900, -2.01999, -5.47802,
                -8.21610, -9.26719, -11.02378 };

Ideally, i would test to see if

6.38792 == 6.38792 and end up with a 'pass'

Under certain conditions, i end up with the situation like

6.38792 != NaN

Knowing that this is a valid case sometimes, how can i represent NaN in my code?

I either need to include NaNs into my array of expected elements or somehow figure out that result is Not A Number

I am using Java

like image 724
James Raitsev Avatar asked Dec 29 '22 01:12

James Raitsev


2 Answers

In Java, you can get NaN by using

Double.NaN

So you can just put this into your array.

If your question is how to check if something is NaN, you can call

Double.isNan(/* ... value ... */);
like image 104
templatetypedef Avatar answered Jan 13 '23 17:01

templatetypedef


You'll have to test for it explicitly, since NaN != NaN, you can't just include it in your array. You have to use Double.isNaN(x).

like image 20
porges Avatar answered Jan 13 '23 18:01

porges