main.js (1723B)
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 expm1 = require( './../../../../base/special/expm1' ); 25 var exp = require( './../../../../base/special/exp' ); 26 var log1p = require( './../../../../base/special/log1p' ); 27 var ln = require( './../../../../base/special/ln' ); 28 var abs = require( './../../../../base/special/abs' ); 29 var LN2 = require( '@stdlib/constants/float64/ln-two' ); 30 var NINF = require( '@stdlib/constants/float64/ninf' ); 31 32 33 // MAIN // 34 35 /** 36 * Computes the natural logarithm of \\( 1-\exp(-|x|) \\). 37 * 38 * @param {number} x - input value 39 * @returns {number} function value 40 * 41 * @example 42 * var v = log1mexp( 1.1 ); 43 * // returns ~-0.40477 44 * 45 * @example 46 * var v = log1mexp( 0.0 ); 47 * // returns -Infinity 48 * 49 * @example 50 * var v = log1mexp( NaN ); 51 * // returns NaN 52 */ 53 function log1mexp( x ) { 54 var ax; 55 if ( isnan( x ) ) { 56 return NaN; 57 } 58 if ( x === 0.0 ) { 59 return NINF; 60 } 61 ax = abs( x ); 62 if ( 0.0 < ax && ax <= LN2 ) { 63 return ln( -expm1( -ax ) ); 64 } 65 // Case: |x| > ln(2) 66 return log1p( -exp( -ax ) ); 67 } 68 69 70 // EXPORTS // 71 72 module.exports = log1mexp;