Java中无穷大、无穷小、非数、最大值、最小值

1. 先看一组表达式

public static void test1() {
    try {
        // System.out.println(1 / 0);
        System.out.println(0 / 0);
    } catch (Exception e) {
        System.out.println(e);
    } finally {
        System.out.println(1 / 0.0);
        System.out.println(1.0 / 0.0);
        System.out.println(1.0 / 0);
        System.out.println(-1.0 / 0);

    }
}
这段代码运行结果如下:
java.lang.ArithmeticException: / by zero
Infinity
Infinity
Infinity
-Infinity
可以看出:
  • 当分子、分母都为整型,分母为 0 的时候,会报除0异常
  • 当分子、分母中有一个为浮点型,且分母为 0 或 0.0 的时候,会得到 Infinity (正无穷大) 或 -Infinity(负无穷大)
原因(个人理解):
  • 分子、分母都为整型,且分母为0,除0异常,这基本就是规定
  • 如果分子、分母中有一个是浮点型时,Java会将整型自动转为浮点型;
    也就是说 1.0 / 0 -> 1.0 / 0.0,1 / 0.0 -> 1.0 / 0.0;
    大家都知道浮点型是不精确的,所以0.0 并不完全等于0,是无限趋近于0;
    而根据高数中无穷大的理解,分母无限趋近于0,分子为常数,会得到无穷大

2. Java中是如何表达无穷大和无穷小的

先看测试代码
public static void test2() {
    double d_p_i = Double.POSITIVE_INFINITY;  // 1.0 / 0.0, 正无穷大
    double d_n_i = Double.NEGATIVE_INFINITY;  // -1.0 / 0.0,负无穷大
    float f_p_i = Float.POSITIVE_INFINITY;    // 1.0f / 0.0f,正无穷大
    float f_n_i = Float.NEGATIVE_INFINITY;    // -1.0f / 0.0f,负无穷大
    System.out.println(d_p_i == f_p_i);
    System.out.println(d_n_i == f_n_i);
}
运行结果:
true
true
看源代码如何定义:
/**
 * A constant holding the positive infinity of type
 * {@code double}. It is equal to the value returned by
 * {@code Double.longBitsToDouble(0x7ff0000000000000L)}.
 */
public static final double POSITIVE_INFINITY = 1.0 / 0.0;

/**
 * A constant holding the negative infinity of type
 * {@code double}. It is equal to the value returned by
 * {@code Double.longBitsToDouble(0xfff0000000000000L)}.
 */
public static final double NEGATIVE_INFINITY = -1.0 / 0.0;

/**
 * A constant holding the positive infinity of type
 * {@code float}. It is equal to the value returned by
 * {@code Float.intBitsToFloat(0x7f800000)}.
 */
public static final float POSITIVE_INFINITY = 1.0f / 0.0f;

/**
 * A constant holding the negative infinity of type
 * {@code float}. It is equal to the value returned by
 * {@code Float.intBitsToFloat(0xff800000)}.
 */
public static final float NEGATIVE_INFINITY = -1.0f / 0.0f;
注意Double和Float的无穷大、无穷小相比,结果为 true

3. Java中的非数 (NaN)

其实很多表达式都会出现 NaN, 以下仅列出几个示例,看测试代码:
public static void test3() {
    double d_NaN = Double.NaN;  // 0.0d / 0.0
    float f_NaN = Float.NaN;    // 0.0f / 0.0f
    System.out.println(0.0 / 0.0);
    System.out.println(0 / 0.0);
    System.out.println(0.0 / 0);
    System.out.println(d_NaN == f_NaN);
}
运行结果:
NaN
NaN
NaN
false
注意这里的 Double的NaN 和 Float的NaN 相比,为false
看源代码如何表示:
/**
 * A constant holding a Not-a-Number (NaN) value of type
 * {@code double}. It is equivalent to the value returned by
 * {@code Double.longBitsToDouble(0x7ff8000000000000L)}.
 */
public static final double NaN = 0.0d / 0.0;

/**
 * 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;

4. Java常见类型的最大最小值,及所占二进制位数和字节数

直接看测试代码:
public static void test4() {
    System.out.println("\nByte: ");
    System.out.println(Byte.SIZE);
    System.out.println(Byte.BYTES);
    System.out.println(Byte.MAX_VALUE);
    System.out.println(Byte.MIN_VALUE);

    System.out.println("\nShort: ");
    System.out.println(Short.SIZE);
    System.out.println(Short.BYTES);
    System.out.println(Short.MAX_VALUE);
    System.out.println(Short.MIN_VALUE);

    System.out.println("\nInteger: ");
    System.out.println(Integer.SIZE);
    System.out.println(Integer.BYTES);
    System.out.println(Integer.MAX_VALUE);
    System.out.println(Integer.MIN_VALUE);

    System.out.println("\nLong: ");
    System.out.println(Long.SIZE);
    System.out.println(Long.BYTES);
    System.out.println(Long.MAX_VALUE);
    System.out.println(Long.MIN_VALUE);

    System.out.println("\nFloat: ");
    System.out.println(Float.SIZE);
    System.out.println(Float.BYTES);
    System.out.println(Float.MAX_VALUE);
    System.out.println(Float.MIN_VALUE);

    System.out.println("\nDouble: ");
    System.out.println(Double.SIZE);
    System.out.println(Double.BYTES);
    System.out.println(Double.MAX_VALUE);
    System.out.println(Double.MIN_VALUE);
}
输出结果为:
Byte: 
8
1
127
-128

Short: 
16
2
32767
-32768

Integer: 
32
4
2147483647
-2147483648

Long: 
64
8
9223372036854775807
-9223372036854775808

Float: 
32
4
3.4028235E38
1.4E-45

Double: 
64
8
1.7976931348623157E308
4.9E-324
查看源代码,以 Integer 为例:
/**
 * The number of bits used to represent an {@code int} value in two's
 * complement binary form.
 *
 * @since 1.5
 */
@Native public static final int SIZE = 32;

/**
 * The number of bytes used to represent a {@code int} value in two's
 * complement binary form.
 *
 * @since 1.8
 */
public static final int BYTES = SIZE / Byte.SIZE;

/**
 * A constant holding the minimum value an {@code int} can
 * have, -2<sup>31</sup>.
 */
@Native public static final int   MIN_VALUE = 0x80000000;

/**
 * A constant holding the maximum value an {@code int} can
 * have, 2<sup>31</sup>-1.
 */
@Native public static final int   MAX_VALUE = 0x7fffffff;

注:本人代码都是基于 jdk1.8 运行
其实还有很多的常量我们都不是经常用到,这里仅列出一部分,仅供参考。其它的需要各位在实践中多去了解

猜你喜欢

转载自blog.csdn.net/qq_34732088/article/details/80927117