2

I have a double[] and it has a value which is NaN. When I add up the array's elements, the NaN makes the result NaN too.

How did it happen? How can I prevent this NaN from being added to the rest of my array?

NPE
  • 486,780
  • 108
  • 951
  • 1,012
lonesome
  • 2,503
  • 6
  • 35
  • 61
  • 9
    http://en.wikipedia.org/wiki/NaN – shikhar Jan 23 '12 at 12:12
  • 9
    Question shows complete lack of even attempting research. – Ray Jan 23 '12 at 12:16
  • 4
    @Ray i know what is NaN, if u read the question, u can see i asked about how to prevent adding NaN to the rest of array...wanna say my knowledge is law, im not afraid, its ok to me, by time passes i improve myself, any problem? :) – lonesome Jan 23 '12 at 12:26

7 Answers7

23

NaN stands for Not-a-Number. It can arise in a variety of ways, for example as a result of 0./0., sqrt(-1), or as the result of a calculation involving other NaNs.

The easiest way to check whether v is a NaN is by using Double.isNaN(v):

public static double sum(double arr[]) {
  double sum = 0.0;
  for (double val : arr) {
    if (!Double.isNaN(val)) {
      sum += val;
    }
  }
  return sum;
}

edit: @Stephen C makes a good point in the comments: before deciding to ignore that NaN, it would be prudent to understand where it came from. It could be that it is the result of a bug elsewhere in your code, and by blindly ignoring the NaN you could simply be masking the bug instead of fixing it.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • 2
    Nice advice ... but also dangerous. If you ignore NaN's without understanding where they came from, you run the risk of hiding bugs in your program and producing invalid output. – Stephen C Jan 23 '12 at 12:32
  • @StephenC: Good point, thanks. I've edited the answer to include a note to that effect. – NPE Jan 23 '12 at 12:37
4

NaN stands for "Not a Number", so "Not A Number" + 10 = "Not a Number"

You might want to consider debuggin your app to find what's in the double array :)

f2lollpll
  • 997
  • 6
  • 15
  • its an array which i created to store results of a mathematical formula, then it happened to be a NaN in the array, so any way to delete this NaN? – lonesome Jan 23 '12 at 12:15
  • this answer seems legit http://stackoverflow.com/a/8971425/883671 – f2lollpll Jan 23 '12 at 12:19
3

how can i prevent that this NaN not bein added to the rest of my double[]

This way:

double[] array = something;
double sum = 0;
for (int i = 0; i < array.length; i++) {
  if (!Double.isNaN(array[i])) {
    sum += array[i];
  }
}
Joonas Pulakka
  • 36,252
  • 29
  • 106
  • 169
  • 3
    Though technically correct, encouraging the OP to ignore a NaN value in a mathematical program is ***not*** a good idea! – Perception Jan 23 '12 at 12:24
  • @Perception: Whether or not ignoring NaNs makes sense depends on the problem at hand. You could - in some contexts - think of NaN as empty Excel cells. When calculating sum over a range of cells, Excel treats empty cells as zeros, which is perfectly reasonable. – Joonas Pulakka Jan 23 '12 at 20:36
  • 1
    You've drawn an incredibly thin edge case based on the shaky assumption that a Java sentinel value is akin to a spreadsheet cell. I have yet to see a definition of NaN that equates it to zero, which is a perfectly valid number. – Perception Jan 23 '12 at 20:46
  • 2
    @Perception: Who said that zero is not a perfectly valid number, or that NaN is zero? *Ignoring* NaNs is totally different from *treating them as zeros*. In a sum, ignoring happens to be the equal to adding zero `(x+0=x)`, but in the generic case, e.g. when calculating average, it would be different: you just don't count the NaNs. In fact, that's what NaNs were originally invented for: representing missing values. – Joonas Pulakka Jan 24 '12 at 14:23
1

RTFM:

Javadoc for NaN

public static final double NaN

A constant holding a Not-a-Number (NaN) value of type double. It is equivalent to the value returned by Double.longBitsToDouble(0x7ff8000000000000L).

And relevant section of the JVM spec says any operation involving a NaN is also a NaN (a bit like a null in SQL)

Community
  • 1
  • 1
Bohemian
  • 412,405
  • 93
  • 575
  • 722
1

You can't just ignore the NaN, it's an indicator of a problem with you're program. You need to find out what is causing the NaN and fix that.

Perception
  • 79,279
  • 19
  • 185
  • 195
0

NaN - Not a Number

btw try :

double[] array = new double[10];
String result = StringUtils.join(array);
System.out.println(result+ " BATMAN !");
light_303
  • 2,101
  • 2
  • 18
  • 35
0

Java isn't my strong suit, but in other languages this comes from assigning a value from a string. Try this:

parseDouble

public static double parseDouble(String s)
                          throws NumberFormatException

Returns a new double initialized to the value represented by the specified String, as performed by the valueOf method of class Double. Parameters: s - the string to be parsed. Returns: the double value represented by the string argument. Throws: NumberFormatException - if the string does not contain a parsable double. Since: 1.2 See Also: valueOf(String)

Taken from here.

LJ Wilson
  • 14,445
  • 5
  • 38
  • 62
  • The `parseDouble()` does not produce `NaN` values ... unless you try to parse the String `"NaN"`. If you attempt to parse an invalid number string, you get an exception, not a `NaN`. – Stephen C Jan 23 '12 at 12:34