time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

main.js (1627B)


      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 isnan = require( './../../../../base/assert/is-nan' );
     24 var isInfinite = require( './../../../../base/assert/is-infinite' );
     25 var PINF = require( '@stdlib/constants/float64/pinf' );
     26 var sqrt = require( './../../../../base/special/sqrt' );
     27 
     28 
     29 // MAIN //
     30 
     31 /**
     32 * Computes the hypotenuse avoiding overflow and underflow.
     33 *
     34 * @param {number} x - number
     35 * @param {number} y - number
     36 * @returns {number} hypotenuse
     37 *
     38 * @example
     39 * var h = hypot( -5.0, 12.0 );
     40 * // returns 13.0
     41 *
     42 * @example
     43 * var h = hypot( NaN, 12.0 );
     44 * // returns NaN
     45 *
     46 * @example
     47 * var h = hypot( -0.0, -0.0 );
     48 * // returns 0.0
     49 */
     50 function hypot( x, y ) {
     51 	var tmp;
     52 	if ( isnan( x ) || isnan( y ) ) {
     53 		return NaN;
     54 	}
     55 	if ( isInfinite( x ) || isInfinite( y ) ) {
     56 		return PINF;
     57 	}
     58 	if ( x < 0.0 ) {
     59 		x = -x;
     60 	}
     61 	if ( y < 0.0 ) {
     62 		y = -y;
     63 	}
     64 	if ( x < y ) {
     65 		tmp = y;
     66 		y = x;
     67 		x = tmp;
     68 	}
     69 	if ( x === 0.0 ) {
     70 		return 0.0;
     71 	}
     72 	y /= x;
     73 	return x * sqrt( 1.0 + (y*y) );
     74 }
     75 
     76 
     77 // EXPORTS //
     78 
     79 module.exports = hypot;