Skip to content

Commit 7eb4379

Browse files
committed
feat: add math/base/special/minmaxabsf
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: passed - task: lint_c_examples status: passed - task: lint_c_benchmarks status: passed - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 57b0e74 commit 7eb4379

File tree

27 files changed

+2604
-0
lines changed

27 files changed

+2604
-0
lines changed
Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# minmaxabsf
22+
23+
> Return the minimum and maximum absolute values of two single-precision floating-point numbers.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var minmaxabsf = require( '@stdlib/math/base/special/minmaxabsf' );
41+
```
42+
43+
#### minmaxabsf( x, y )
44+
45+
Returns the minimum and maximum absolute values of two single-precision floating-point numbers in a single pass.
46+
47+
```javascript
48+
var v = minmaxabsf( 4.2, 3.14 );
49+
// returns [ 3.14, 4.2 ]
50+
51+
v = minmaxabsf( +0.0, -0.0 );
52+
// returns [ 0.0, 0.0 ]
53+
```
54+
55+
If any argument is `NaN`, the function returns `NaN` for both the minimum value and the maximum value.
56+
57+
```javascript
58+
var v = minmaxabsf( 4.2, NaN );
59+
// returns [ NaN, NaN ]
60+
61+
v = minmaxabsf( NaN, 3.14 );
62+
// returns [ NaN, NaN ]
63+
```
64+
65+
#### minmaxabsf.assign( x, y, out, stride, offset )
66+
67+
Returns the minimum and maximum absolute values of two single-precision floating-point numbers in a single pass and assigns results to a provided output array.
68+
69+
```javascript
70+
var Float32Array = require( '@stdlib/array/float32' );
71+
72+
var out = new Float32Array( 2 );
73+
74+
var v = minmaxabsf.assign( 5.0, -2.0, out, 1, 0 );
75+
// returns <Float32Array>[ 2.0, 5.0 ]
76+
77+
var bool = ( v === out );
78+
// returns true
79+
```
80+
81+
</section>
82+
83+
<!-- /.usage -->
84+
85+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
86+
87+
<section class="notes">
88+
89+
</section>
90+
91+
<!-- /.notes -->
92+
93+
<!-- Package usage examples. -->
94+
95+
<section class="examples">
96+
97+
## Examples
98+
99+
<!-- eslint no-undef: "error" -->
100+
101+
```javascript
102+
var randu = require( '@stdlib/random/base/randu' );
103+
var minmaxabsf = require( '@stdlib/math/base/special/minmaxabsf' );
104+
105+
var x;
106+
var y;
107+
var v;
108+
var i;
109+
110+
for ( i = 0; i < 100; i++ ) {
111+
x = ( randu()*100.0 ) - 50.0;
112+
y = ( randu()*100.0 ) - 50.0;
113+
v = minmaxabsf( x, y );
114+
console.log( 'minmaxabsf(%d,%d) = [%d, %d]', x, y, v[0], v[1] );
115+
}
116+
```
117+
118+
</section>
119+
120+
<!-- /.examples -->
121+
122+
<!-- C interface documentation. -->
123+
124+
* * *
125+
126+
<section class="c">
127+
128+
## C APIs
129+
130+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
131+
132+
<section class="intro">
133+
134+
</section>
135+
136+
<!-- /.intro -->
137+
138+
<!-- C usage documentation. -->
139+
140+
<section class="usage">
141+
142+
### Usage
143+
144+
```c
145+
#include "stdlib/math/base/special/minmaxabsf.h"
146+
```
147+
148+
#### stdlib_base_minmaxabsf( x, y, &min, &max )
149+
150+
Evaluates the minimum and maximum absolute values of two single-precision floating-point numbers in a single pass.
151+
152+
```c
153+
float x = -3.14f;
154+
float y = 2.71f;
155+
156+
float min;
157+
float max;
158+
stdlib_base_minmaxabsf( x, y, &min, &max );
159+
```
160+
161+
The function accepts the following arguments:
162+
163+
- **x**: `[in] float` first number.
164+
- **y**: `[in] float` second number.
165+
- **min**: `[out] float` destination for the minimum absolute value.
166+
- **max**: `[out] float` destination for the maximum absolute value.
167+
168+
```c
169+
void stdlib_base_minmaxabsf( const float x, const float y, float* min, float* max );
170+
```
171+
172+
</section>
173+
174+
<!-- /.usage -->
175+
176+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
177+
178+
<section class="notes">
179+
180+
</section>
181+
182+
<!-- /.notes -->
183+
184+
<!-- C API usage examples. -->
185+
186+
<section class="examples">
187+
188+
### Examples
189+
190+
```c
191+
#include "stdlib/math/base/special/minmaxabsf.h"
192+
#include <stdio.h>
193+
194+
int main( void ) {
195+
const float x[] = { 1.0f, 0.45f, -0.89f, 0.0f / 0.0f, -0.78f, -0.22f, 0.66f, 0.11f, -0.55f, 0.0f };
196+
const float y[] = { -0.22f, 0.66f, 0.0f, -0.55f, 0.33f, 1.0f, 0.0f / 0.0f, 0.11f, 0.45f, -0.78f };
197+
198+
float min;
199+
float max;
200+
int i;
201+
for ( i = 0; i < 10; i++ ) {
202+
stdlib_base_minmaxabsf( x[ i ], y[ i ], &min, &max );
203+
printf( "x: %f, y: %f => min: %f, max: %f\n", x[ i ], y[ i ], min, max );
204+
}
205+
}
206+
```
207+
208+
</section>
209+
210+
<!-- /.examples -->
211+
212+
</section>
213+
214+
<!-- /.c -->
215+
216+
<!-- 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. -->
217+
218+
<section class="references">
219+
220+
</section>
221+
222+
<!-- /.references -->
223+
224+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
225+
226+
<section class="related">
227+
228+
</section>
229+
230+
<!-- /.related -->
231+
232+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
233+
234+
<section class="links">
235+
236+
<!-- <related-links> -->
237+
238+
<!-- </related-links> -->
239+
240+
</section>
241+
242+
<!-- /.links -->
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
25+
var isArray = require( '@stdlib/assert/is-array' );
26+
var minabsf = require( '@stdlib/math/base/special/minabsf' );
27+
var maxabsf = require( '@stdlib/math/base/special/maxabsf' );
28+
var pkg = require( './../package.json' ).name;
29+
var minmaxabsf = require( './../lib' );
30+
31+
32+
// MAIN //
33+
34+
bench( pkg, function benchmark( b ) {
35+
var x;
36+
var y;
37+
var z;
38+
var i;
39+
40+
x = uniform( 100, -500.0, 500.0, {
41+
'dtype': 'float32'
42+
});
43+
y = uniform( 100, -500.0, 500.0, {
44+
'dtype': 'float32'
45+
});
46+
47+
b.tic();
48+
for ( i = 0; i < b.iterations; i++ ) {
49+
z = minmaxabsf( x[ i%x.length ], y[ i%y.length ] );
50+
if ( z.length !== 2 ) {
51+
b.fail( 'should have expected length' );
52+
}
53+
}
54+
b.toc();
55+
if ( !isArray( z ) ) {
56+
b.fail( 'should return an array' );
57+
}
58+
b.pass( 'benchmark finished' );
59+
b.end();
60+
});
61+
62+
bench( pkg+':assign', function benchmark( b ) {
63+
var out;
64+
var x;
65+
var y;
66+
var z;
67+
var i;
68+
69+
x = uniform( 100, -500.0, 500.0, {
70+
'dtype': 'float32'
71+
});
72+
y = uniform( 100, -500.0, 500.0, {
73+
'dtype': 'float32'
74+
});
75+
out = [ 0.0, 0.0 ];
76+
77+
b.tic();
78+
for ( i = 0; i < b.iterations; i++ ) {
79+
z = minmaxabsf.assign( x[ i%x.length ], y[ i%y.length ], out, 1, 0 );
80+
if ( z.length !== 2 ) {
81+
b.fail( 'should have expected length' );
82+
}
83+
}
84+
b.toc();
85+
if ( !isArray( z ) ) {
86+
b.fail( 'should return an array' );
87+
}
88+
b.pass( 'benchmark finished' );
89+
b.end();
90+
});
91+
92+
bench( pkg+'::minabsf,maxabsf', function benchmark( b ) {
93+
var x;
94+
var y;
95+
var z;
96+
var i;
97+
98+
x = uniform( 100, -500.0, 500.0, {
99+
'dtype': 'float32'
100+
});
101+
y = uniform( 100, -500.0, 500.0, {
102+
'dtype': 'float32'
103+
});
104+
105+
b.tic();
106+
for ( i = 0; i < b.iterations; i++ ) {
107+
z = [ minabsf( x[ i%x.length ], y[ i%y.length ] ), maxabsf( x, y ) ];
108+
if ( z.length !== 2 ) {
109+
b.fail( 'should have expected length' );
110+
}
111+
}
112+
b.toc();
113+
if ( !isArray( z ) ) {
114+
b.fail( 'should return an array' );
115+
}
116+
b.pass( 'benchmark finished' );
117+
b.end();
118+
});
119+
120+
bench( pkg+'::minabsf,maxabsf,memory_reuse', function benchmark( b ) {
121+
var x;
122+
var y;
123+
var z;
124+
var i;
125+
126+
x = uniform( 100, -500.0, 500.0, {
127+
'dtype': 'float32'
128+
});
129+
y = uniform( 100, -500.0, 500.0, {
130+
'dtype': 'float32'
131+
});
132+
z = [ 0.0, 0.0 ];
133+
134+
b.tic();
135+
for ( i = 0; i < b.iterations; i++ ) {
136+
z[ 0 ] = minabsf( x[ i%x.length ], y[ i%y.length ] );
137+
z[ 1 ] = maxabsf( x[ i%x.length ], y[ i%y.length ] );
138+
if ( z.length !== 2 ) {
139+
b.fail( 'should have expected length' );
140+
}
141+
}
142+
b.toc();
143+
if ( !isArray( z ) ) {
144+
b.fail( 'should return an array' );
145+
}
146+
b.pass( 'benchmark finished' );
147+
b.end();
148+
});

0 commit comments

Comments
 (0)