Forum Moderators: coopster

Message Too Old, No Replies

Comparing 3 numeric values

         

grandpa

3:53 pm on Feb 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I need to evaluate 3 numbers and determine which is the greatest. However, at any time all three may be equal, or two the same, but different from the third. So what I need is the largest available number.

I started with this

if ($a_rows > $b_rows AND $a_rows >= $c_rows) { $max_rows = $a_rows; }

if ($b_rows > $a_rows AND $b_rows > $c_rows) { $max_rows = $b_rows; }

if ($c_rows > $a_rows AND $c_rows >= $b_rows) { $max_rows = $c_rows; }

Needless to say, $max_rows was NULL once $a_rows and $c_row were of equal value and $b_rows was less than both. So I modified my conditions, making them >=,
if ($a_rows >= $b_rows)

That helped. But I'm looking at this and wondering..
There must be a more efficient way to compare the 3 numbers and determine the value of $max_rows.

jatar_k

5:41 pm on Feb 19, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



well, the first thing that came to mind is as they are collected to put them into an array and sort them into descending order and then your answer would always be $array[0]. That would depend on whether you need to use them again. If you did you could use proper indexes to retain them.

The other thing is as you go you could just compare whether they are larger than the one before, if they are equal then you can keep either as they are the same.

grandpa

7:35 pm on Feb 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The way I'm sticking everything into arrays lately, I'm surprised I didn't come up with that option. I don't need the value again - it's used later on in the script to set an upper limit on row processing.

Thanks

jatar_k

7:38 pm on Feb 19, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



if they aren't used again then I would also unset the array after you get the value.

hakre

6:31 pm on Feb 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



normally i would use the array, too (this came at first into my mind) but because only the highest value is needed, this can be written as a "standard" expression instead:

$max_rows = ($a_rows>$b_rows)?(($a_rows>$c_rows)?$a_rows: (($c_rows>$b_rows)?$c_rows:$b_rows))): (($b_rows>$c_rows)?$b_rows: (($c_rows>$a_rows)?$c_rows:$a_rows));

*gg*

[edited by: jatar_k at 10:33 pm (utc) on Feb. 20, 2005]
[edit reason] fixed sidescroll [/edit]

grandpa

1:32 am on Feb 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thank hakre,

That expression didn't exactly work, it needs some fine tuning. I'll work on it. Ternary operators are not my strong suit, but I suspect that would be more efficient way to code for these comparisons.

As I say, I'll work on it, but it's academic at this point. Stuffing an array and obtaining my upper limit was a snap.

ergophobe

11:19 pm on Feb 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Make it easy on yourself and super fast

$max_rows = max($a_rows, $b_rows, $c_rows);

If there's a chance that one of the variables will be a string, make sure to read the manual for caveats or you may get unexpected results

$max = ("Whoops, this is a string", 0, 0, 0);
echo $max;

outputs "Whoops, this is a string"

grandpa

7:20 am on Feb 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



mixed max ( number arg1, number arg2 [, number ...] ) [us4.php.net]

I really did look for it...
Thanks!

incrediBILL

8:09 am on Feb 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Most languages like c, c++, etc have a MAX() function and you do something simple like....

e=10, f=4, g=20;

a = max( e, f);
b = max( g, a);

b=20 should be the final result

Check your language, I'm sure they have this simple function that's been in programming libraries since the dawn of time.

incrediBILL

8:10 am on Feb 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



OOPS! I just read the other post about mixed max....

Guess it's bedtime.