diff --git a/CHANGELOG.md b/CHANGELOG.md index 1692ab33c..3a1df8257 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3696,6 +3696,28 @@ This release closes the following issue: +
+ +#### [@stdlib/math/base/special/nanmax](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/nanmax) + +
+ +
+ +##### Features + +- [`bffee89`](https://github.com/stdlib-js/stdlib/commit/bffee891fd783eebc475e8ecefa035025df7f2dd) - add `math/base/special/nanmax` + +
+ + + +
+ +
+ + +
#### [@stdlib/math/base/special/nanmin](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/nanmin) @@ -4464,6 +4486,7 @@ A total of 26 people contributed to this release. Thank you to the following con
+- [`bffee89`](https://github.com/stdlib-js/stdlib/commit/bffee891fd783eebc475e8ecefa035025df7f2dd) - **feat:** add `math/base/special/nanmax` _(by Ridam Garg, stdlib-bot, Philipp Burckhardt)_ - [`46ff6c1`](https://github.com/stdlib-js/stdlib/commit/46ff6c17fe80cb1a7e153ac1fc728d6dab2195cf) - **docs:** update description per conventions _(by Philipp Burckhardt)_ - [`29fbbb6`](https://github.com/stdlib-js/stdlib/commit/29fbbb62d94cb2af1d8856004b75c03daf942053) - **chore:** minor clean-up _(by Philipp Burckhardt)_ - [`fb04f19`](https://github.com/stdlib-js/stdlib/commit/fb04f1968006f8bee36735f9b71d857f478bb6c1) - **refactor:** reduce test tolerance and fix casting [(#2356)](https://github.com/stdlib-js/stdlib/pull/2356) _(by Gunj Joshi)_ diff --git a/base/special/nanmax/README.md b/base/special/nanmax/README.md new file mode 100644 index 000000000..71d3e2e86 --- /dev/null +++ b/base/special/nanmax/README.md @@ -0,0 +1,100 @@ + + +# nanmax + +> Return the maximum value, ignoring NaN. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var nanmax = require( '@stdlib/math/base/special/nanmax' ); +``` + +#### nanmax( x, y ) + +Returns the maximum value. + +```javascript +var v = nanmax( 4.2, 3.14 ); +// returns 4.2 + +v = nanmax( +0.0, -0.0 ); +// returns +0.0 +``` + +If any argument is `NaN`, the function returns the other operand. + +```javascript +var v = nanmax( 4.2, NaN ); +// returns 4.2 + +v = nanmax( NaN, 3.14 ); +// returns 3.14 +``` + +If both arguments are `NaN`, the function returns `NaN`. + +```javascript +var v = nanmax( NaN, NaN ); +// returns NaN +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + diff --git a/base/special/nanmax/benchmark/benchmark.js b/base/special/nanmax/benchmark/benchmark.js new file mode 100644 index 000000000..e0e94e9e6 --- /dev/null +++ b/base/special/nanmax/benchmark/benchmark.js @@ -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( './../../../../base/assert/is-nan' ); +var pkg = require( './../package.json' ).name; +var nanmax = 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 = nanmax( 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(); +}); diff --git a/base/special/nanmax/docs/types/index.d.ts b/base/special/nanmax/docs/types/index.d.ts new file mode 100644 index 000000000..ff74e7a9e --- /dev/null +++ b/base/special/nanmax/docs/types/index.d.ts @@ -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 maximum value, ignoring NaN. +* +* @param x - first number +* @param y - second number +* @returns maximum value +* +* @example +* var v = nanmax( 3.14, 4.2 ); +* // returns 4.2 +* +* @example +* var v = nanmax( 4.14, NaN ); +* // returns 4.14 +* +* @example +* var v = nanmax( NaN, NaN ); +* // returns NaN +*/ +declare function nanmax( x: number, y: number ): number; + + +// EXPORTS // + +export = nanmax; diff --git a/base/special/nanmax/docs/types/repl.txt b/base/special/nanmax/docs/types/repl.txt new file mode 100644 index 000000000..fd57db814 --- /dev/null +++ b/base/special/nanmax/docs/types/repl.txt @@ -0,0 +1,32 @@ + +{{alias}}( x, y ) + Returns the maximum value. + + If one operand is NaN, the other operand is always returned. + + Parameters + ---------- + x: number + First number. + + y: number + Second number. + + Returns + ------- + out: number + Maximum value. + + Examples + -------- + > var v = {{alias}}( 3.14, 4.2 ) + 4.2 + > v = {{alias}}( 3.14, NaN ) + 3.14 + > v = {{alias}}( NaN, 4.2 ) + 4.2 + > v = {{alias}}( NaN, NaN ) + NaN + + See Also + -------- diff --git a/base/special/nanmax/docs/types/test.ts b/base/special/nanmax/docs/types/test.ts new file mode 100644 index 000000000..78e080627 --- /dev/null +++ b/base/special/nanmax/docs/types/test.ts @@ -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 nanmax = require( './index' ); + + +// TESTS // + +// The function returns a number... +{ + nanmax( 3.0, -0.4 ); // $ExpectType number +} + +// The compiler throws an error if the function is provided non-number arguments... +{ + nanmax( true, 3.0 ); // $ExpectError + nanmax( false, 3.0 ); // $ExpectError + nanmax( [], 3.0 ); // $ExpectError + nanmax( {}, 3.0 ); // $ExpectError + nanmax( 'abc', 3.0 ); // $ExpectError + nanmax( ( x: number ): number => x, 3.0 ); // $ExpectError + + nanmax( 1.2, true ); // $ExpectError + nanmax( 1.2, false ); // $ExpectError + nanmax( 1.2, [] ); // $ExpectError + nanmax( 1.2, {} ); // $ExpectError + nanmax( 1.2, 'abc' ); // $ExpectError + nanmax( 1.2, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + nanmax(); // $ExpectError + nanmax( 3.0 ); // $ExpectError + nanmax( 3.0, 2.0, 1.0 ); // $ExpectError +} diff --git a/base/special/nanmax/examples/index.js b/base/special/nanmax/examples/index.js new file mode 100644 index 000000000..fdc34abf5 --- /dev/null +++ b/base/special/nanmax/examples/index.js @@ -0,0 +1,37 @@ +/** +* @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 nanmax = require( './../lib' ); + +var m = nanmax( 3.0, 4.0 ); +console.log( m ); +// => 4.0 + +m = nanmax( NaN, 4.0 ); +console.log( m ); +// => 4.0 + +m = nanmax( 4.0, NaN ); +console.log( m ); +// => 4.0 + +m = nanmax( NaN, NaN ); +console.log( m ); +// => NaN diff --git a/base/special/nanmax/lib/index.js b/base/special/nanmax/lib/index.js new file mode 100644 index 000000000..b11c08d56 --- /dev/null +++ b/base/special/nanmax/lib/index.js @@ -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 maximum value, ignoring NaN. +* +* @module @stdlib/math/base/special/nanmax +* +* @example +* var nanmax = require( '@stdlib/math/base/special/nanmax' ); +* +* var v = nanmax( 3.14, 4.2 ); +* // returns 4.2 +* +* v = nanmax( 4.14, NaN ); +* // returns 4.14 +* +* v = nanmax( NaN, NaN ); +* // returns NaN +*/ + +// MODULES // + +var nanmax = require( './main.js' ); + + +// EXPORTS // + +module.exports = nanmax; diff --git a/base/special/nanmax/lib/main.js b/base/special/nanmax/lib/main.js new file mode 100644 index 000000000..7d2ee7ac4 --- /dev/null +++ b/base/special/nanmax/lib/main.js @@ -0,0 +1,58 @@ +/** +* @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 max = require( './../../../../base/special/max'); +var isnan = require( './../../../../base/assert/is-nan'); + + +// MAIN // + +/** +* Returns the maximum value, ignoring NaN. +* +* @param {number} x - first number +* @param {number} y - second number +* @returns {number} maximum value +* +* @example +* var v = nanmax( 3.14, 4.2 ); +* // returns 4.2 +* +* @example +* var v = nanmax( 4.14, NaN ); +* // returns 4.14 +* +* @example +* var v = nanmax( NaN, NaN); +* // returns NaN +*/ +function nanmax( x, y ) { + if ( isnan( x ) ) { + return ( isnan( y ) ) ? NaN : y; + } + return ( isnan( y ) ) ? x : max( x, y ); +} + + +// EXPORTS // + +module.exports = nanmax; diff --git a/base/special/nanmax/package.json b/base/special/nanmax/package.json new file mode 100644 index 000000000..b11a4535a --- /dev/null +++ b/base/special/nanmax/package.json @@ -0,0 +1,61 @@ +{ + "name": "@stdlib/math/base/special/nanmax", + "version": "0.0.0", + "description": "Return the maximum value, ignoring NaN.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdmath", + "mathematics", + "math", + "math.max", + "maximum", + "max", + "smallest" + ] +} diff --git a/base/special/nanmax/test/test.js b/base/special/nanmax/test/test.js new file mode 100644 index 000000000..5c1669fb6 --- /dev/null +++ b/base/special/nanmax/test/test.js @@ -0,0 +1,66 @@ +/** +* @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 tape = require( 'tape' ); +var isnan = require( './../../../../base/assert/is-nan' ); +var nanmax = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof nanmax, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `NaN` if both operands are `NaN`', function test( t ) { + var v; + + v = nanmax( NaN, NaN ); + t.strictEqual( isnan( v ), true, 'returns NaN value' ); + t.end(); +}); + +tape( 'the function returns the non-NaN value if one of the operands is `NaN`', function test( t ) { + var v; + + v = nanmax( NaN, 3.14 ); + t.strictEqual( v, 3.14, 'returns not NaN value' ); + + v = nanmax( 4.2, NaN ); + t.strictEqual( v, 4.2, 'returns not NaN value' ); + + t.end(); +}); + +tape( 'the function returns the maximum value', function test( t ) { + var v; + + v = nanmax( 5.2, 3.14 ); + t.strictEqual( v, 5.2, 'returns max value' ); + + v = nanmax( -4.2, 3.14 ); + t.strictEqual( v, 3.14, 'returns max value' ); + + t.end(); +});