maxabs.js (1632B)
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 abs = require( './../../../../base/special/abs' ); 24 var max = require( './../../../../base/special/max' ); 25 var PINF = require( '@stdlib/constants/float64/pinf' ); 26 27 28 // MAIN // 29 30 /** 31 * Returns the maximum absolute value. 32 * 33 * @param {number} [x] - first number 34 * @param {number} [y] - second number 35 * @param {...number} [args] - numbers 36 * @returns {number} maximum absolute value 37 * 38 * @example 39 * var v = maxabs( 3.14, -4.2 ); 40 * // returns 4.2 41 * 42 * @example 43 * var v = maxabs( 5.9, 3.14, 4.2 ); 44 * // returns 5.9 45 * 46 * @example 47 * var v = maxabs( 3.14, NaN ); 48 * // returns NaN 49 * 50 * @example 51 * var v = maxabs( +0.0, -0.0 ); 52 * // returns +0.0 53 */ 54 function maxabs( x, y ) { 55 var nargs; 56 var args; 57 var i; 58 59 nargs = arguments.length; 60 if ( nargs === 0 ) { 61 return PINF; 62 } 63 if ( nargs === 2 ) { 64 return max( abs( x ), abs( y ) ); 65 } 66 args = []; 67 for ( i = 0; i < nargs; i++ ) { 68 args.push( abs( arguments[ i ] ) ); 69 } 70 return max.apply( null, args ); 71 } 72 73 74 // EXPORTS // 75 76 module.exports = maxabs;