minmax.js (2820B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2018 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 isNegativeZero = require( './../../../../base/assert/is-negative-zero' ); 24 var isPositiveZero = require( './../../../../base/assert/is-positive-zero' ); 25 var isnan = require( './../../../../base/assert/is-nan' ); 26 var NINF = require( '@stdlib/constants/float64/ninf' ); 27 var PINF = require( '@stdlib/constants/float64/pinf' ); 28 29 30 // MAIN // 31 32 /** 33 * Returns the minimum and maximum values. 34 * 35 * @private 36 * @param {(Array|TypedArray|Object)} out - output object 37 * @param {number} x - first number 38 * @param {number} [y] - second number 39 * @param {...number} [args] - numbers 40 * @returns {(Array|TypedArray|Object)} minimum and maximum values 41 * 42 * @example 43 * var out = [ 0.0, 0.0 ]; 44 * var v = minmax( out, 3.14, 4.2 ); 45 * // returns [ 3.14, 4.2 ] 46 * 47 * var bool = ( v === out ); 48 * // returns true 49 * 50 * @example 51 * var out = [ 0.0, 0.0 ]; 52 * var v = minmax( out, 5.9, 3.14, 4.2 ); 53 * // returns [ 3.14, 5.9 ] 54 * 55 * @example 56 * var out = [ 0.0, 0.0 ]; 57 * var v = minmax( out, 3.14, NaN ); 58 * // returns [ NaN, NaN ] 59 * 60 * @example 61 * var out = [ 0.0, 0.0 ]; 62 * var v = minmax( out, +0.0, -0.0 ); 63 * // returns [ -0.0, 0.0 ] 64 */ 65 function minmax( out, x, y ) { 66 var len; 67 var min; 68 var max; 69 var v; 70 var i; 71 72 len = arguments.length; 73 if ( len === 2 ) { 74 out[ 0 ] = x; 75 out[ 1 ] = x; 76 return out; 77 } 78 if ( len === 3 ) { 79 if ( isnan( x ) || isnan( y ) ) { 80 out[ 0 ] = NaN; 81 out[ 1 ] = NaN; 82 return out; 83 } 84 if ( x === y && x === 0.0 ) { 85 if ( isNegativeZero( x ) ) { 86 out[ 0 ] = x; 87 out[ 1 ] = y; 88 return out; 89 } 90 out[ 0 ] = y; 91 out[ 1 ] = x; 92 return out; 93 } 94 if ( x < y ) { 95 out[ 0 ] = x; 96 out[ 1 ] = y; 97 return out; 98 } 99 out[ 0 ] = y; 100 out[ 1 ] = x; 101 return out; 102 } 103 min = PINF; 104 max = NINF; 105 for ( i = 0; i < len; i++ ) { 106 v = arguments[ i ]; 107 if ( isnan( v ) ) { 108 out[ 0 ] = NaN; 109 out[ 1 ] = NaN; 110 return out; 111 } 112 if ( v < min ) { 113 min = v; 114 } else if ( 115 v === 0.0 && 116 v === min && 117 isNegativeZero( v ) 118 ) { 119 min = v; 120 } 121 if ( v > max ) { 122 max = v; 123 } else if ( 124 v === 0.0 && 125 v === max && 126 isPositiveZero( v ) 127 ) { 128 max = v; 129 } 130 } 131 out[ 0 ] = min; 132 out[ 1 ] = max; 133 return out; 134 } 135 136 137 // EXPORTS // 138 139 module.exports = minmax;