Sorting array of doubles [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
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?

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.

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.

Related

Algorithm for finding a single number to represent two different numerical scales? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm looking for an algorithm to combine two values;
Where the first value indicates a positive result when the value is higher. For example, 0.98 is 'good' and 0.15 is 'bad'.
Where the second value indicates a positive result when the value is lower. For example, 10,000 is 'bad', whereas 1000 is 'good'.
I need a method of determining a value that can represent both of these scales with one number, so that I can sort my findings on my application from high to low accordingly. I'm not sure if anyone knows of such an algorithm, or any advice, but any help is greatly appreciated. Thank you.
P.S. I am aware I can 'negate' one of the two values, to have them appear on a similar scale, however I'm not sure how this would work in Java.
EDIT: Sorry, so to elaborate, I'm sorting images based on similarity to a user input image. Each of my algorithms that I'm using to return a value of similarity, function on a different scale. The first being a value between 0.00 and 1.00, with numbers being closer to 1.00, indicating the image is more similar to the original. Whereas, my second algorithm returns values from 1000+, with higher values indicating the image is less similar to the original. I need to take these two values and combine them to allow me to sort the resulting images in order of similarity, with the most similar image being shown at the top of my list, and the least similar at the bottom. Hopefully this helps clear up any confusion. Thanks again.
If your only goal is sorting, you need to come up with a function g(x,y) that represents the "goodness" of your pair of values. A pair (x1,y1) is better than (x2,y2) if and only if g(x1,y1) > g(x2,y2).
The function must represent what you consider "good". A simple example would be:
g(x,y) = x - y / 10000

Float comparision [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
in C language
float a=0.1;
if(a==.1)
printf("hello");
else
printf("123");
Output is 123
But for Java
float a=0.1F;
if(a==.1)
System.out.println("hello");
else
System.out.println("123");
Ans is hello.
Why?
Here comparison by == first converts float to double and than compares both value.
float real = 0.1f;
double real2 = real;
System.out.println(real2);
OUTPUT
0.10000000149011612
Now you can see when you convert float to double for value 0.1 you will not get the exact value here. Here system will convert your float value to double with extra precision in data.
Same thing happens here when you write if(a==.1) your a will be converted to something like 0.10000000149011612 and than compares with 0.1 which is already double and has exact value 0.1 and so it result to false and must print 123 and not hello that I am sure about.
You're comparing apples and oranges. In the Java case, you should be using
if (a == 0.1F)

Could someone help me understand what this is asking [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
To help Beta Rabbit crack the lock, write a function answer(n) which returns the smallest positive integer base b, at least 2, in which the integer n is a palindrome. The input n will satisfy "0 <= n <= 1000."
Test cases
Inputs:
(int) n = 0
Output:
(int) 2
Inputs:
(int) n = 42
Output:
(int) 4
it is not the problem I need help with as there are similar ones on this site. It is the actual question. this input n they talk about, what the hell does it stand for? It can't be the base as b represents the base. It is not the positive integer they want for output because it is independent of that number and in the test cases it doesn't seem to have any correlation with it. Does it just want me to find the smallest palindrome of any base that's less than 1000 in decimal? Thanks to anyone that takes the time to help me figure this out , it's part of Google's foobar questions. I don't mind doing the work to solve the flecking thing as long as I can understand what the thing is asking.
What is Google Foobar?
I may not give you a full answer, but think about it this way. The number n, convert it to the output base... see if it's a palindrome

Why does math.sqrt return a float to one decimal point and not just the value wo the decimal point? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm not sure if this question has been answered(couldn't find it when I did a google search).
I saw http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html that the math class square root method returns a double. I experimented with it in eclipse with some ints that have whole square roots - 4, 9 and found that the square root with those always returned a floating point value with one decimal - 4.0, 9.0. I was curious as to why it even returned that extra decimal? I thought that ints could be considered as double values too. To me returning just 1 makes more sense cause you conserve more memory(i guess more memory is needed to store that extra decimal point) I even tested it out
public static double control(){
return 1;
}
and saw it was valid to just return 1.
[I] found that [Math.sqrt(x) where x is a perfect square] always returned a floating point value with one decimal.
You are mistaking a particular printed representation of a double value with the value itself. A double does not have a decimal point. A double is a bit pattern that represents a particular real number (4 for example).
Decimal points only appear in a particuular decimal representation of real numbers. If I write "0.25", that obviously has a decimal point. If I write "1/4", there is no decimal point. But those are just two different representations of the same real number. So is the particular bit pattern that represents the double value returned by the Java expression, 1.0/4.0.
I don't know why Double.toString(4) returns the string, "4.0" instead of returning "4", but I'm guessing that somebody wanted to make it consistent with numeric literals in the Java language. When a "4" appears in your program, that's an int literal, and when "4.0" appears in your program, that's a double literal.
That method returns a double. It's going to display as a double because of the return type the method is set to. There is a solution to this here on stackoverflow to return the in if it's say 4.0 and show the double if it isn't. Solution on stackoverflow

How to get integer of a value? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
How to round up a decimal number to a whole number?
So for example when it is 1.25 rounds to 1 or -3.25 rounds to -3 etc
if you want decimal part do this .
double value = 1.25;
int i = (int)value;
if you want to round value , do this
Math.round(value);
Use Math.round(float number) method http://docs.oracle.com/javase/6/docs/api/java/lang/Math.html#round(float) if you want to round it. If you want to cut decimal part just cast it to int.
A small trick I've learned during the years is just adding the value 0.5 to the value you want to round. After you did that, you can round it to an integer.
It's a generic workaround, which will work with alot of programming languages.
I dont know much about Java, but I think you will find a round-Method in the API. The package should be Math.
Edit:\
To round UP you could just add 1 instead of the value 0.5.

Categories

Resources