什么是 NaN?以及是如何创建的?

发布于 2024-12-28 19:39:25 字数 159 浏览 3 评论 0原文

我有一个 double[],它的值为 NaN。当我将数组的元素相加时,NaN 也使结果为 NaN

它是怎么发生的?如何防止此 NaN 添加到数组的其余部分?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

那伤。 2025-01-04 19:39:25

NaN 代表非数字。它可以以多种方式出现,例如由于0 ./0.sqrt(-1),或作为涉及其他 NaN 的计算结果。

检查 v 是否为 NaN 的最简单方法是使用 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;
}

编辑: @Stephen C在评论中提出了一个很好的观点:在决定忽略该 NaN 之前,明智的做法是了解它的来源。这可能是代码中其他地方的错误造成的,如果盲目地忽略 NaN,您可能只是掩盖了该错误而不是修复它。

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.

谈场末日恋爱 2025-01-04 19:39:25

NaN 代表“Not a Number”,因此“Not A Number”+ 10 =“Not a Number”

您可能需要考虑调试您的应用程序以查找双数组中的内容:)

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 :)

郁金香雨 2025-01-04 19:39:25

我怎样才能防止这个 NaN 不被添加到我的 double[] 的其余部分

这样:

double[] array = something;
double sum = 0;
for (int i = 0; i < array.length; i++) {
  if (!Double.isNaN(array[i])) {
    sum += array[i];
  }
}

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];
  }
}
誰認得朕 2025-01-04 19:39:25

RTFM:

NaN 的 Javadoc

公共静态最终双精度 NaN

保存 double 类型的非数字 (NaN) 值的常量。它相当于 Double.longBitsToDouble(0x7ff8000000000000L) 返回的值。

JVM 规范的相关部分说任何涉及 NaN 的操作也是 NaN(有点像 SQL 中的 null

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)

笑红尘 2025-01-04 19:39:25

你不能忽略 NaN,它表明你的程序有问题。您需要找出导致 NaN 的原因并修复它。

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.

把时间冻结 2025-01-04 19:39:25

NaN - 不是数字

顺便尝试一下:

double[] array = new double[10];
String result = StringUtils.join(array);
System.out.println(result+ " BATMAN !");

NaN - Not a Number

btw try :

double[] array = new double[10];
String result = StringUtils.join(array);
System.out.println(result+ " BATMAN !");
-小熊_ 2025-01-04 19:39:25

Java 不是我的强项,但在其他语言中,这来自于从字符串中分配值。试试这个:

parseDouble

public static double parseDouble(String s)
                          throws NumberFormatException

返回一个新的 double,初始化为指定 String 表示的值,如 Double 类的 valueOf 方法执行的那样。
参数:
s - 要解析的字符串。
返回:
由字符串参数表示的双精度值。
投掷:
NumberFormatException - 如果字符串不包含可解析的双精度值。
自从:
1.2
参见:
valueOf(String)

取自此处

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文