time-to-botec

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

sqrt1pm1.js (2182B)


      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 * ## Notice
     20 *
     21 * The original C++ code and copyright notice are from the [Boost library]{@link https://github.com/boostorg/math/blob/fa1896fbc4c6fadc167307342ceb20bf2b6c0688/include/boost/math/special_functions/sqrt1pm1.hpp}. This implementation follows the original, but has been modified for JavaScript.
     22 *
     23 * ```text
     24 * (C) Copyright John Maddock 2006.
     25 *
     26 * Use, modification and distribution are subject to the
     27 * Boost Software License, Version 1.0. (See accompanying file
     28 * LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
     29 * ```
     30 */
     31 
     32 'use strict';
     33 
     34 // MODULES //
     35 
     36 var isnan = require( './../../../../base/assert/is-nan' );
     37 var expm1 = require( './../../../../base/special/expm1' );
     38 var log1p = require( './../../../../base/special/log1p' );
     39 var sqrt = require( './../../../../base/special/sqrt' );
     40 var abs = require( './../../../../base/special/abs' );
     41 
     42 
     43 // MAIN //
     44 
     45 /**
     46 * Computes the value of `sqrt(1+x)-1`.
     47 *
     48 * @param {number} x - input value
     49 * @returns {number} square root of `1+x` minus one
     50 *
     51 * @example
     52 * var v = sqrt1pm1( 3.0 );
     53 * // returns 1.0
     54 *
     55 * @example
     56 * var v = sqrt1pm1( 0.5 );
     57 * // returns ~0.225
     58 *
     59 * @example
     60 * var v = sqrt1pm1( 0.02 );
     61 * // returns ~0.01
     62 *
     63 * @example
     64 * var v = sqrt1pm1( -0.5 );
     65 * // returns ~-0.293
     66 *
     67 * @example
     68 * var v = sqrt1pm1( -1.1 );
     69 * // returns NaN
     70 *
     71 * @example
     72 * var v = sqrt1pm1( NaN );
     73 * // returns NaN
     74 */
     75 function sqrt1pm1( x ) {
     76 	if ( isnan( x ) ) {
     77 		return NaN;
     78 	}
     79 	if ( abs( x ) > 0.75 ) {
     80 		return sqrt( 1.0+x ) - 1.0;
     81 	}
     82 	return expm1( log1p( x ) / 2.0 );
     83 }
     84 
     85 
     86 // EXPORTS //
     87 
     88 module.exports = sqrt1pm1;