You probably don't know what's the meaning of that E-4
at the end.
9.914204103773398E-4
Is actually
9.914204103773398 * 10^-4
Which is smaller than 0.002385171991295645
.
For more details, visit the JLS - 3.10.2. Floating-Point Literals.
Pregunta
I've array of doubles and I'm sorting it using Arrays.sort
problem is that in the output array I've something like this(this is array output after reverse):
0.002385171991295645
...
9.914204103773398E-4
...
1.00139601969068E-4
9.975711760353395E-5
and so on, as we can see shortest number are at the top and longest are at the bottom. Numbers in given range e.g. E-4 are sorted good. I've even created a sample code to test this:
double l1 = 0.002385171991295645;
double l2 = 9.914204103773398E-4;
if(l1 > l2) {
System.out.println("TRUE");
} else {
System.out.println("FALSE");
}
And it gives me "TRUE", how to sort this array?
Solución
You probably don't know what's the meaning of that E-4
at the end.
9.914204103773398E-4
Is actually
9.914204103773398 * 10^-4
Which is smaller than 0.002385171991295645
.
For more details, visit the JLS - 3.10.2. Floating-Point Literals.
Otros consejos
You are getting confused by the scientific notation (also known as standard form).
9.914204103773398E-4
means 9.91... x 10^(-4)
, i.e. 0.0009914...
. So this value is indeed smaller than 0.0023..
.
The "e" or "E" means "exponent," which denotes scientific notation.
Here's a useful example from the primitive data types tutorial on Oracle.com:
double d1 = 123.4;
double d2 = 1.234e2;
Those are both the same number. The "e" means, basically, 10^x
(10
raised to the power of x
) where the number that follows is x
. So in the above, 1.234 * 10^2
. 10^2
is 100
, so 1.234 * 100
is 1.234
.
So looking at one of your numbers, 9.914204103773398E-4
, that's 9.914204103773398 * 10^-4
. 10^-4
is 0.0001
, so 9.914204103773398 * 0.0001
, which is 0.0009914103773398
.