max.js (1984B)
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 isPositiveZero = require( './../../../../base/assert/is-positive-zero' ); 24 var isnan = require( './../../../../base/assert/is-nan' ); 25 var NINF = require( '@stdlib/constants/float64/ninf' ); 26 var PINF = require( '@stdlib/constants/float64/pinf' ); 27 28 29 // MAIN // 30 31 /** 32 * Returns the maximum value. 33 * 34 * @param {number} [x] - first number 35 * @param {number} [y] - second number 36 * @param {...number} [args] - numbers 37 * @returns {number} maximum value 38 * 39 * @example 40 * var v = max( 3.14, 4.2 ); 41 * // returns 4.2 42 * 43 * @example 44 * var v = max( 5.9, 3.14, 4.2 ); 45 * // returns 5.9 46 * 47 * @example 48 * var v = max( 3.14, NaN ); 49 * // returns NaN 50 * 51 * @example 52 * var v = max( +0.0, -0.0 ); 53 * // returns +0.0 54 */ 55 function max( x, y ) { 56 var len; 57 var m; 58 var v; 59 var i; 60 61 len = arguments.length; 62 if ( len === 2 ) { 63 if ( isnan( x ) || isnan( y ) ) { 64 return NaN; 65 } 66 if ( x === PINF || y === PINF ) { 67 return PINF; 68 } 69 if ( x === y && x === 0.0 ) { 70 if ( isPositiveZero( x ) ) { 71 return x; 72 } 73 return y; 74 } 75 if ( x > y ) { 76 return x; 77 } 78 return y; 79 } 80 m = NINF; 81 for ( i = 0; i < len; i++ ) { 82 v = arguments[ i ]; 83 if ( isnan( v ) || v === PINF ) { 84 return v; 85 } 86 if ( v > m ) { 87 m = v; 88 } else if ( 89 v === m && 90 v === 0.0 && 91 isPositiveZero( v ) 92 ) { 93 m = v; 94 } 95 } 96 return m; 97 } 98 99 100 // EXPORTS // 101 102 module.exports = max;