Skip to content

Commit a985cc2

Browse files
RidamGargRidamGargstdlib-botPlaneshifter
authored
feat: add math/base/special/nanmin
PR-URL: #2342 Closes: #2318 --------- Signed-off-by: Ridam Garg <ridamgarg8979@gmail> Signed-off-by: Philipp Burckhardt <[email protected]> Co-authored-by: RidamGarg <ridamgarg8979@gmail> Co-authored-by: stdlib-bot <[email protected]> Co-authored-by: Philipp Burckhardt <[email protected]>
1 parent ddf4bdb commit a985cc2

File tree

10 files changed

+532
-0
lines changed

10 files changed

+532
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
# nanmin
22+
23+
> Return the minimum 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 nanmin = require( '@stdlib/math/base/special/nanmin' );
41+
```
42+
43+
#### nanmin( x, y )
44+
45+
Returns the minimum value.
46+
47+
```javascript
48+
var v = nanmin( 4.2, 3.14 );
49+
// returns 3.14
50+
51+
v = nanmin( +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 = nanmin( 4.2, NaN );
59+
// returns 4.2
60+
61+
v = nanmin( NaN, 3.14 );
62+
// returns 3.14
63+
```
64+
65+
66+
If both argument are `NaN`, the function returns `NaN`.
67+
68+
```javascript
69+
var v = nanmin( NaN, NaN );
70+
// returns NaN
71+
72+
```
73+
74+
</section>
75+
76+
<!-- /.usage -->
77+
78+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
79+
80+
<section class="notes">
81+
82+
</section>
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 nanmin = 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 = nanmin( 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+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
{{alias}}( x, y )
3+
Returns the minimum 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+
Minimum value.
19+
20+
Examples
21+
--------
22+
> var v = {{alias}}( 3.14, 4.2 )
23+
3.14
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+
--------
33+
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 minimum value, ignoring NaN.
23+
*
24+
* @param x - first number
25+
* @param y - second number
26+
* @returns minimum value
27+
*
28+
* @example
29+
* var v = min( 3.14, 4.2 );
30+
* // returns 3.14
31+
*
32+
* @example
33+
* var v = min( 4.14, NaN );
34+
* // returns 4.14
35+
*
36+
* @example
37+
* var v = min( NaN, NaN );
38+
* // returns NaN
39+
*/
40+
declare function nanmin( x: number, y: number ): number;
41+
42+
43+
// EXPORTS //
44+
45+
export = nanmin;
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 nanmin = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a number...
25+
{
26+
nanmin( 3.0, -0.4 ); // $ExpectType number
27+
}
28+
29+
// The compiler throws an error if the function is provided non-number arguments...
30+
{
31+
nanmin( true, 3.0 ); // $ExpectError
32+
nanmin( false, 3.0 ); // $ExpectError
33+
nanmin( [], 3.0 ); // $ExpectError
34+
nanmin( {}, 3.0 ); // $ExpectError
35+
nanmin( 'abc', 3.0 ); // $ExpectError
36+
nanmin( ( x: number ): number => x, 3.0 ); // $ExpectError
37+
38+
nanmin( 1.2, true ); // $ExpectError
39+
nanmin( 1.2, false ); // $ExpectError
40+
nanmin( 1.2, [] ); // $ExpectError
41+
nanmin( 1.2, {} ); // $ExpectError
42+
nanmin( 1.2, 'abc' ); // $ExpectError
43+
nanmin( 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+
nanmin(); // $ExpectError
49+
nanmin( 3.0 ); // $ExpectError
50+
nanmin( 3.0, 2.0, 1.0 ); // $ExpectError
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 nanmin = require( './../lib' );
22+
23+
var m = nanmin( 3.0, 4.0 );
24+
// returns 3.0
25+
26+
m = nanmin( NaN, 4.0 );
27+
// returns 4.0
28+
29+
m = nanmin( 4.0, NaN );
30+
// returns 4.0
31+
32+
m = nanmin( NaN, NaN );
33+
// returns NaN
34+
35+
console.log(m);
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 minimum value, ignoring NaN.
23+
*
24+
* @module @stdlib/math/base/special/nanmin
25+
*
26+
* @example
27+
* var nanmin = require( '@stdlib/math/base/special/nanmin' );
28+
*
29+
* var v = nanmin( 3.14, 4.2 );
30+
* // returns 3.14
31+
*
32+
* v = nanmin( 4.14, NaN );
33+
* // returns 4.14
34+
*
35+
* v = nanmin( NaN, NaN );
36+
* // returns NaN
37+
*/
38+
39+
// MODULES //
40+
41+
var nanmin = require( './main.js' );
42+
43+
44+
// EXPORTS //
45+
46+
module.exports = nanmin;

0 commit comments

Comments
 (0)