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.
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
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.
Besides, knowing the answer to the question is not going to help, because the values of
Float.MIN_VALUE
andDouble.MIN_VALUE
won'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.)I suppose, the answer (i.e. the real reason for the decision) might be 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.
MIN_VALUE
should be namedEPSILON
it's the smallest postive value a float can represent.Because a float uses the sign-magnitude encoding, the lowest value a float can represent is
-MAX_VALUE
.A possible explanation could be that Java just used the same naming convention as C++, which again inherited the names from C.
Java was influenced by C++, which shares the same confusing naming pattern. In C++, the analogy of
Float.MIN_VALUE
isstd::numeric_limits<T>::min()
, which is defined as: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:But C++ was not the first language. It all goes back to C, which defines FLT_MIN the minimal floating point value.
So, why did C choose to define the minimums of floating point numbers and integers inconsistently?
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 + 1
should hold).MIN_VALUE tells you how precise a float can get, but not the mathematical minimum it can represent. They should have named it better...
Negative of MAX_VALUE is the mathematical minimum value for floats(same goes for 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.
For more info http://people.uncw.edu/tompkinsj/133/numbers/Reals.htm