为什么Java中的Float.MIN_VALUE是正值? [英] Why is Float.MIN_VALUE in Java a positive 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,因为 While the name is debatable as the "true minimum value" of a 要了解为什么这是最小价值",需要对Java有所了解(或 IEEE- 754 )浮点值有效.有了这种见识,在阅读了文档之后,很明显 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 真实最小值"是 The "true minimum value" is 这与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屋!
float
的真实最小值"是-Float.MAX_VALUE
,但我怀疑选择MIN_VALUE
是为了与其他数字类型保持一致.使用名称MIN_RANGE_VALUE
和MAX_RANGE_VALUE
(或类似名称)可能会使区别更加明显.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.Float.MIN_VALUE
是浮点数的尾数和指数分量可表示的最小非零值.或者,浮点数可以表示的最小正值.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 是作为离散位存储的,因此该范围限制对于正数和负数都是相同的.-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.