Skip to content

[RFC] Add clamp function #19434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions ext/standard/basic_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -1629,6 +1629,12 @@ function min(mixed $value, mixed ...$values): mixed {}
*/
function max(mixed $value, mixed ...$values): mixed {}

/**
* @compile-time-eval
* @frameless-function {"arity": 3}
*/
function clamp(mixed $value, mixed $min, mixed $max): mixed {}

function array_walk(array|object &$array, callable $callback, mixed $arg = UNKNOWN): true {}

function array_walk_recursive(array|object &$array, callable $callback, mixed $arg = UNKNOWN): true {}
Expand Down
16 changes: 15 additions & 1 deletion ext/standard/basic_functions_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 73 additions & 0 deletions ext/standard/math.c
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,79 @@ PHP_FUNCTION(round)
}
/* }}} */

/* {{{ Return the given value if in range of min and max */
PHP_FUNCTION(clamp)
{
zval *zvalue, *zmin, *zmax;

ZEND_PARSE_PARAMETERS_START(3, 3)
Z_PARAM_ZVAL(zvalue)
Z_PARAM_ZVAL(zmin)
Z_PARAM_ZVAL(zmax)
ZEND_PARSE_PARAMETERS_END();

if (EXPECTED(Z_TYPE_P(zmin) == IS_DOUBLE) && UNEXPECTED(zend_isnan(Z_DVAL_P(zmin)))) {
zend_argument_value_error(2, "cannot be NAN");
RETURN_THROWS();
}

if (EXPECTED(Z_TYPE_P(zmax) == IS_DOUBLE) && UNEXPECTED(zend_isnan(Z_DVAL_P(zmax)))) {
zend_argument_value_error(3, "cannot be NAN");
RETURN_THROWS();
}

if (zend_compare(zmin, zmax) > 0) {
zend_argument_value_error(2, "must be smaller than or equal to argument #3 ($max)");
RETURN_THROWS();
}

if (zend_compare(zmax, zvalue) == -1) {
RETURN_COPY(zmax);
}

if (zend_compare(zvalue, zmin) == -1) {
RETURN_COPY(zmin);
}

RETURN_COPY(zvalue);
}
/* }}} */

/* {{{ Return the given value if in range of min and max */
ZEND_FRAMELESS_FUNCTION(clamp, 3)
{
zval *zvalue, *zmin, *zmax;
Z_FLF_PARAM_ZVAL(1, zvalue);
Z_FLF_PARAM_ZVAL(2, zmin);
Z_FLF_PARAM_ZVAL(3, zmax);

if (EXPECTED(Z_TYPE_P(zmin) == IS_DOUBLE) && UNEXPECTED(zend_isnan(Z_DVAL_P(zmin)))) {
zend_argument_value_error(2, "cannot be NAN");
RETURN_THROWS();
}

if (EXPECTED(Z_TYPE_P(zmax) == IS_DOUBLE) && UNEXPECTED(zend_isnan(Z_DVAL_P(zmax)))) {
zend_argument_value_error(3, "cannot be NAN");
RETURN_THROWS();
}

if (zend_compare(zmin, zmax) > 0) {
zend_argument_value_error(2, "must be smaller than or equal to argument #3 ($max)");
RETURN_THROWS();
}

if (zend_compare(zmax, zvalue) == -1) {
RETURN_COPY(zmax);
}

if (zend_compare(zvalue, zmin) == -1) {
RETURN_COPY(zmin);
}

RETURN_COPY(zvalue);
}
/* }}} */

/* {{{ Returns the sine of the number in radians */
PHP_FUNCTION(sin)
{
Expand Down
61 changes: 61 additions & 0 deletions ext/standard/tests/math/clamp.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
--TEST--
clamp() tests
--INI--
precision=14
date.timezone = UTC
--FILE--
<?php

var_dump(clamp(2, 1, 3));
var_dump(clamp(0, 1, 3));
var_dump(clamp(6, 1, 3));
var_dump(clamp(2, 1.3, 3.4));
var_dump(clamp(2.5, 1, 3));
var_dump(clamp(2.5, 1.3, 3.4));
var_dump(clamp(0, 1.3, 3.4));
var_dump(clamp(M_PI, -INF, INF));
var_dump(clamp(NAN, 4, 6));
var_dump(clamp("a", "c", "g"));
var_dump(clamp("d", "c", "g"));
echo clamp('2025-08-01', '2025-08-15', '2025-09-15'), "\n";
echo clamp('2025-08-20', '2025-08-15', '2025-09-15'), "\n";
echo clamp(new \DateTimeImmutable('2025-08-01'), new \DateTimeImmutable('2025-08-15'), new \DateTimeImmutable('2025-09-15'))->format('Y-m-d'), "\n";
echo clamp(new \DateTimeImmutable('2025-08-20'), new \DateTimeImmutable('2025-08-15'), new \DateTimeImmutable('2025-09-15'))->format('Y-m-d'), "\n";

try {
var_dump(clamp(4, NAN, 6));
} catch (ValueError $error) {
echo $error->getMessage(), "\n";
}

try {
var_dump(clamp(7, 6, NAN));
} catch (ValueError $error) {
echo $error->getMessage(), "\n";
}

try {
var_dump(clamp(1, 3, 2));
} catch (ValueError $error) {
echo $error->getMessage(), "\n";
}
?>
--EXPECT--
int(2)
int(1)
int(3)
int(2)
float(2.5)
float(2.5)
float(1.3)
float(3.141592653589793)
float(NAN)
string(1) "c"
string(1) "d"
2025-08-15
2025-08-20
2025-08-15
2025-08-20
clamp(): Argument #2 ($min) cannot be NAN
clamp(): Argument #3 ($max) cannot be NAN
clamp(): Argument #2 ($min) must be smaller than or equal to argument #3 ($max)