min value of float in java is positive why? [dupli

2020-03-08 08:11发布

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
4条回答
聊天终结者
2楼-- · 2020-03-08 08:27

min value of float in java is positive why?

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 and Double.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.

查看更多
何必那么认真
3楼-- · 2020-03-08 08:41

MIN_VALUE should be named EPSILON 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.

查看更多
可以哭但决不认输i
4楼-- · 2020-03-08 08:45

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 is std::numeric_limits<T>::min(), which is defined as:

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:

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

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).

查看更多
\"骚年 ilove
5楼-- · 2020-03-08 08:50

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

查看更多
登录 后发表回答