-
-
Notifications
You must be signed in to change notification settings - Fork 835
feat: add math/base/special/nanmin
#2342
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
0f2c4d6
d144bb4
78d82a3
cf88b61
4a1e103
aeb3795
4ff14fc
b1edc3d
6460912
5ccd941
4a4f8f3
39e88db
4a8425e
0118ae0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<!-- | ||
|
||
@license Apache-2.0 | ||
|
||
Copyright (c) 2024 The Stdlib Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
|
||
--> | ||
|
||
# nanmin | ||
|
||
> Return the minimum value, ignoring NaN. | ||
|
||
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> | ||
|
||
<section class="intro"> | ||
|
||
</section> | ||
|
||
<!-- /.intro --> | ||
|
||
<!-- Package usage documentation. --> | ||
|
||
<section class="usage"> | ||
|
||
## Usage | ||
|
||
```javascript | ||
var nanmin = require( '@stdlib/math/base/special/nanmin' ); | ||
``` | ||
|
||
#### nanmin( x, y ) | ||
|
||
Returns the minimum value. | ||
|
||
```javascript | ||
var v = nanmin( 4.2, 3.14 ); | ||
// returns 3.14 | ||
|
||
v = nanmin( +0.0, -0.0 ); | ||
// returns -0.0 | ||
``` | ||
|
||
If any argument is `NaN`, the function returns the other operand. | ||
|
||
```javascript | ||
var v = nanmin( 4.2, NaN ); | ||
// returns 4.2 | ||
|
||
v = nanmin( NaN, 3.14 ); | ||
// returns 3.14 | ||
``` | ||
|
||
|
||
If both argument are `NaN`, the function returns `NaN`. | ||
|
||
```javascript | ||
var v = nanmin( NaN, NaN ); | ||
// returns NaN | ||
|
||
``` | ||
|
||
</section> | ||
|
||
<!-- /.usage --> | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This readme is missing various sections (eg, examples, links, etc). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added the section previously but got this comment from @Planeshifter. We auto-populate these sections from metadata, so this should be left empty. Should I add this in my next PR? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @RidamGarg Sorry if my comment was not fully clear; the suggested change was to remove the inner content of the sections, not remove them altogether. So <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
<section class="related">
* * *
## See Also
- <span class="package-name">[`@stdlib/math/base/special/max`][@stdlib/math/base/special/max]</span><span class="delimiter">: </span><span class="description">return the maximum value.</span>
- <span class="package-name">[`@stdlib/math/base/special/minabs`][@stdlib/math/base/special/minabs]</span><span class="delimiter">: </span><span class="description">return the minimum absolute value.</span>
- <span class="package-name">[`@stdlib/math/base/special/minn`][@stdlib/math/base/special/minn]</span><span class="delimiter">: </span><span class="description">return the minimum value.</span>
</section>
<!-- /.related -->
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
<section class="links">
<!-- <related-links> -->
[@stdlib/math/base/special/max]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/max
[@stdlib/math/base/special/minabs]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/minabs
[@stdlib/math/base/special/minn]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/minn
<!-- </related-links> -->
</section>
<!-- /.links --> should have become <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
<section class="related">
</section>
<!-- /.related -->
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
<section class="links">
<!-- <related-links> -->
<!-- </related-links> -->
</section>
<!-- /.links --> We auto-populate the contents for the related packages, but that will only work if the sections are there. Notice that the examples section is separate from that. It should contain the example from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ooh, Okay @Planeshifter, Now I got it. Thanks for the clarification. |
||
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
||
<section class="notes"> | ||
|
||
</section> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2024 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// MODULES // | ||
|
||
var bench = require( '@stdlib/bench' ); | ||
var isnan = require( '@stdlib/math/base/assert/is-nan' ); | ||
var pkg = require( './../package.json' ).name; | ||
var nanmin = require( './../lib' ); | ||
|
||
|
||
// MAIN // | ||
|
||
bench( pkg, function benchmark( b ) { | ||
var values; | ||
var x; | ||
var y; | ||
var z; | ||
var i; | ||
|
||
values = [ 3, 2, 2.0, 1.5, NaN ]; | ||
x = 0.4; | ||
|
||
b.tic(); | ||
for ( i = 0; i < b.iterations; i++ ) { | ||
y = values[ i%values.length ]; | ||
z = nanmin( x, y ); | ||
if ( isnan( z ) ) { | ||
b.fail( 'should not return NaN' ); | ||
} | ||
} | ||
b.toc(); | ||
if ( isnan( z ) ) { | ||
b.fail( 'should not return NaN' ); | ||
} | ||
b.pass( 'benchmark finished' ); | ||
b.end(); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
|
||
{{alias}}( x, y ) | ||
Returns the minimum value. | ||
|
||
If one operand is NaN, the other operand is always returned. | ||
|
||
Parameters | ||
---------- | ||
x: number | ||
First number. | ||
|
||
y: number | ||
Second number. | ||
|
||
Returns | ||
------- | ||
out: number | ||
Minimum value. | ||
|
||
Examples | ||
-------- | ||
> var v = {{alias}}( 3.14, 4.2 ) | ||
3.14 | ||
> v = {{alias}}( 3.14, NaN ) | ||
3.14 | ||
> v = {{alias}}( NaN, 4.2 ) | ||
4.2 | ||
> v = {{alias}}( NaN, NaN ) | ||
NaN | ||
|
||
See Also | ||
-------- | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2024 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
// TypeScript Version: 4.1 | ||
|
||
/** | ||
* Return the minimum value, ignoring NaN. | ||
* | ||
* @param x - first number | ||
* @param y - second number | ||
* @returns minimum value | ||
* | ||
* @example | ||
* var v = min( 3.14, 4.2 ); | ||
* // returns 3.14 | ||
* | ||
* @example | ||
* var v = min( 4.14, NaN ); | ||
* // returns 4.14 | ||
* | ||
* @example | ||
* var v = min( NaN, NaN ); | ||
* // returns NaN | ||
*/ | ||
declare function nanmin( x: number, y: number ): number; | ||
|
||
|
||
// EXPORTS // | ||
|
||
export = nanmin; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2024 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import nanmin = require( './index' ); | ||
|
||
|
||
// TESTS // | ||
|
||
// The function returns a number... | ||
{ | ||
nanmin( 3.0, -0.4 ); // $ExpectType number | ||
} | ||
|
||
// The compiler throws an error if the function is provided non-number arguments... | ||
{ | ||
nanmin( true, 3.0 ); // $ExpectError | ||
nanmin( false, 3.0 ); // $ExpectError | ||
nanmin( [], 3.0 ); // $ExpectError | ||
nanmin( {}, 3.0 ); // $ExpectError | ||
nanmin( 'abc', 3.0 ); // $ExpectError | ||
nanmin( ( x: number ): number => x, 3.0 ); // $ExpectError | ||
|
||
nanmin( 1.2, true ); // $ExpectError | ||
nanmin( 1.2, false ); // $ExpectError | ||
nanmin( 1.2, [] ); // $ExpectError | ||
nanmin( 1.2, {} ); // $ExpectError | ||
nanmin( 1.2, 'abc' ); // $ExpectError | ||
nanmin( 1.2, ( x: number ): number => x ); // $ExpectError | ||
} | ||
|
||
// The compiler throws an error if the function is provided an unsupported number of arguments... | ||
{ | ||
nanmin(); // $ExpectError | ||
nanmin( 3.0 ); // $ExpectError | ||
nanmin( 3.0, 2.0, 1.0 ); // $ExpectError | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2024 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var nanmin = require( './../lib' ); | ||
|
||
var m = nanmin( 3.0, 4.0 ); | ||
// returns 3.0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should be logging each return value to the console. |
||
|
||
m = nanmin( NaN, 4.0 ); | ||
// returns 4.0 | ||
|
||
m = nanmin( 4.0, NaN ); | ||
// returns 4.0 | ||
|
||
m = nanmin( NaN, NaN ); | ||
// returns NaN | ||
|
||
console.log(m); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2024 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
/** | ||
* Return the minimum value, ignoring NaN. | ||
* | ||
* @module @stdlib/math/base/special/nanmin | ||
* | ||
* @example | ||
* var nanmin = require( '@stdlib/math/base/special/nanmin' ); | ||
* | ||
* var v = nanmin( 3.14, 4.2 ); | ||
* // returns 3.14 | ||
* | ||
* v = nanmin( 4.14, NaN ); | ||
* // returns 4.14 | ||
* | ||
* v = nanmin( NaN, NaN ); | ||
* // returns NaN | ||
*/ | ||
|
||
// MODULES // | ||
|
||
var nanmin = require( './main.js' ); | ||
|
||
|
||
// EXPORTS // | ||
|
||
module.exports = nanmin; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.