• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Mark.
 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about this:


Too messy? If you're using Java 5, you can do this:
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would do this:


[ November 14, 2004: Message edited by: Patrick Haley ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic