java中float的最小值为什么是正的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9746850/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:03:58  来源:igfitidea点击:

min value of float in java is positive why?

java

提问by ajaycoolfbd

when we use MIN_VALUE function on either of primitive types in java it give us minimum value possible for that type. BUT in case of float and double it returned minimum positive value though float and double can have negative values also.

当我们在 Java 中的任何一个原始类型上使用 MIN_VALUE 函数时,它都会为我们提供该类型的最小值。但是在 float 和 double 的情况下,它返回最小正值,尽管 float 和 double 也可以具有负值。

回答by Stephen C

min value of float in java is positive why?

java中float的最小值为什么是正的?

Why they chose to name the constants like that is unanswerable (by us) because nobody on SO was in the room when the decision was made.

为什么他们选择这样命名常量是(我们)无法回答的,因为在做出决定时,SO 上没有人在房间里。

Besides, knowing the answer to the question is not going to help, because the values of Float.MIN_VALUEand Double.MIN_VALUEwon't be changed, no matter how "wrong" they might be. (It would break any existing code that uses these constants, and the Java designers only do that when there is no other viable alternative. Leaving it alone is clearly a viable alternative.)

此外,知道问题的答案是不会帮助,因为价值观Float.MIN_VALUEDouble.MIN_VALUE不会改变,无论怎么“错”他们可能。(它会破坏任何使用这些常量的现有代码,而 Java 设计者只有在没有其他可行的替代方案时才会这样做。不管它是什么,显然是一个可行的替代方案。)

I suppose, the answer (i.e. the real reason for the decision) mightbe relevant to people developing brand new programming languages. However, they are going to have to make up their own minds anyway. FWIW, I wouldn't have designed it this way, but that's not relevant.

我想,答案(即决定的真正原因)可能与开发全新编程语言的人有关。然而,无论如何,他们将不得不做出自己的决定。FWIW,我不会这样设计,但这无关紧要。

回答by XiaoChuan Yu

MIN_VALUEtells you how precise a float can get, but not the mathematical minimum it can represent. They should have named it better...

MIN_VALUE告诉您浮点数可以达到的精确度,但不是它可以表示的数学最小值。他们应该更好地命名它......

Negative of MAX_VALUEis the mathematical minimum value for floats(same goes for double).

MAX_VALUE 的负数是浮点数的数学最小值(double 也是如此)。

The reason you can assume this has to do with how numbers are represented in binary:

您可以假设这与数字如何以二进制表示的原因有关:

Java float and doubles use sign bit to represent negative/positive values as opposed to two's complement representation for integers. This means it's positive and negative value have the same magnitude, ie. -(positive max) = negative max. Therefore you don't need to define an actual mathematical minimum constant for floats because you can just use the positive max constant to figure out the what the negative bound is. For integers, you need 2 different constants defining max/min because of the way they represented in binary, i.e. -max != min.

Java 浮点数和双精度数使用符号位来表示负/正值,而不是整数的二进制补码表示。这意味着它的正值和负值具有相同的大小,即。-(正最大值)= 负最大值。因此,您不需要为浮点数定义实际的数学最小常数,因为您只需使用正的最大常数即可确定负界限是什么。对于整数,由于它们以二进制表示的方式,您需要 2 个不同的常量来定义 max/min,即 -max != min。

For more info http://people.uncw.edu/tompkinsj/133/numbers/Reals.htm

更多信息http://people.uncw.edu/tompkinsj/133/numbers/Reals.htm

回答by Willem Van Onsem

MIN_VALUEshould be named EPSILONit's the smallest postive value a float can represent.

MIN_VALUE应该命名EPSILON为浮点数可以表示的最小正值。

Because a float uses the sign-magnitude encoding, the lowest value a float can represent is -MAX_VALUE.

由于浮点数使用符号幅度编码,因此浮点数可以表示的最低值是-MAX_VALUE

回答by Philipp Cla?en

A possible explanation could be that Java just used the same naming convention as C++, which again inherited the names from C.

一个可能的解释可能是 Java 只是使用了与 C++ 相同的命名约定,它再次继承了 C 的名称。

Java was influenced by C++, which shares the same confusing naming pattern. In C++, the analogy of Float.MIN_VALUEis std::numeric_limits<T>::min(), which is defined as:

Java 受 C++ 的影响,它共享相同的令人困惑的命名模式。在 C++ 中,类比为Float.MIN_VALUEis std::numeric_limits<T>::min(),其定义为:

Minimum finite value. For floating types with denormalization (variable number of exponent bits): minimum positive normalized value.

最小有限值。对于具有非规范化(可变数量的指数位)的浮点类型:最小正规范化值。

In C++ that is a potential source of bugs in template code, so later in C++11, they added std::numeric_limits<T>::lowest(), which is defined as:

在 C++ 中,这是模板代码中潜在的错误来源,所以后来在 C++11 中,他们添加了std::numeric_limits<T>::lowest(),其定义为:

Minimum finite value. (since C++11) For integral types: the same as min(). For floating-point types: implementation-dependent; generally, the negative of max().

最小有限值。(C++11 起) 对于整型:与 min() 相同。对于浮点类型:依赖于实现;通常,max() 的负数。

But C++ was not the first language. It all goes back to C, which defines FLT_MINthe minimal floating point value.

但是 C++ 不是第一语言。这一切都可以追溯到 C,它定义了FLT_MIN最小浮点值。

So, why did C choose to define the minimums of floating point numbers and integers inconsistently?

那么,为什么 C 选择不一致地定义浮点数和整数的最小值?

Not sure, but it could have to do with symmetry (see this answer). For floats, you can use -FLT_MAX(or -Float.MAX_VALUE). For integers, negating the maximum value is not portable. In fact, it is generally wrong on all modern architectures (where -INT_MAX == INT_MIN + 1should hold).

不确定,但它可能与对称性有关(请参阅此答案)。对于浮点数,您可以使用-FLT_MAX(或-Float.MAX_VALUE)。对于整数,否定最大值是不可移植的。事实上,在所有现代架构上(-INT_MAX == INT_MIN + 1应该在哪里),它通常都是错误的。