Skip to content

feat: add math/base/special/minmaxf #3390

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

Closed
wants to merge 9 commits into from
Closed
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
256 changes: 256 additions & 0 deletions lib/node_modules/@stdlib/math/base/special/minmaxf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
<!--

@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.

-->

# minmaxf

> Return the minimum and maximum values for single-precision floating-point number.

<!-- 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 -->

<!-- Package usage documentation. -->

<section class="usage">

## Usage

```javascript
var minmaxf = require( '@stdlib/math/base/special/minmaxf' );
```

#### minmaxf( x, y )

Returns the minimum and maximum values in a single pass for single-precision floating-point number.

```javascript
var v = minmaxf( 4.0, 3.0 );
// returns <Float32Array>[ 3.0, 4.0 ]

v = minmaxf( +0.0, -0.0 );
// returns <Float32Array>[ -0.0, +0.0 ]
```

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

```javascript
var v = minmaxf( 4.2, NaN );
// returns <Float32Array>[ NaN, NaN ]

v = minmaxf( NaN, 3.14 );
// returns <Float32Array>[ NaN, NaN ]
```

#### minmaxf.assign( x, y, out, stride, offset )

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

```javascript
var Float32Array = require( '@stdlib/array/float32' );

var out = new Float32Array( 2 );

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

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

</section>

<!-- /.usage -->

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

<section class="notes">

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

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

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

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

</section>

<!-- /.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">

### Usages

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

#### stdlib_base_minmaxf( x, y, min, max )

Returns the minimum and maximum for single-precision floating-point number.

```c
float min;
float max;
stdlib_base_minmaxf( 3.2, 4.1, &min, &max );

stdlib_base_minmaxf( 0.0f, -0.0f, &min, &max );
```

The function accepts the following arguments:

- **x**: `[in] float` input value.
- **y**: `[in] float` input value.
- **min**: `[in] *float` output min.
- **max**: `[in] *float` output max.

```c
void stdlib_base_minmaxf( const float x, const float y, float* min, float* 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/minmaxf.h"
#include <stdio.h>

int main(void) {
const float x1[] = { 1.0, 0.45, -0.89, 0.0 / 0.0, -0.78, -0.22, 0.66, 0.11, -0.55, 0.0 };
const float x2[] = { -0.22, 0.66, 0.0, -0.55, 0.33, 1.0, 0.0 / 0.0, 0.11, 0.45, -0.78 };

float min;
float max;
int i;

for ( i = 0; i < 10; i++ ) {
stdlib_base_minmaxf( x1[i], x2[i], &min, &max);
printf( "x1[ %d ]: %f, x2[ %d ]: %f, minmaxf( x1[ %d ], x2[ %d ] ): ( %f, %f )\n", i, x1[ i ], i, x2[ i ], i, 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">

</section>

<!-- /.references -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

* * *

## See Also

- <span class="package-name">[`@stdlib/math/base/special/max`][@stdlib/math/base/special/max]</span><span class="delimiter">: </span><span class="description">return the maximum value.</span>
- <span class="package-name">[`@stdlib/math/base/special/min`][@stdlib/math/base/special/min]</span><span class="delimiter">: </span><span class="description">return the minimum value.</span>
- <span class="package-name">[`@stdlib/math/base/special/minmaxabs`][@stdlib/math/base/special/minmaxabs]</span><span class="delimiter">: </span><span class="description">return the minimum and maximum absolute values.</span>

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

<!-- <related-links> -->

[@stdlib/math/base/special/max]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/max

[@stdlib/math/base/special/min]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/min

[@stdlib/math/base/special/minmaxabs]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/minmaxabs

<!-- </related-links> -->

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/**
* @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 bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randn' );
var isFloat32Array = require( '@stdlib/assert/is-float32array' );
var isArray = require( '@stdlib/assert/is-array' );
var minf = require( '@stdlib/math/base/special/minf' );
var maxf = require( '@stdlib/math/base/special/maxf' );
var float32Array = require( '@stdlib/array/float32' );
var pkg = require( './../package.json' ).name;
var minmaxf = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var x;
var y;
var z;
var i;

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

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

z = new float32Array( 2 );
b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*1000.0 ) - 500.0;
y = ( randu()*1000.0 ) - 500.0;
z = minmaxf.assign( x, y, z, 1, 0 );
if ( z.length !== 2 ) {
b.fail( 'should have expected length' );
}
}
b.toc();
if ( !isFloat32Array( z ) ) {
b.fail( 'should return an array' );
}
b.pass( 'benchmark finished' );
b.end();
});

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

z = [];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*1000.0 ) - 500.0;
y = ( randu()*1000.0 ) - 500.0;
z = [ minf( x, y ), maxf( 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', function benchmark( b ) {
var x;
var y;
var z;
var i;

z = [];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*1000.0 ) - 500.0;
y = ( randu()*1000.0 ) - 500.0;
z[ 0 ] = minf( x, y );
z[ 1 ] = maxf( 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