Float isNaN 0.0f / 0.0f

 /**
     * 如果是一个数字 返回false,否则返回true
     * Returns {@code true} if the specified number is a
     * Not-a-Number (NaN) value, {@code false} otherwise.
     * 
     * @param   v   the value to be tested. 被校验的参数v
     * @return  {@code true} if the argument is NaN; 如果不是数字 返回true
     *          {@code false} otherwise. 返回false
     */
    public static boolean isNaN(float v) {
        return (v != v);
    }

刚看见这个方法 就很好奇 v !=v v还是一个float类型的,这就尴尬了
哪儿遇到过这种情况, 1f != 1f ??? 自己不等于自己 ???
还真有这种情况 比如

float f = 10f/0f;
System.out.println(f);
// 输出结果 
NaN

Float类中也有一个NaN的变量 对 就是 除数是0f的情况 ,应该只有这种情况是NaN了吧

  /**
     * A constant holding a Not-a-Number (NaN) value of type
     * {@code float}.  It is equivalent to the value returned by
     * {@code Float.intBitsToFloat(0x7fc00000)}.
     */
    public static final float NaN = 0.0f / 0.0f;
发布了431 篇原创文章 · 获赞 91 · 访问量 25万+

猜你喜欢

转载自blog.csdn.net/qq_36291682/article/details/99702018
0.0