Compare to double values return Max

  • Thread starter Thread starter MFRASER
  • Start date Start date
M

MFRASER

How can I compare two double values and return the greater of the two
values?

example
double doublea = 1.0;
double doubleb = 2.0

double retVal = ?compare doublea and doubleb
 
MFRASER said:
How can I compare two double values and return the greater of the two
values?

Why not use old-fashioned "if"?
double doublea = 1.0;
double doubleb = 2.0

double retVal = ?compare doublea and doubleb

well, you /could/ do it like this:

\\\
double doublea = 1.0;
double doubleb = 2.0;

double retval = (doublea > doubleb) ? doublea : doubleb;
///

however, I *strongly* recommend use of the "if"-way even if it's more
code to write.
 
...
How can I compare two double values
and return the greater of the two
values?

example
double doublea = 1.0;
double doubleb = 2.0

double retVal = ?compare doublea and doubleb


double retVal = Math.Max(doublea, doubleb);

or

double retVal = (doublea > doubleb) ? doublea : doubleb;

or

double retVal = 0;
if (doublea > doubleb)
{
retVal = doublea;
}
else
{
retVal = doubleb;
}

// Bjorn A
 
Thanks for the response I ended up using the Math library.

double retVal = Math.Max(doublea, doubleb)

works very nice.
 
Back
Top