Skip to content

Commit d01e0f3

Browse files
feat: add math/base/special/minabsf
PR-URL: #2832 Ref: #649 --------- Signed-off-by: Philipp Burckhardt <[email protected]> Co-authored-by: Philipp Burckhardt <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]>
1 parent 0215bd7 commit d01e0f3

File tree

24 files changed

+1853
-0
lines changed

24 files changed

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

0 commit comments

Comments
 (0)