What is this used for?

Hey guys. I have a code question. What the heck is math.min? Like, what is it used for? I know printing doesn’t always explain what certain things do, as I tried using print(math.min(1, 2). It only printed out one. I can’t find anything that would make math.min come in handy, as well as math.max. They seem to serve no purpose, even though I know that they do, I just don’t know what. How can I use either of these things?

2 Likes

min returns the lowest of two numbers

I understand that, but I’m not sure how to use it in code. I don’t see any points of math.min being useful, and I have the same viewpoint of math.max.

A minimax algorithm for instance.

You’ll know it when you see it.

Making sure a number is within a certain range…

value = math.max(0, math.min(value, 100))

has to be between 0 and 100. if higher or lower then this range, it will snap to the highest/lowest of the allowed range.

So, let me ask then. If I were to use this code below…

local Object = script.Parent
while Object.Transparency ~= math.min(0, 100) do
    Object.Transparency += 1
    task.wait(1)
end

What would happen?

this will always compare with 0…

You’d increase the object.Transparency forever as math.min selects 0, transparency will always not be equal to 0 unless it started as 0.

There’s no point in making up random places you can use this. It’s mainly useful in places where you need to quickly select for either a bigger or smaller number, or have the number remain within a bound.

1 Like

Okay sure. Could you give me an example of what it could be used for then? Or someone? Like, code I mean.

It returns the smallest number of its arguments:

print(math.min(1, 5, 2, -3, 400)) -- -3

The opposite of math.min is math.max which returns the highest number.

math.min can be used in situation where you need to return the smallest value with the given arguments.

Let me provide an example for you. Lets say a character has 75 remaining health.
He takes a whole lotta damage which exceeds his current health. You may not want negative numbers to appear in your gui, so you only set the maximum of the values, in this case, 0.

local characterHealth = 75
local damageAmount = 90

-- Reduce character's health by damage amount, but ensure it doesn't go below zero
characterHealth = math.max(0, characterHealth - damageAmount)
1 Like

He gave you an example, and I mentioned a minimax algorithm, both are perfectly normal places to use this. You can go google a minimax algorithm and study up on it.

For the sake of confusing you even more, there’s math.huge too

math.huge is just infinite is it not?

it just represents a “super large number”, doesn’t necessarily mean its infinite.

Ehh, not quite infinite. That wouldn’t make sense considering how numbers work on a computer

Oh. Whenever I use it with numbers, it always says “inf”.

Thats because it exceeds the maximum value of a number on roblox.

1 Like

Okay, so I just tested some things, and I now get the use of math.max. This could actually be super helpful. I still don’t get math.min though. I know it’s technically the opposite of math.max, but I have to do more testing with it. Sorry, I’m dumb when it comes to math.