Skip to content

Commit bffee89

Browse files
RidamGargstdlib-botPlaneshifter
authored
feat: add math/base/special/nanmax
PR-URL: #2358 Closes: #2317 --------- Signed-off-by: Ridam Garg <67867319+RidamGarg@users.noreply.github.com> Signed-off-by: Philipp Burckhardt <pburckhardt@outlook.com> Co-authored-by: stdlib-bot <82920195+stdlib-bot@users.noreply.github.com> Co-authored-by: Philipp Burckhardt <pburckhardt@outlook.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
1 parent 46ff6c1 commit bffee89

File tree

10 files changed

+551
-0
lines changed

10 files changed

+551
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
# nanmax
22+
23+
> Return the maximum value, 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 nanmax = require( '@stdlib/math/base/special/nanmax' );
41+
```
42+
43+
#### nanmax( x, y )
44+
45+
Returns the maximum value.
46+
47+
```javascript
48+
var v = nanmax( 4.2, 3.14 );
49+
// returns 4.2
50+
51+
v = nanmax( +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 = nanmax( 4.2, NaN );
59+
// returns 4.2
60+
61+
v = nanmax( 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 = nanmax( 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+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
83+
84+
<section class="related">
85+
86+
</section>
87+
88+
<!-- /.related -->
89+
90+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
91+
92+
<section class="links">
93+
94+
<!-- <related-links> -->
95+
96+
<!-- </related-links> -->
97+
98+
</section>
99+
100+
<!-- /.links -->
Lines changed: 55 additions & 0 deletions
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var pkg = require( './../package.json' ).name;
26+
var nanmax = 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, 2, 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 = nanmax( x, y );
45+
if ( isnan( z ) ) {
46+
b.fail( 'should not return NaN' );
47+
}
48+
}
49+
b.toc();
50+
if ( isnan( z ) ) {
51+
b.fail( 'should not return NaN' );
52+
}
53+
b.pass( 'benchmark finished' );
54+
b.end();
55+
});
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
// TypeScript Version: 4.1
20+
21+
/**
22+
* Return the maximum value, ignoring NaN.
23+
*
24+
* @param x - first number
25+
* @param y - second number
26+
* @returns maximum value
27+
*
28+
* @example
29+
* var v = nanmax( 3.14, 4.2 );
30+
* // returns 4.2
31+
*
32+
* @example
33+
* var v = nanmax( 4.14, NaN );
34+
* // returns 4.14
35+
*
36+
* @example
37+
* var v = nanmax( NaN, NaN );
38+
* // returns NaN
39+
*/
40+
declare function nanmax( x: number, y: number ): number;
41+
42+
43+
// EXPORTS //
44+
45+
export = nanmax;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
{{alias}}( x, y )
3+
Returns the maximum value.
4+
5+
If one operand is NaN, the other operand is always returned.
6+
7+
Parameters
8+
----------
9+
x: number
10+
First number.
11+
12+
y: number
13+
Second number.
14+
15+
Returns
16+
-------
17+
out: number
18+
Maximum value.
19+
20+
Examples
21+
--------
22+
> var v = {{alias}}( 3.14, 4.2 )
23+
4.2
24+
> v = {{alias}}( 3.14, NaN )
25+
3.14
26+
> v = {{alias}}( NaN, 4.2 )
27+
4.2
28+
> v = {{alias}}( NaN, NaN )
29+
NaN
30+
31+
See Also
32+
--------
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
import nanmax = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a number...
25+
{
26+
nanmax( 3.0, -0.4 ); // $ExpectType number
27+
}
28+
29+
// The compiler throws an error if the function is provided non-number arguments...
30+
{
31+
nanmax( true, 3.0 ); // $ExpectError
32+
nanmax( false, 3.0 ); // $ExpectError
33+
nanmax( [], 3.0 ); // $ExpectError
34+
nanmax( {}, 3.0 ); // $ExpectError
35+
nanmax( 'abc', 3.0 ); // $ExpectError
36+
nanmax( ( x: number ): number => x, 3.0 ); // $ExpectError
37+
38+
nanmax( 1.2, true ); // $ExpectError
39+
nanmax( 1.2, false ); // $ExpectError
40+
nanmax( 1.2, [] ); // $ExpectError
41+
nanmax( 1.2, {} ); // $ExpectError
42+
nanmax( 1.2, 'abc' ); // $ExpectError
43+
nanmax( 1.2, ( x: number ): number => x ); // $ExpectError
44+
}
45+
46+
// The compiler throws an error if the function is provided an unsupported number of arguments...
47+
{
48+
nanmax(); // $ExpectError
49+
nanmax( 3.0 ); // $ExpectError
50+
nanmax( 3.0, 2.0, 1.0 ); // $ExpectError
51+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
var nanmax = require( './../lib' );
22+
23+
var m = nanmax( 3.0, 4.0 );
24+
console.log( m );
25+
// => 4.0
26+
27+
m = nanmax( NaN, 4.0 );
28+
console.log( m );
29+
// => 4.0
30+
31+
m = nanmax( 4.0, NaN );
32+
console.log( m );
33+
// => 4.0
34+
35+
m = nanmax( NaN, NaN );
36+
console.log( m );
37+
// => NaN
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
/**
22+
* Return the maximum value, ignoring NaN.
23+
*
24+
* @module @stdlib/math/base/special/nanmax
25+
*
26+
* @example
27+
* var nanmax = require( '@stdlib/math/base/special/nanmax' );
28+
*
29+
* var v = nanmax( 3.14, 4.2 );
30+
* // returns 4.2
31+
*
32+
* v = nanmax( 4.14, NaN );
33+
* // returns 4.14
34+
*
35+
* v = nanmax( NaN, NaN );
36+
* // returns NaN
37+
*/
38+
39+
// MODULES //
40+
41+
var nanmax = require( './main.js' );
42+
43+
44+
// EXPORTS //
45+
46+
module.exports = nanmax;

0 commit comments

Comments
 (0)