factory.js (1834B)
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 hypot1a = require( './hypot1a.js' ); 24 var hypot1b = require( './hypot1b.js' ); 25 var hypot2a = require( './hypot2a.js' ); 26 var hypot2b = require( './hypot2b.js' ); 27 var closure1a = require( './closure1a.js' ); 28 var closure1b = require( './closure1b.js' ); 29 30 31 // MAIN // 32 33 /** 34 * Returns a function to compute a hypotenuse using the alpha max plus beta min algorithm. 35 * 36 * @param {number} alpha - alpha 37 * @param {number} beta - beta 38 * @param {boolean} [nonnegative] - boolean indicating whether input values are always nonnegative 39 * @param {boolean} [ints] - boolean indicating whether input values are always 32-bit integers 40 * @returns {Function} function to compute a hypotenuse 41 * 42 * @example 43 * var hypot = factory( 1.0, 0.5 ); 44 * // returns <Function> 45 */ 46 function factory( alpha, beta, nonnegative, ints ) { 47 if ( ints ) { 48 if ( alpha === 1.0 && beta === 0.5 ) { 49 if ( nonnegative ) { 50 return hypot1a; 51 } 52 return hypot1b; 53 } 54 if ( alpha === 1.0 && beta === 0.25 ) { 55 if ( nonnegative ) { 56 return hypot2a; 57 } 58 return hypot2b; 59 } 60 } 61 if ( nonnegative ) { 62 return closure1a( alpha, beta ); 63 } 64 return closure1b( alpha, beta ); 65 } 66 67 68 // EXPORTS // 69 70 module.exports = factory;