Skip to content

[REQUEST] Clamp function #464

@Darxo

Description

@Darxo

Is your feature request related to a problem? Please describe.

The common math function "Clamp" is missing in the squirrel ::Math table.

Currently we need to implement clamping by writing something like this:
return ::Math.max(0, ::Math.min(100, _foo));

Right now, especially after you've worked with such lines a bunch of times, you will easily identify it.
But as the line grows larger, because the arguments come from different places, it becomes harder to parse at a glance.

Describe the solution you'd like

Add a new ::MSU.Math.Clamp function.

It could look something like this:

// Clamps an integer or float _value to be within [_min, _max] range
// throw InvalidValue exception when _min is greater than _max
::MSU.Math.clamp <- function( _value, _min, _max )
{
	if (_min > _max)
	{
		::logError("_min must be less than or equal to _max");
		throw ::MSU.Exception.InvalidValue(_min);
	}
	return _value < _min ? _min : (_value > _max ? _max : _value);
}

Additional context

Here is a now closed PR with some thoughts from the MSU guys about this: #461

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestA feature requested by the community

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions