Skip to content

Latest commit

 

History

History
263 lines (162 loc) · 6.13 KB

File metadata and controls

263 lines (162 loc) · 6.13 KB

minmax

Return the minimum and maximum values.

Usage

var minmax = require( '@stdlib/math/base/special/minmax' );

minmax( x, y )

Returns the minimum and maximum values in a single pass.

var v = minmax( 4.2, 3.14 );
// returns [ 3.14, 4.2 ]

v = minmax( +0.0, -0.0 );
// returns [ -0.0, +0.0 ]

If any argument is NaN, the function returns NaN for both the minimum value and the maximum value.

var v = minmax( 4.2, NaN );
// returns [ NaN, NaN ]

v = minmax( NaN, 3.14 );
// returns [ NaN, NaN ]

minmax.assign( x, y, out, stride, offset )

Returns the minimum and maximum values in a single pass and assigns results to a provided output array.

var Float64Array = require( '@stdlib/array/float64' );

var out = new Float64Array( 2 );

var v = minmax.assign( 5.0, -2.0, out, 1, 0 );
// returns <Float64Array>[ -2.0, 5.0 ]

var bool = ( v === out );
// returns true

Examples

var minstd = require( '@stdlib/random/base/minstd-shuffle' );
var minmax = require( '@stdlib/math/base/special/minmax' );

var x;
var y;
var v;
var i;

for ( i = 0; i < 100; i++ ) {
    x = minstd();
    y = minstd();
    v = minmax( x, y );
    console.log( 'minmax(%d,%d) = [%d, %d]', x, y, v[0], v[1] );
}

C APIs

Usage

#include "stdlib/math/base/special/minmax.h"

stdlib_base_minmax( x, y, &min, &max )

Returns the minimum and maximum value.

#include <stdint.h>

double min;
double max;

stdlib_base_minmax( 3.14, NaN );
// returns [ NaN, NaN ]

The function accepts the following arguments:

  • x: [in] double input value.
  • y: [in] double input value.
  • min: [out] double destination for minimum value.
  • max: [out] double destination for maximum value.
void stdlib_base_minmax( const double x, const double y, double* min, double* max );

Examples

#include "stdlib/math/base/special/minmax.h"
#include <stdlib.h>
#include <stdio.h>

int main( void ) {
    double min;
    double max;
    double x;
    double y;
    int i;

    const double x1[] = { 1.0, 0.45, -0.89, 0.0 / 0.0, -0.78, -0.22, 0.66, 0.11, -0.55, 0.0 };
    const double x2[] = { -0.22, 0.66, 0.0, -0.55, 0.33, 1.0, 0.0 / 0.0, 0.11, 0.45, -0.78 };

    for ( i = 0; i < 12; i++ ) {
        x = ( ( (double)rand() / (double)RAND_MAX ) * 200.0 ) - 100.0;
        y = ( ( (double)rand() / (double)RAND_MAX ) * 200.0 ) - 100.0;
        stdlib_base_minmax( x[ i ], y[ i ], &min, &max );
        printf( "x: %lf => min: %lf, y: %lf, minmax(x, y): %lf\n", x[ i ], y[ i ]min, max );
    }
}

See Also