time-to-botec

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

main.js (1865B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2020 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 float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' );
     24 var isnanf = require( './../../../../base/assert/is-nanf' );
     25 var isInfinitef = require( './../../../../base/assert/is-infinitef' );
     26 var PINF = require( '@stdlib/constants/float32/pinf' );
     27 var sqrt = require( './../../../../base/special/sqrt' );
     28 
     29 
     30 // MAIN //
     31 
     32 /**
     33 * Computes the hypotenuse avoiding overflow and underflow (single-precision).
     34 *
     35 * @param {number} x - number
     36 * @param {number} y - number
     37 * @returns {number} hypotenuse
     38 *
     39 * @example
     40 * var h = hypotf( -5.0, 12.0 );
     41 * // returns 13.0
     42 *
     43 * @example
     44 * var h = hypotf( NaN, 12.0 );
     45 * // returns NaN
     46 *
     47 * @example
     48 * var h = hypotf( -0.0, -0.0 );
     49 * // returns 0.0
     50 */
     51 function hypotf( x, y ) {
     52 	var tmp;
     53 	if ( isnanf( x ) || isnanf( y ) ) {
     54 		return NaN;
     55 	}
     56 	if ( isInfinitef( x ) || isInfinitef( y ) ) {
     57 		return PINF;
     58 	}
     59 	if ( x < 0.0 ) {
     60 		x = -x;
     61 	}
     62 	if ( y < 0.0 ) {
     63 		y = -y;
     64 	}
     65 	if ( x < y ) {
     66 		tmp = y;
     67 		y = x;
     68 		x = tmp;
     69 	}
     70 	if ( x === 0.0 ) {
     71 		return 0.0;
     72 	}
     73 	y = float64ToFloat32( y / x );
     74 	return float64ToFloat32( x * float64ToFloat32( sqrt( float64ToFloat32( 1.0 + float64ToFloat32(y*y) ) ) ) ); // eslint-disable-line max-len
     75 }
     76 
     77 
     78 // EXPORTS //
     79 
     80 module.exports = hypotf;