Skip to content

feat: add C implementation for math/base/special/minmax #6296

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 9 commits into
base: develop
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
101 changes: 101 additions & 0 deletions lib/node_modules/@stdlib/math/base/special/minmax/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,107 @@ for ( i = 0; i < 100; i++ ) {

<!-- /.examples -->

<!-- C interface documentation. -->

* * *

<section class="c">

## C APIs

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- C usage documentation. -->

<section class="usage">

### Usage

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

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

Returns the minimum and maximum value.

```c
#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.

```c
void stdlib_base_minmax( const double x, const double y, double* min, double* max );
```

</section>

<!-- /.usage -->

<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

</section>

<!-- /.notes -->

<!-- C API usage examples. -->

<section class="examples">

### Examples

```c
#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 );
}
}
```

</section>

<!-- /.examples -->

</section>

<!-- /.c -->

<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="references">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
// MODULES //

var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var isArray = require( '@stdlib/assert/is-array' );
var min = require( '@stdlib/math/base/special/min' );
var max = require( '@stdlib/math/base/special/max' );
Expand All @@ -37,10 +37,11 @@ bench( pkg, function benchmark( b ) {
var z;
var i;

x = discreteUniform( 100, -500, 500 );
y = discreteUniform( 100, -500, 500 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*1000.0 ) - 500.0;
y = ( randu()*1000.0 ) - 500.0;
z = minmax( x, y );
if ( z.length !== 2 ) {
b.fail( 'should have expected length' );
Expand All @@ -63,10 +64,11 @@ bench( pkg+':assign', function benchmark( b ) {

out = [ 0.0, 0.0 ];

x = discreteUniform( 100, -500, 500 );
y = discreteUniform( 100, -500, 500 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*1000.0 ) - 500.0;
y = ( randu()*1000.0 ) - 500.0;
z = minmax.assign( x, y, out, 1, 0 );
if ( z.length !== 2 ) {
b.fail( 'should have expected length' );
Expand All @@ -86,10 +88,11 @@ bench( pkg+'::min,max', function benchmark( b ) {
var z;
var i;

x = discreteUniform( 100, -500, 500 );
y = discreteUniform( 100, -500, 500 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*1000.0 ) - 500.0;
y = ( randu()*1000.0 ) - 500.0;
z = [ min( x, y ), max( x, y ) ];
if ( z.length !== 2 ) {
b.fail( 'should have expected length' );
Expand All @@ -111,10 +114,11 @@ bench( pkg+'::min,max,memory_reuse', function benchmark( b ) {

z = [ 0.0, 0.0 ];

x = discreteUniform( 100, -500, 500 );
y = discreteUniform( 100, -500, 500 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*1000.0 ) - 500.0;
y = ( randu()*1000.0 ) - 500.0;
z[ 0 ] = min( x, y );
z[ 1 ] = max( x, y );
if ( z.length !== 2 ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var resolve = require( 'path' ).resolve;
var bench = require( '@stdlib/bench' );
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var isArray = require( '@stdlib/assert/is-array' );
var min = require( '@stdlib/math/base/special/min' );
var max = require( '@stdlib/math/base/special/max' );
var tryRequire = require( '@stdlib/utils/try-require' );
var pkg = require( './../package.json' ).name;


// VARIABLES //

var minmax = tryRequire( resolve( __dirname, './../lib/native.js' ) );
var opts = {
'skip': ( minmax instanceof Error )
};


// MAIN //

bench( pkg+'::native', opts, function benchmark( b ) {
var x;
var y;
var z;
var i;

x = discreteUniform( 100, -500, 500 );
y = discreteUniform( 100, -500, 500 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
z = minmax( x, y );
if ( z.length !== 2 ) {
b.fail( 'should have expected length' );
}
}
b.toc();
if ( !isArray( z ) ) {
b.fail( 'should return an array' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+'::min,max', opts, function benchmark( b ) {
var x;
var y;
var z;
var i;

x = discreteUniform( 100, -500, 500 );
y = discreteUniform( 100, -500, 500 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
z = [ min( x, y ), max( x, y ) ];
if ( z.length !== 2 ) {
b.fail( 'should have expected length' );
}
}
b.toc();
if ( !isArray( z ) ) {
b.fail( 'should return an array' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+'::min,max,memory_reuse', opts, function benchmark( b ) {
var x;
var y;
var z;
var i;

z = [ 0.0, 0.0 ];

x = discreteUniform( 100, -500, 500 );
y = discreteUniform( 100, -500, 500 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
z[ 0 ] = min( x, y );
z[ 1 ] = max( x, y );
if ( z.length !== 2 ) {
b.fail( 'should have expected length' );
}
}
b.toc();
if ( !isArray( z ) ) {
b.fail( 'should return an array' );
}
b.pass( 'benchmark finished' );
b.end();
});
Loading
Loading