Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!

# compare three double numbers

raghuveer mandal
Greenhorn
Posts: 9
How can i compare four double valus and find if the one is greates.
i had tried this code but it fails
if(doubA<=doubB<=doubC<=doubD)
{
System.out.println("doubD is greatest");
}

Nick George
Ranch Hand
Posts: 815
You can't have compound inequalities, but even if you could, what you have is only true if they all go in that particular order, not just if doubD is the greatest.

The key is with &&.

Mark Patrick
Ranch Hand
Posts: 51
You can't do it in one statement. Compare one statement to another and store the greatest value in a temp variable. Then compare the temp variable to the third variable and store the greate value in the temp variable...etc, until you've gone through all of the values.

Math.max() is a useful method for this comparison.

raghuveer mandal
Greenhorn
Posts: 9
Thanks Nick, if you can help me out can you please tell me is there any package or any class which has some method where i pass four or more double variables and check weather the required number is greatest among all?

raghuveer mandal
Greenhorn
Posts: 9
Thanks a lot Mark.

Nick George
Ranch Hand
Posts: 815
Math.max(...) seems to me to be a bit of a round-about way... you still have to and together a number of individual checks.

What you want is just
if( a>b && a>c && a>d){
...
}

Mark Patrick
Ranch Hand
Posts: 51
Nick,

I know you have to do individual checks. Something like:

How many different combinations of
would you need to handle this situation and how much harder would the code be to read. Your code only finds if 'a' is the greatest. You also couldn't easily put that kind of logic in a loop to find the largest number in a big array, but it could easily be done use Math.max().

Mark Patrick
Ranch Hand
Posts: 51
Nick,

I know you have to do individual checks. Something like:

How many different combinations of
would you need to handle this situation and how much harder would the code be to read. Your code only finds if 'a' is the greatest. You also couldn't easily put that kind of logic in a loop to find the largest number in a big array, but it could easily be done use Math.max().

Nick George
Ranch Hand
Posts: 815
My understanding is that we want to be able to print:

System.out.println("doubD is greatest");

You're method, it seems to me, would then have to be shot through either some more if's, or a switch. It's certainly better if we just want the biggest number.
[ November 13, 2004: Message edited by: Nick George ]

Mike Gershman
Ranch Hand
Posts: 1272

Too messy? If you're using Java 5, you can do this:

Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
Don't max & min seem like natural methods for optional parameters? The REXX language lets you give any number of parameters. If you're in JDK 5 see if you can make MyMath.max() with optional parameters.

Patrick Haley
Greenhorn
Posts: 10
I would do this:

[ November 14, 2004: Message edited by: Patrick Haley ]