clampf
Restrict a single-precision floating-point number to a specified range.
Usage
var clampf = require( '@stdlib/math/base/special/clampf' );
clampf( v, min, max )
Restricts a single-precision floating-point number to a specified range.
var v = clampf( 3.0, 0.0, 5.0 );
// returns 3.0
v = clampf( -3.0, 0.0, 5.0 );
// returns 0.0
v = clampf( 10.0, 0.0, 5.0 );
// returns 5.0
v = clampf( -0.0, 0.0, 5.0 );
// returns 0.0
v = clampf( 0.0, -3.0, -0.0 );
// returns -0.0
If provided NaN
for any argument, the function returns NaN
.
var v = clampf( NaN, 0.0, 5.0 );
// returns NaN
v = clampf( 0.0, NaN, 5.0 );
// returns NaN
v = clampf( 3.14, 0.0, NaN );
// returns NaN
Examples
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
var clampf = require( '@stdlib/math/base/special/clampf' );
var min;
var max;
var v;
var i;
for ( i = 0; i < 100; i++ ) {
min = discreteUniform( 0.0, 10.0 );
max = discreteUniform( 5.0, 15.0 );
v = discreteUniform( -20.0, 20.0 );
console.log( 'clampf(%d,%d,%d) => %d', v, min, max, clampf( v, min, max ) );
}
C APIs
Usage
#include "stdlib/math/base/special/clampf.h"
stdlib_base_clampf( v, min, max )
Restricts a single-precision floating-point number to a specified range.
float y = stdlib_base_clampf( -3.14f, 0.0f, 5.0f );
// returns 0.0f
The function accepts the following arguments:
- v:
[in] float
input value. - min:
[in] float
minimum value. - max:
[in] float
maximum value.
float stdlib_base_clampf( const float v, const float min, const float max );
Examples
#include "stdlib/math/base/special/clampf.h"
#include <stdio.h>
int main( void ) {
const float x[] = { 3.14f, -3.14f, 0.0f, 0.0f/0.0f };
float y;
int i;
for ( i = 0; i < 4; i++ ) {
y = stdlib_base_clampf( x[ i ], -3.0f, 3.0f );
printf( "clamp(%f, %f, %f) = %f\n", x[ i ], -3.0f, 3.0f, y );
}
}