Skip to content

Commit 594602f

Browse files
feat: add math/base/special/fast/maxf
--- 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: missing_dependencies - task: lint_c_examples status: missing_dependencies - task: lint_c_benchmarks status: missing_dependencies - 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 990705b commit 594602f

File tree

24 files changed

+1918
-0
lines changed

24 files changed

+1918
-0
lines changed
Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
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+
# maxf
22+
23+
> Return the maximum 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 maxf = require( '@stdlib/math/base/special/fast/maxf' );
41+
```
42+
43+
#### maxf( x, y )
44+
45+
Returns the maximum single-precision floating-point number.
46+
47+
```javascript
48+
var v = maxf( 4.2, 3.14 );
49+
// returns 4.199999809265137
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 = maxf( +0.0, -0.0 );
66+
// returns -0.0
67+
68+
v = maxf( -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 = maxf( 3.14, NaN );
76+
// returns NaN
77+
78+
v = maxf( NaN, 3.14 );
79+
// returns 3.140000104904175
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 minstd = require( '@stdlib/random/base/minstd-shuffle' );
96+
var maxf = require( '@stdlib/math/base/special/fast/maxf' );
97+
98+
var x;
99+
var y;
100+
var v;
101+
var i;
102+
103+
for ( i = 0; i < 100; i++ ) {
104+
x = minstd();
105+
y = minstd();
106+
v = maxf( x, y );
107+
console.log( 'maxf(%d,%d) = %d', x, y, v );
108+
}
109+
```
110+
111+
</section>
112+
113+
<!-- /.examples -->
114+
115+
<!-- C interface documentation. -->
116+
117+
* * *
118+
119+
<section class="c">
120+
121+
## C APIs
122+
123+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
124+
125+
<section class="intro">
126+
127+
</section>
128+
129+
<!-- /.intro -->
130+
131+
<!-- C usage documentation. -->
132+
133+
<section class="usage">
134+
135+
### Usage
136+
137+
```c
138+
#include "stdlib/math/base/special/fast/maxf.h"
139+
```
140+
141+
#### stdlib_base_fast_maxf( x, y )
142+
143+
Returns the maximum single-precision floating-point number.
144+
145+
```c
146+
float out = stdlib_base_fast_maxf( 4.2f, 3.14f );
147+
// returns 4.2f
148+
149+
out = stdlib_base_fast_maxf( 0.0f, -0.0f );
150+
// returns 0.0f
151+
```
152+
153+
The function accepts the following arguments:
154+
155+
- **x**: `[in] float` input value.
156+
- **y**: `[in] float` input value.
157+
158+
```c
159+
float stdlib_base_fast_maxf( const float x, const float y );
160+
```
161+
162+
</section>
163+
164+
<!-- /.usage -->
165+
166+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
167+
168+
<section class="notes">
169+
170+
</section>
171+
172+
<!-- /.notes -->
173+
174+
<!-- C API usage examples. -->
175+
176+
<section class="examples">
177+
178+
### Examples
179+
180+
```c
181+
#include "stdlib/math/base/special/fast/maxf.h"
182+
#include <stdlib.h>
183+
#include <stdio.h>
184+
185+
int main( void ) {
186+
float x;
187+
float y;
188+
float v;
189+
int i;
190+
191+
for ( i = 0; i < 100; i++ ) {
192+
x = ( ( (float)rand() / (float)RAND_MAX ) * 200.0f ) - 100.0f;
193+
y = ( ( (float)rand() / (float)RAND_MAX ) * 200.0f ) - 100.0f;
194+
v = stdlib_base_fast_maxf( x, y );
195+
printf( "x: %f, y: %f, maxf(x, y): %f\n", x, y, v );
196+
}
197+
}
198+
```
199+
200+
</section>
201+
202+
<!-- /.examples -->
203+
204+
</section>
205+
206+
<!-- /.c -->
207+
208+
<!-- 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. -->
209+
210+
<section class="references">
211+
212+
</section>
213+
214+
<!-- /.references -->
215+
216+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
217+
218+
<section class="related">
219+
220+
</section>
221+
222+
<!-- /.related -->
223+
224+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
225+
226+
<section class="links">
227+
228+
<!-- <related-links> -->
229+
230+
<!-- </related-links> -->
231+
232+
</section>
233+
234+
<!-- /.links -->
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 maxf = 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+
43+
x = uniform( 100, -100.0, 100.0, opts );
44+
y = uniform( 100, -100.0, 100.0, opts );
45+
46+
b.tic();
47+
for ( i = 0; i < b.iterations; i++ ) {
48+
z = maxf( x[ i % x.length ], y[ i % y.length ] );
49+
if ( isnanf( z ) ) {
50+
b.fail( 'should not return NaN' );
51+
}
52+
}
53+
b.toc();
54+
if ( isnanf( z ) ) {
55+
b.fail( 'should not return NaN' );
56+
}
57+
b.pass( 'benchmark finished' );
58+
b.end();
59+
});
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 maxf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
34+
var opts = {
35+
'skip': ( maxf 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 = maxf( 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)