zoukankan      html  css  js  c++  java
  • java除0问题探究 Yannis

    今天在写代码的时候,突然发现一个小问题:

    double a = 0;
    System.out.println(a/0);

    这部分竟然出乎我意料的没有报除零异常。而是打印NaN。思索之后,感觉问题关键可能是出在数据类型上。修改a为int类型。果然出现熟悉的/zero错误。这时候,突然想起,以前看过文章说过该问题,但现在的我已经忘记了当时的思想。所以重新寻找思路。各种搜索之后,看了看API发现问题的根源。附API的代码:

    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;
    通过查看源码发现java中对于Double的类型,将该情况定义为静态变量。所以得到上面没有报错就很正常了。
    人生苦短,我用python
  • 相关阅读:
    Currency Exchange
    Robot Motion
    Crashing Robots
    Parencodings
    Y2K Accounting Bug
    Tautology
    Power of Cryptography
    Radar Installation -poj 1328
    The Pilots Brothers' refrigerator
    【java】之cron表达式
  • 原文地址:https://www.cnblogs.com/pigga/p/10098348.html
Copyright © 2011-2022 走看看