This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Goof's profile picture

Find Biggest Value In 5 Different Variables

Started by Goof, 02 November 2013 - 07:03 AM
Goof #1
Posted 02 November 2013 - 08:03 AM
hello..

I just wanted to find the biggest number in 5 different variables like this:

m = 1
t = 7
s = 3
y = 22


then the script has to choose the y, because its the biggest number…

I totally forgot how to do this thing, therefore i ask here…



Thanks in advance
sens #2
Posted 02 November 2013 - 08:06 AM
math.max(m,t,s,y)
LBPHacker #3
Posted 02 November 2013 - 08:06 AM
Man…
print(math.max(m, t, s, y))
EDIT: I hate when the forum can't keep up with the ninjas :D/>
sens #4
Posted 02 November 2013 - 08:08 AM
Pipped at the post! :ph34r:/>
Goof #5
Posted 02 November 2013 - 08:16 AM
Okay.. Thanks :)/>
Goof #6
Posted 06 November 2013 - 09:52 AM
Just have to ask a bit different( yes the previous posts worked correct and thats good), but:

Is it possible to see the math.max code in the math api( which doesnt exist) ?


Thanks
theoriginalbit #7
Posted 06 November 2013 - 10:47 AM
Is it possible to see the math.max code in the math api( which doesnt exist) ?
But it does exist…. but this is how it'd be done

function maxNumber(...)
  --# get all the args
  local args = {...}
  -- make sure there are some args
  if #args == 0 then
    error("Missing arguments", 2)
  end
  -- now to the logic
  local max = args[1] -- we set it to the first element in the array instead of a number such as 0, just incase all the arguments are negative
  for i,v in pairs(args) do
    -- just some validation, making sure its a number
    if type(v) ~= "number" then
      error("Arg #"..i.." is not a number", 2)
    end
    -- is it the biggest?
    if v > max then
      -- yes it was
      max = v
    end
  end
  return max
end