Skip to content

Commit 84b8294

Browse files
gunjjoshikgryte
andauthored
feat: add math/base/special/nanmaxf
PR-URL: #3035 Ref: #649 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Signed-off-by: Athan Reines <kgryte@gmail.com>
1 parent f770fc2 commit 84b8294

File tree

24 files changed

+1809
-0
lines changed

24 files changed

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

0 commit comments

Comments
 (0)