time-to-botec

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

log1pexp.js (2532B)


      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 following copyright and license were part of the original implementation available as part of the [StatsFuns.jl]{@link https://github.com/JuliaStats/StatsFuns.jl/blob/master/src/basicfuns.jl} Julia package. The implementation follows the original, but has been modified for JavaScript.
     22 *
     23 * ```text
     24 * Copyright (c) 2015: Dahua Lin.
     25 *
     26 * Permission is hereby granted, free of charge, to any person obtaining
     27 * a copy of this software and associated documentation files (the
     28 * "Software"), to deal in the Software without restriction, including
     29 * without limitation the rights to use, copy, modify, merge, publish,
     30 * distribute, sublicense, and/or sell copies of the Software, and to
     31 * permit persons to whom the Software is furnished to do so, subject to
     32 * the following conditions:
     33 *
     34 * The above copyright notice and this permission notice shall be
     35 * included in all copies or substantial portions of the Software.
     36 *
     37 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     38 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     39 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
     40 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
     41 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
     42 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
     43 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     44 * ```
     45 */
     46 
     47 'use strict';
     48 
     49 // MODULES //
     50 
     51 var log1p = require( '@stdlib/math/base/special/log1p' );
     52 var exp = require( '@stdlib/math/base/special/exp' );
     53 
     54 
     55 // MAIN //
     56 
     57 /**
     58 * Compute ln( 1 + exp(x) ) without overflow.
     59 *
     60 * @private
     61 * @param {number} x - input value
     62 * @returns {number} function value
     63 */
     64 function log1pexp( x ) {
     65 	if ( x <= 18.0 ) {
     66 		return log1p( exp(x) );
     67 	}
     68 	if ( x > 33.3 ) {
     69 		return x;
     70 	}
     71 	// Case: 18.0 < x <= 33.3
     72 	return x + exp( -x );
     73 }
     74 
     75 
     76 // EXPORTS //
     77 
     78 module.exports = log1pexp;