为什么Java中的Float.MIN_VALUE是正值? [英] Why is Float.MIN_VALUE in Java a positive value?

查看:356
本文介绍了为什么Java中的Float.MIN_VALUE是正值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您认为Float.MIN_VALUE等于什么?

What do you think Float.MIN_VALUE equals to?

下面的代码说明了我最近5个小时去的地方,尝试解决一个错误.

The next code explains where my last 5 hours went to, trying solving a bug.

public static void main(String[] args) {
    compareToZero(Float.MIN_VALUE); // Out = true false false
    compareToZero(Float.MAX_VALUE); // Out = true false false

    System.out.println("Float minimum " + Float.MIN_VALUE); // Out = 1.4E-45
    System.out.println("Float maximum " + Float.MAX_VALUE); // Out = 3.4028235E38
}

private static void compareToZero(float value1) {
    System.out.print((value1 > 0) + " ");
    System.out.print((value1 < 0) + " ");
    System.out.print((value1 == 0) + "\n");
}

我没想到float的最小值将为正值……找不到任何用途.

I didn't imagined that minimum value of float will be a positive value... Can't find any use for it.

推荐答案

每个

一个常数,它持有float最小的正非零值2-149.它等于十六进制浮点文字0x0.000002P-126f,也等于Float.intBitsToFloat(0x1).

A constant holding the smallest positive nonzero value of type float, 2-149. It is equal to the hexadecimal floating-point literal 0x0.000002P-126f and also equal to Float.intBitsToFloat(0x1).

虽然名称值得商as,因为float的真实最小值"是-Float.MAX_VALUE,但我怀疑选择MIN_VALUE是为了与其他数字类型保持一致.使用名称MIN_RANGE_VALUEMAX_RANGE_VALUE(或类似名称)可能会使区别更加明显.

While the name is debatable as the "true minimum value" of a float is -Float.MAX_VALUE, I suspect MIN_VALUE was chosen for consistency with the other numeric types. Using the names MIN_RANGE_VALUE and MAX_RANGE_VALUE (or similar) might have made the difference more clear.

要了解为什么这是最小价值",需要对Java有所了解(或 IEEE- 754 )浮点值有效.有了这种见识,在阅读了文档之后,很明显Float.MIN_VALUE浮点数的尾数和指数分量可表示的最小非零值.或者,浮点数可以表示的最小正值.

To understand why this is the "minimum value" requires understanding a little bit about how Java (or IEEE-754) floating point values work. With this insight, after reading the documentation, it is clear that Float.MIN_VALUE is the minimum non-zero value representable by the mantissa and exponent components of a float. Or, the smallest positive value a float can represent.

真实最小值"是-Float.MAX_VALUE,因为Float.MAX_VALUE表示浮点数的尾数和指数分量可以表示的最大值.由于浮点数的 sign 是作为离散位存储的,因此该范围限制对于正数和负数都是相同的.

The "true minimum value" is -Float.MAX_VALUE because Float.MAX_VALUE represents the maximum value that the mantissa and exponent components of a float can represent. Since the sign for a float is stored as a discrete bit, this range limit is the same for both positive and negative numbers.

这与Java(以及大多数CPU)中整数的工作方式不同:它们使用 two进行编码补. (某些计算机系统使用了一个离散的符号位,称为一个人的补码",它的两个整数值为零:0和-0 !!)

This differs from how integers work in Java (and on most CPUs): they are encoded using two's complement. (Some computer systems used a discrete sign bit, which is called "one's complement", which then has two integer values of zero: 0 and -0!)

研究愉快!

这篇关于为什么Java中的Float.MIN_VALUE是正值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆