programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# compare three double numbers

Greenhorn
Posts: 9
• Number of slices to send:
Optional 'thank-you' note:
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");
}

Ranch Hand
Posts: 815
• Number of slices to send:
Optional 'thank-you' note:
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 &&.

Ranch Hand
Posts: 51
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
Thanks a lot Mark.

Nick George
Ranch Hand
Posts: 815
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
Nick,

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

Doesn't seem that round-about.

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
• Number of slices to send:
Optional 'thank-you' note:
Nick,

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

Doesn't seem that round-about.

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
• Number of slices to send:
Optional 'thank-you' note:
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 ]

Ranch Hand
Posts: 1272
• Number of slices to send:
Optional 'thank-you' note:

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

(instanceof Sidekick)
Posts: 8791
• Number of slices to send:
Optional 'thank-you' note:
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.

Greenhorn
Posts: 10
• Number of slices to send:
Optional 'thank-you' note:
I would do this:

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

 Don't get me started about those stupid light bulbs.