Java 为什么 Double.MIN_VALUE 不是负数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3884793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is Double.MIN_VALUE in not negative
提问by mo-seph
Can anyone shed some light on why Double.MIN_VALUE
is not actually the minimum value that Doubles can take? It is a positive value, and a Double can of course be negative.
谁能解释为什么Double.MIN_VALUE
实际上不是双打可以取的最小值?它是一个正值,Double 当然可以是负值。
I understand why it's a useful number, but it seems a very unintuitive name, especially when compared to Integer.MIN_VALUE
. Calling it Double.SMALLEST_POSITIVE
or MIN_INCREMENT
or similar would have clearer semantics.
我理解为什么它是一个有用的数字,但它似乎是一个非常不直观的名称,尤其是与Integer.MIN_VALUE
. 调用它Double.SMALLEST_POSITIVE
或MIN_INCREMENT
类似的会有更清晰的语义。
Also, what is the minimum value that Doubles can take? Is it -Double.MAX_VALUE
? The docs don't seem to say.
另外,双打可以取的最小值是多少?是-Double.MAX_VALUE
吗?文档似乎没有说。
采纳答案by aioobe
The IEEE 754 format has one bit reserved for the sign and the remaining bits representing the magnitude. This means that it is "symmetrical" around origo (as opposed to the Integer values, which have one more negative value). Thus the minimum value is simply the same as the maximum value, with the sign-bit changed, so yes, -Double.MAX_VALUE
is the smallest possible actual number you can represent with a double
.
IEEE 754 格式为符号保留一位,其余位代表幅度。这意味着它围绕 origo 是“对称的”(与整数值相反,后者还有一个负值)。因此,最低值就是一样的最大值,用符号位改变,所以是的,-Double.MAX_VALUE
是你可以用一个代表最小的实际数量double
。
I suppose the Double.MAX_VALUE
should be seen as maximum magnitude, in which case it actually makes sense to simply write -Double.MAX_VALUE
. It also explains why Double.MIN_VALUE
is the least positive value (since that represents the least possible magnitude).
我想Double.MAX_VALUE
应该被视为最大量级,在这种情况下,简单地编写-Double.MAX_VALUE
. 它还解释了为什么Double.MIN_VALUE
是最小正值(因为它代表了最小可能的量级)。
But sure, I agree that the naming is a bit misleading. Being used to the meaning Integer.MIN_VALUE
, I too was a bit surprised when I read that Double.MIN_VALUE
was the smallest absolutevalue that could be represented. Perhaps they thought it was superfluous to have a constant representing the least possible value as it is simply a -
away from MAX_VALUE
:-)
但可以肯定的是,我同意命名有点误导。习惯了这个意思Integer.MIN_VALUE
,当我读到它Double.MIN_VALUE
是可以表示的最小绝对值时,我也有点惊讶。也许他们认为拥有一个代表最小可能值的常数是多余的,因为它只是-
远离MAX_VALUE
:-)
(Note, there is also Double.NEGATIVE_INFINITY
but I'm disregarding from this, as it is to be seen as a "special case" and does not in fact represent any actual number.)
(注意,也有Double.NEGATIVE_INFINITY
但我不理会这一点,因为它被视为“特殊情况”,实际上并不代表任何实际数字。)
Hereis a good text on the subject.
这里有一篇关于这个主题的好文章。
回答by Colin Hebert
The minimum value for a double is Double.NEGATIVE_INFINITY
that's why Double.MIN_VALUE
isn't really the minimum for a Double
.
double 的最小值就是Double.NEGATIVE_INFINITY
为什么Double.MIN_VALUE
不是 a 的最小值的原因Double
。
As the double are floating point numbers, you can only have the biggest number (with a lower precision) or the closest number to 0 (with a great precision).
由于 double 是浮点数,因此您只能拥有最大的数字(精度较低)或最接近 0 的数字(精度较高)。
If you really want a minimal value for a double that isn't infinity then you can use -Double.MAX_VALUE
.
如果你真的想要一个不是无穷大的 double 的最小值,那么你可以使用-Double.MAX_VALUE
.
回答by John Gardner
Because with floating point numbers, the precisionis what is important as there's no exact range.
因为对于浮点数,精度很重要,因为没有精确的 range。
/**
* A constant holding the smallest positive nonzero value of type
* <code>double</code>, 2<sup>-1074</sup>. It is equal to the
* hexadecimal floating-point literal
* <code>0x0.0000000000001P-1022</code> and also equal to
* <code>Double.longBitsToDouble(0x1L)</code>.
*/
But i agree that it should probably have been named something better :)
但我同意它可能应该被命名为更好的 :)
回答by Durandal
These constants have nothing to do with sign. This makes more sense if you consider a double as a composite of three parts: Sign, Exponent and Mantissa. Double.MIN_VALUE is actually the smallest value Mantissa can assume when the Exponent is at minimun value before a flush to zero occurs. Likewise MAX_VALUE can be understood as the largest value Mantissa can assume when the Exponent is at maximum value before a flush to infinity occurs.
这些常数与符号无关。如果您将 double 视为由三个部分组成的组合,则这更有意义:符号、指数和尾数。Double.MIN_VALUE 实际上是当指数在刷新为零之前处于最小值时尾数可以假定的最小值。同样,MAX_VALUE 可以理解为当指数处于最大值时尾数可以假设的最大值,然后才会出现刷新到无穷大的情况。
A more descriptive name for these two could be Largest Absolute(add non-zero for verbositiy) and Smallest Absolutevalue (add non-infinity for verbositiy).
这两个更具描述性的名称可以是最大绝对值(为详细程度添加非零)和最小绝对值(为详细程度添加非无穷大)。
Check out the IEEE 754 (1985)standard for details. There is a revised (2008) version, but that only introduces more formats which aren't even supported by java (strictly speaking java even lacks support for some mandatory features of IEEE 754 1985, like many other high level languages).
有关详细信息,请查看IEEE 754 (1985)标准。有一个修订版(2008 年),但它只引入了更多 Java 甚至不支持的格式(严格来说,Java 甚至不支持 IEEE 754 1985 的某些强制性功能,就像许多其他高级语言一样)。
回答by Philipp Cla?en
I assume the confusing names can be traced back to C, which defined FLT_MIN
as the smallest positive number.
我认为混淆的名称可以追溯到 C,它定义FLT_MIN
为最小的正数。
Like in Java, where you have to use -Double.MAX_VALUE
, you have to use -FLT_MAX
to get the smallest float in C.
就像在 Java 中,你必须使用-Double.MAX_VALUE
,你必须使用-FLT_MAX
来获得 C 中的最小浮点数。
回答by prime
As it says in the documents,
正如文件中所说,
Double.MIN_VALUE is a constant holding the smallest POSITIVEnonzero value of type double, 2^(-1074).
Double.MIN_VALUE 是一个常量,包含 double 类型的最小POSITIVE非零值,2^(-1074)。
The trick here is we are talking about a floating point number representation. The double data type is a double-precision 64-bit IEEE 754 floating point. Floating points represent numbers from 1,000,000,000,000to 0.0000000000000001with ease, and while maximizing precision (the number of digits) at both ends of the scale. (For more refer this)
这里的技巧是我们谈论的是浮点数表示。double 数据类型是双精度 64 位 IEEE 754 浮点数。浮点数可以轻松表示从1,000,000,000,000到0.0000000000000001 的数字,同时最大限度地提高刻度两端的精度(位数)。(更多参考这个)
The mantissa, always a positive number, holds the significant digits of the floating-point number. The exponent indicates the positive or negative power of the radix that the mantissa and sign should be multiplied by. The four components are combined as followsto get the floating-point value.
该尾数,始终为正数,持有浮点数的显著数字。指数表示尾数和符号应乘以的基数的正或负幂。四个分量按如下组合得到浮点值。
Think that the MIN_VALUE is the minimum value that the mantissa can represent. As the minimum values of a floating point representation is the minimum magnitude that can be represented using that. (Could have used a better name to avoid this confusion though)
认为MIN_VALUE 是尾数可以表示的最小值。由于浮点表示的最小值是可以使用它表示的最小幅度。(虽然可以使用更好的名称来避免这种混淆)
123 > 10 > 1 > 0.12 > 0.012 > 0.0000123 > 0.000000001 > 0.0000000000000001
123 > 10 > 1 > 0.12 > 0.012 > 0.0000123 > 0.000000001 > 0.0000000000000001
Below is just FYI.
以下仅供参考。
Double-precision floating-point can represent 2,098 powers of two, from 2^-1074 through 2^1023. Denormalized powers of two are those from 2^-1074 through 2^-1023; normalized powers of two are those from 2^-1022 through 2^1023. Refer thisand this.
双精度浮点数可以表示从 2^-1074 到 2^1023 的 2,098 次幂。2 的非规范化幂是从 2^-1074 到 2^-1023 的幂;2 的归一化幂是从 2^-1022 到 2^1023 的幂。参考这个和这个。