@stdlib/math-base-special-clampf

Restrict a single-precision floating-point number to a specified range.

Usage no npm install needed!

<script type="module">
  import stdlibMathBaseSpecialClampf from 'https://cdn.skypack.dev/@stdlib/math-base-special-clampf';
</script>

README

clampf

NPM version Build Status Coverage Status

Restrict a single-precision floating-point number to a specified range.

Installation

npm install @stdlib/math-base-special-clampf

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() {
    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 );
    }
}

Notice

This package is part of stdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.

For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.

Community

Chat


License

See LICENSE.

Copyright

Copyright © 2016-2022. The Stdlib Authors.