Skip to content

Commit 8f581f1

Browse files
feat: add math/base/special/fast/minf
PR-URL: #8112 Reviewed-by: Athan Reines <[email protected]> Reviewed-by: Karan Anand <[email protected]>
1 parent 2b52cb2 commit 8f581f1

File tree

24 files changed

+1905
-0
lines changed

24 files changed

+1905
-0
lines changed
Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
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+
# minf
22+
23+
> Return the minimum single-precision floating-point number.
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 minf = require( '@stdlib/math/base/special/fast/minf' );
41+
```
42+
43+
#### minf( x, y )
44+
45+
Returns the minimum single-precision floating-point number.
46+
47+
```javascript
48+
var v = minf( 4.2, 3.14 );
49+
// returns ~3.14
50+
```
51+
52+
</section>
53+
54+
<!-- /.usage -->
55+
56+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
57+
58+
<section class="notes">
59+
60+
## Notes
61+
62+
- The implementation **ignores** the sign of `0`.
63+
64+
```javascript
65+
var v = minf( +0.0, -0.0 );
66+
// returns -0.0
67+
68+
v = minf( -0.0, +0.0 );
69+
// returns +0.0
70+
```
71+
72+
- The implementation does **not** check for `NaN` arguments.
73+
74+
```javascript
75+
var v = minf( 3.14, NaN );
76+
// returns NaN
77+
78+
v = minf( NaN, 3.14 );
79+
// returns ~3.14
80+
```
81+
82+
</section>
83+
84+
<!-- /.notes -->
85+
86+
<!-- Package usage examples. -->
87+
88+
<section class="examples">
89+
90+
## Examples
91+
92+
<!-- eslint no-undef: "error" -->
93+
94+
```javascript
95+
var uniform = require( '@stdlib/random/array/uniform' );
96+
var logEachMap = require( '@stdlib/console/log-each-map' );
97+
var minf = require( '@stdlib/math/base/special/fast/minf' );
98+
99+
var opts = {
100+
'dtype': 'float32'
101+
};
102+
var x = uniform( 100, -10.0, 10.0, opts );
103+
var y = uniform( x.length, -10.0, 10.0, opts );
104+
105+
logEachMap( 'minf(%0.4f,%0.4f) = %0.4f', x, y, minf );
106+
```
107+
108+
</section>
109+
110+
<!-- /.examples -->
111+
112+
<!-- C interface documentation. -->
113+
114+
* * *
115+
116+
<section class="c">
117+
118+
## C APIs
119+
120+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
121+
122+
<section class="intro">
123+
124+
</section>
125+
126+
<!-- /.intro -->
127+
128+
<!-- C usage documentation. -->
129+
130+
<section class="usage">
131+
132+
### Usage
133+
134+
```c
135+
#include "stdlib/math/base/special/fast/minf.h"
136+
```
137+
138+
#### stdlib_base_fast_minf( x, y )
139+
140+
Returns the minimum single-precision floating-point number.
141+
142+
```c
143+
float out = stdlib_base_fast_minf( 4.2f, 3.14f );
144+
// returns 3.14f
145+
146+
out = stdlib_base_fast_minf( 0.0f, -0.0f );
147+
// returns -0.0f
148+
```
149+
150+
The function accepts the following arguments:
151+
152+
- **x**: `[in] float` input value.
153+
- **y**: `[in] float` input value.
154+
155+
```c
156+
float stdlib_base_fast_minf( const float x, const float y );
157+
```
158+
159+
</section>
160+
161+
<!-- /.usage -->
162+
163+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
164+
165+
<section class="notes">
166+
167+
</section>
168+
169+
<!-- /.notes -->
170+
171+
<!-- C API usage examples. -->
172+
173+
<section class="examples">
174+
175+
### Examples
176+
177+
```c
178+
#include "stdlib/math/base/special/fast/minf.h"
179+
#include <stdlib.h>
180+
#include <stdio.h>
181+
182+
int main( void ) {
183+
float x;
184+
float y;
185+
float v;
186+
int i;
187+
188+
for ( i = 0; i < 100; i++ ) {
189+
x = ( ( (float)rand() / (float)RAND_MAX ) * 200.0f ) - 100.0f;
190+
y = ( ( (float)rand() / (float)RAND_MAX ) * 200.0f ) - 100.0f;
191+
v = stdlib_base_fast_minf( x, y );
192+
printf( "x: %f, y: %f, minf(x, y): %f\n", x, y, v );
193+
}
194+
}
195+
```
196+
197+
</section>
198+
199+
<!-- /.examples -->
200+
201+
</section>
202+
203+
<!-- /.c -->
204+
205+
<!-- 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. -->
206+
207+
<section class="references">
208+
209+
</section>
210+
211+
<!-- /.references -->
212+
213+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
214+
215+
<section class="related">
216+
217+
</section>
218+
219+
<!-- /.related -->
220+
221+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
222+
223+
<section class="links">
224+
225+
<!-- <related-links> -->
226+
227+
<!-- </related-links> -->
228+
229+
</section>
230+
231+
<!-- /.links -->
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var pkg = require( './../package.json' ).name;
27+
var minf = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var opts;
34+
var x;
35+
var y;
36+
var z;
37+
var i;
38+
39+
opts = {
40+
'dtype': 'float32'
41+
};
42+
x = uniform( 100, -100.0, 100.0, opts );
43+
y = uniform( 100, -100.0, 100.0, opts );
44+
45+
b.tic();
46+
for ( i = 0; i < b.iterations; i++ ) {
47+
z = minf( x[ i%x.length ], y[ i%y.length ] );
48+
if ( isnanf( z ) ) {
49+
b.fail( 'should not return NaN' );
50+
}
51+
}
52+
b.toc();
53+
if ( isnanf( z ) ) {
54+
b.fail( 'should not return NaN' );
55+
}
56+
b.pass( 'benchmark finished' );
57+
b.end();
58+
});
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var uniform = require( '@stdlib/random/array/uniform' );
26+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
27+
var tryRequire = require( '@stdlib/utils/try-require' );
28+
var pkg = require( './../package.json' ).name;
29+
30+
31+
// VARIABLES //
32+
33+
var minf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
34+
var opts = {
35+
'skip': ( minf instanceof Error )
36+
};
37+
38+
39+
// MAIN //
40+
41+
bench( pkg + '::native', opts, function benchmark( b ) {
42+
var opts;
43+
var x;
44+
var y;
45+
var z;
46+
var i;
47+
48+
opts = {
49+
'dtype': 'float32'
50+
};
51+
x = uniform( 100, -100.0, 100.0, opts );
52+
y = uniform( 100, -100.0, 100.0, opts );
53+
54+
b.tic();
55+
for ( i = 0; i < b.iterations; i++ ) {
56+
z = minf( x[ i%x.length ], y[ i%y.length ] );
57+
if ( isnanf( z ) ) {
58+
b.fail( 'should not return NaN' );
59+
}
60+
}
61+
b.toc();
62+
if ( isnanf( z ) ) {
63+
b.fail( 'should not return NaN' );
64+
}
65+
b.pass( 'benchmark finished' );
66+
b.end();
67+
});

0 commit comments

Comments
 (0)