Why is Double.MIN_VALUE in not negative
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 flipped, so yes, -Double.MAX_VALUE
is the lowest actual number you can represent with a 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).
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 absolute value 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
:-)
(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.)
Here is a good text on the subject.
Is Double.MIN_VALUE is greater than zero in Java?
According to the javadoc for Double.MIN_VALUE, MIN_VALUE is:
A constant holding the smallest
positive nonzero value of type double
So Double.MIN_VALUE is not negative, it's the positive value that's as close as a Double can get to zero (without being zero).
Why is -(Double.MAX_VALUE) smaller than Double.MIN_VALUE?
See https://docs.oracle.com/javase/8/docs/api/java/lang/Double.html
Double.MAX_VALUE
is the biggest finite number that double can hold. (A very large positive number.)
Double.MIN_VALUE
is the smallest positive number that double can hold. (A very small positive number.)
So -Double.MAX_VALUE
is a very large negative number. That's a lower number than a very small positive number.
Contrarily, Integer.MIN_VALUE
is the most negative number that an int can hold. It has a different meaning from the similarly named constant in Double.
Comparing 0 with Double min value
Okay, let's see what we get from Double.MIN_VALUE. When we say,
System.out.println(Double.MIN_VALUE);
It prints out that the minimum double value is 4.9E-324, which is POSITIVE and NONZERO.
In your code you compare it to 0. Even though how small 4.9E-324 is, it is still greater than 0.
If you are trying to find the smallest negative double that you can get, then you are looking for,
System.out.println(-Double.MIN_VALUE);
This will return -4.9E-324
, which is the smallest and negative number that you can get with Double
.
Why min value of Double is returned as 0.0
Refer to the JavaDoc for Double.MIN_VALUE
A constant holding the smallest positive nonzero value of type double,
2-1074. It is equal to the hexadecimal floating-point literal
0x0.0000000000001P-1022 and also equal to
Double.longBitsToDouble(0x1L).
0.0d
is thus smaller than Double.MIN_VALUE
Double.Min_Value not working as expected
Double.MIN_VALUE
holds a positive number, so 0 < Double.MIN_VALUE
.
/**
* 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>.
*/
public static final double MIN_VALUE = 0x0.0000000000001P-1022; // 4.9e-324
You should use some arbitrary negative value instead (or perhaps Double.NEGATIVE_INFINITY
).
Why the expression (double.MinValue double.MinValue + 1) returns false?
The two min values are different things.
In Java, MIN_VALUE
is the name for the smallest positive double
value. In C#, MinValue
is the name for the double value less than all other finite values; in Java, that value would be -Double.MAX_VALUE
.
And that value is so massively negative that adding 1
to it gets lost in the rounding. double
doesn't have enough precision to represent MinValue
differently from MinValue+1
. It's, roughly, -1800000....00 where there are 307 zeroes. double
can only represent, roughly, seventeen decimal digits of precision. So adding one gets lost in the rounding.
Java: Compare Double 0 with Min/Max
Look at the documentation of Double.MIN_VALUE
, which says:
A constant holding the smallest positive nonzero value of type double, 2-1074.
So this is a value larger than 0, which is why you 'false' if you check if it's smaller than zero.
Related Topics
How to Simulate Low Memory in the Android Emulator
Httpurlconnection.Getresponsecode() Returns -1 on Second Invocation
Android Gives Error "Cannot Fit Requested Classes in a Single Dex File"
Problems Importing Project into Android Studio Regarding Actionbarsherlock
Android Intent for Sending Email with Attachment
How to Resolve "Duplicate Files Copied in APK Meta-Inf/*"
Divide Elements on Groups in Recyclerview or Grouping Recyclerview Items ,Say by Date
Return Data from Asynctask Class
Proguard and Reflection in Android
Networksecurityconfig: No Network Security Config Specified -- Android 7.0 Error
How to Update Xml File from Another Xml File Dynamically
Issue: Passing Large Data to Second Activity
How to Parse or Split Url Address in Java
Android App Bundle Introduces Resource Not Found Crash in Android App