aspose file tools*
The moose likes Beginning Java and the fly likes compare three double numbers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "compare three double numbers" Watch "compare three double numbers" New topic
Author

compare three double numbers

raghuveer mandal
Greenhorn

Joined: Nov 13, 2004
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

Joined: Apr 04, 2004
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 &&.


I've heard it takes forever to grow a woman from the ground
Mark Patrick
Ranch Hand

Joined: Feb 22, 2004
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.


Mark Patrick<br />SCJP 1.4
raghuveer mandal
Greenhorn

Joined: Nov 13, 2004
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

Joined: Nov 13, 2004
Posts: 9
Thanks a lot Mark.
Nick George
Ranch Hand

Joined: Apr 04, 2004
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

Joined: Feb 22, 2004
Posts: 51
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

Joined: Feb 22, 2004
Posts: 51
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

Joined: Apr 04, 2004
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

Joined: Mar 13, 2004
Posts: 1272
How about this:


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


Mike Gershman
SCJP 1.4, SCWCD in process
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Patrick Haley
Greenhorn

Joined: Sep 15, 2004
Posts: 10
I would do this:


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

Wannabe SCJP 1.4<br /> <br />It wasn't raining when Noah built the ark.<br /> --Howard Ruff
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: compare three double numbers