Why max() and min() only accepts 2 arguments?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By mateusak
:warning: Old Version Published before Godot 3 was released.

I mean I just can’t get it. What’s up with this brilliant idea?

float max( float a, float b )
Return the maximum of two values.

What were you expecting?

duke_meister | 2016-05-12 04:15

:bust_in_silhouette: Reply From: eaglecat

It’s not weird. Java and C# also use 2 arguments
you can write your own code that may use array as argument

:bust_in_silhouette: Reply From: Gokudomatic2

Working with two floats is way faster than working with an array of objects. It’s all about performance. But you can write an additional max and min function that accept arrays of untyped values if you want.

:bust_in_silhouette: Reply From: GlaDOSik

Let’s say you want to clamp a value v to max constant 20. You could do it with code

if (v > 20):
  v = 20

But this is shorter and looks better in code:

v = max(v, 20.0)

You can use min max to clamp a value to the range (Godot has a clamp function, but still…)

v = max(0.0, min(20.0, v))

You can nest them, if you want to use more values

v = max(v1, max(v2, max(v3, v4)))

I personally use them a lot in my current Java project.

Am i wrong or is

if (v > 20):
  v = 20

as max(v,20) the opposite of what you want?
because if v was bigger than 20, the max() function would return the bigger value (v)? so you would get the desired functionality with the min() function rather than the max()

rcv4 | 2022-09-24 16:14

:bust_in_silhouette: Reply From: Tottel

Like the people before me have said, it’s a totally standard way of working.

If you only have a couple of variables to compare, you can nest the max() function calls, like Gladosik said.

If you have more variables, consider making them a list; sort them; and retrieve the first value to get the highest number.