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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
NaNstands for Not-a-Number. It can arise in a variety of ways, for example as a result of0./0.,sqrt(-1), or as the result of a calculation involving otherNaNs.The easiest way to check whether
vis aNaNis by usingDouble.isNaN(v):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 theNaNyou could simply be masking the bug instead of fixing it.