-
-
Notifications
You must be signed in to change notification settings - Fork 801
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
feat: add implementation of stdlib/math/base/special/minmaxabsf
#6290
base: develop
Are you sure you want to change the base?
Changes from all commits
21e3c87
7ab15ee
79fa76e
a459008
e4244db
14ff772
f79ffcf
f092b66
160318e
eca6eed
883bae8
f24b5cc
a57e629
18d14d4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,258 @@ | ||||||
<!-- | ||||||
@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. | ||||||
--> | ||||||
|
||||||
# minmaxabsf | ||||||
|
||||||
> Return the minimum and maximum absolute single-precision floating-point numbers. | ||||||
<!-- 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 minmaxabsf = require( '@stdlib/math/base/special/minmaxabsf' ); | ||||||
``` | ||||||
|
||||||
#### minmaxabsf( x,y ) | ||||||
|
||||||
Returns the minimum and maximum absolute single-precision floating-point values in a single pass. | ||||||
|
||||||
```javascript | ||||||
var v = minmaxabsf( 4.0, 3.0 ); | ||||||
// returns <Float32Array>[ 3.0, 4.0 ] | ||||||
|
||||||
v = minmaxabsf( +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 = minmaxabsf( 4.2, NaN ); | ||||||
// returns <Float32Array>[ NaN, NaN ] | ||||||
|
||||||
v = minmaxabsf( NaN, 3.14 ); | ||||||
// returns <Float32Array>[ NaN, NaN ] | ||||||
``` | ||||||
|
||||||
#### minmaxabsf.assign( x, y, out, stride, offset ) | ||||||
|
||||||
Returns the minimum and maximum absolute single-precision values in a single pass and assigns the results to a provided output array. | ||||||
|
||||||
```javascript | ||||||
var Float32Array = require( '@stdlib/array/float32' ); | ||||||
|
||||||
var out = new Float32Array( 2 ); | ||||||
|
||||||
var v = minmaxabsf.assign( 5.0, -1.0, out, 1, 0 ); | ||||||
// returns <Float32Array>[ 1.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 randu = require( '@stdlib/random/base/randu' ); | ||||||
var minmaxabsf = require( '@stdlib/math/base/special/minmaxabsf' ); | ||||||
|
||||||
var x; | ||||||
var y; | ||||||
var v; | ||||||
var i; | ||||||
|
||||||
for ( i = 0; i < 100; i++ ) { | ||||||
x = ( randu()*100.0 ) - 50.0; | ||||||
y = ( randu()*100.0 ) - 50.0; | ||||||
v = minmaxabsf( x, y ); | ||||||
console.log( 'minmaxabsf(%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"> | ||||||
|
||||||
### Usage | ||||||
|
||||||
```c | ||||||
#include "stdlib/math/base/special/minmaxabsf.h" | ||||||
``` | ||||||
|
||||||
#### stdlib_base_minmaxabsf( x, y, min, max ) | ||||||
|
||||||
Returns the minimum and maximum absolute single-precision floating-point values in a single pass. | ||||||
|
||||||
```c | ||||||
float min; | ||||||
float max; | ||||||
stdlib_base_minmaxabsf( -4.2f, 3.14f, &min, &max ); | ||||||
// [ 3.14, 4.2 ] | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These return annotations need to be fixed. |
||||||
|
||||||
stdlib_base_minmaxabsf( 0.0f, -0.0f, &min, &max ); | ||||||
// [ 0.0, 0.0 ] | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs to be fixed. |
||||||
``` | ||||||
The function accepts the following arguments: | ||||||
- **x**: `[in] float` input value. | ||||||
- **y**: `[in] float` input value. | ||||||
- **min**: `[in] *float` output min. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
- **max**: `[in] *float` output max. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
```c | ||||||
void stdlib_base_minmaxabsf( 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/minmaxabsf.h" | ||||||
#include <stdlib.h> | ||||||
#include <stdio.h> | ||||||
|
||||||
int main(void) { | ||||||
srand(time(NULL)); // Seed random number generator | ||||||
|
||||||
float x1[10], x2[10]; | ||||||
float min, max; | ||||||
int i; | ||||||
|
||||||
// Generate random floating-point values between -1.0 and 1.0 | ||||||
for (i = 0; i < 10; i++) { | ||||||
x1[i] = ((float)rand() / RAND_MAX) * 2.0f - 1.0f; // Random float in [-1,1] | ||||||
x2[i] = ((float)rand() / RAND_MAX) * 2.0f - 1.0f; // Random float in [-1,1] | ||||||
|
||||||
// Introduce NaN values randomly | ||||||
if (rand() % 10 == 0) x1[i] = NAN; | ||||||
if (rand() % 10 == 0) x2[i] = NAN; | ||||||
} | ||||||
|
||||||
// Compute min and max values | ||||||
for (i = 0; i < 10; i++) { | ||||||
stdlib_base_minmaxabsf(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); | ||||||
} | ||||||
|
||||||
return 0; | ||||||
} | ||||||
``` | ||||||
</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"> | ||||||
</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> --> | ||||||
<!-- </related-links> --> | ||||||
</section> | ||||||
<!-- /.links --> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* @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 float32Array = require( '@stdlib/array/float32' ); | ||
var pkg = require( './../package.json' ).name; | ||
var minmaxabsf = 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 = minmaxabsf( 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 = minmaxabsf.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(); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can refactor this example to use
random/array/uniform
andconsole/log-each-map
.