简体   繁体   English

在Java中,如果在操作中使用Double.NaN会发生什么?

[英]In Java, what happens if you use Double.NaN in an operation?

I have compiled code that erroneously tries to add a number and Double.NaN. 我编译了错误地尝试添加数字和Double.NaN的代码。 I'm wondering if it's throwing an exception that's not getting caught? 我想知道它是否会抛出一个未被捕获的异常? Does anyone know how that situation is handled? 有谁知道这种情况是如何处理的?
Thanks. 谢谢。

Adding a number to NaN gives NaN. 向NaN添加数字会产生NaN。 It isn't expected to cause an exception. 预计不会引起异常。 I understand that this conforms to IEEE 754. 据我所知,这符合IEEE 754。

To answer Steve B's question: 回答Steve B的问题:

POSITIVE_INFINITY is the largest postive number that you can store if you have unlimited storage space. 如果您拥有无限的存储空间,POSITIVE_INFINITY是您可以存储的最大正数。 Without this luxury we have to use a construction like 1.0 / 0.0 which does a fine job. 如果没有这种奢侈品,我们必须使用像1.0 / 0.0这样的结构。 Same goes for NEGATIVE_INFINITY but then the largest negative number. NEGATIVE_INFINITY也是如此,但是最大的负数。

NaN is normally defined as 0.0 / 0.0 because there is no such number as 0/0 so that perfectly qualifies for a NaN. NaN通常定义为0.0 / 0.0,因为没有0/0这样的数字,因此完全符合NaN的要求。

public static void main(String args[])
{
    Double d = Double.NaN + 1.0;
    System.out.println(d);
}

prints Double.Nan. 打印Double.Nan。 Can anyone explain the source implementation? 任何人都可以解释源实现吗?

  public static final double POSITIVE_INFINITY = 1.0 / 0.0;
  public static final double NEGATIVE_INFINITY = -1.0 / 0.0;
  public static final double NaN = 0.0d / 0.0;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM