Jump to content




How to compare three values?


  • You cannot reply to this topic
8 replies to this topic

#1 Lettuce

  • Members
  • 210 posts
  • LocationIn your fridge.

Posted 14 September 2012 - 01:06 AM

I'm surprised no one asked this before. How do I compare three values at the same time? In this case, to find the greatest number. Can this be shortened:
Spoiler

I pulled that out of my hat, so there may be errors, but you see what I'm trying to do. That will get ridiculously lengthy very quickly. And I was only trying to compare three numbers! Imagine if I had 20.

I've never been taught any programming language, save for HTML (and I know it doesn't count), so I learned most of what I know here at the forums/wiki. That's why I'm asking what is almost certainly a "noob" question.

--Lettuce

#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 14 September 2012 - 01:15 AM

Put all your numbers in a table.

local biggest = table[1]
for i=2, #table do
  if table[i] > biggest then
    biggest = table[i]
  end
end


#3 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 14 September 2012 - 01:16 AM

Well, to get the maximum/minimum you can use the math library/api:
local max = math.max(x1, x2, x3, x4, ...)
local min = math.min(x1, x2, x3, x4, ...)

Edit:
If you have all the values in a table, you can simply use:
local tbl = { x1, x2, x3, ... }
local max = math.max(unpack(tbl))


#4 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 14 September 2012 - 01:19 AM

Oh, durr. Don't know how I forgot about the min and max functions. I'd bet that math.max looks similar to that snippet I wrote above, though.

#5 Lettuce

  • Members
  • 210 posts
  • LocationIn your fridge.

Posted 14 September 2012 - 01:20 AM

Okay. I kinda figured there would be an API. What's that unpack() though? I never used that, and it looks extremely useful. Could one compare two tables with that directly?
--Lettuce

#6 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 14 September 2012 - 01:32 AM

Unpack expands the values in a table into separate returns, so in this case, it causes all of the values of the table to become the arguments of math.max().

#7 Lettuce

  • Members
  • 210 posts
  • LocationIn your fridge.

Posted 14 September 2012 - 01:38 AM

Interesting. Well, in a roundabout fashion, in my real program, I "unpacked" the hard way, counting occurrences of values in a table, so that will make my job easier. Thanks a lot guys!

--Lettuce

#8 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 14 September 2012 - 01:52 AM

View PostLyqyd, on 14 September 2012 - 01:19 AM, said:

Oh, durr. Don't know how I forgot about the min and max functions. I'd bet that math.max looks similar to that snippet I wrote above, though.
Yep, pretty much:
double m = args.checkdouble(1);
for ( int i=2,n=args.narg(); i<=n; ++i )
    m = Math.max(m,args.checkdouble(i));
return valueOf(m);
(It's the java code from LuaJ math library).
:)/>

#9 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 14 September 2012 - 02:02 AM

View PostMysticT, on 14 September 2012 - 01:52 AM, said:

View PostLyqyd, on 14 September 2012 - 01:19 AM, said:

Oh, durr. Don't know how I forgot about the min and max functions. I'd bet that math.max looks similar to that snippet I wrote above, though.
Yep, pretty much:
double m = args.checkdouble(1);
for ( int i=2,n=args.narg(); i<=n; ++i )
	m = Math.max(m,args.checkdouble(i));
return valueOf(m);
(It's the java code from LuaJ math library).
:)/>

Haha, yeah, that's exactly it, for loop setup and all. That's a little nuts.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users