cosh.js (2425B)
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, long comment, copyright, license, and constants are from [Cephes]{@link http://www.netlib.org/cephes}. The implementation follows the original, but has been modified for JavaScript. 22 * 23 * ```text 24 * Copyright 1985, 1995, 2000 by Stephen L. Moshier 25 * 26 * Some software in this archive may be from the book _Methods and Programs for Mathematical Functions_ (Prentice-Hall or Simon & Schuster International, 1989) or from the Cephes Mathematical Library, a commercial product. In either event, it is copyrighted by the author. What you see here may be used freely but it comes with no support or guarantee. 27 * 28 * Stephen L. Moshier 29 * moshier@na-net.ornl.gov 30 * ``` 31 */ 32 33 'use strict'; 34 35 // MODULES // 36 37 var isnan = require( './../../../../base/assert/is-nan' ); 38 var exp = require( './../../../../base/special/exp' ); 39 40 41 // MAIN // 42 43 /** 44 * Computes the hyperbolic cosine of a number. 45 * 46 * ## Method 47 * 48 * ```tex 49 * \operatorname{cosh}(x) = \frac{ \exp(x) + \exp(-x) }{2} 50 * ``` 51 * 52 * ## Notes 53 * 54 * - Relative error: 55 * 56 * | arithmetic | domain | # trials | peak | rms | 57 * |:----------:|:--------:|:--------:|:-------:|:-------:| 58 * | DEC | +- 88 | 50000 | 4.0e-17 | 7.7e-18 | 59 * | IEEE | +-MAXLOG | 30000 | 2.6e-16 | 5.7e-17 | 60 * 61 * 62 * @param {number} x - input value (in radians) 63 * @returns {number} hyperbolic cosine 64 * 65 * @example 66 * var v = cosh( 0.0 ); 67 * // returns 1.0 68 * 69 * @example 70 * var v = cosh( 2.0 ); 71 * // returns ~3.762 72 * 73 * @example 74 * var v = cosh( -2.0 ); 75 * // returns ~3.762 76 * 77 * @example 78 * var v = cosh( NaN ); 79 * // returns NaN 80 */ 81 function cosh( x ) { 82 if ( isnan( x ) ) { 83 return x; 84 } 85 if ( x < 0.0 ) { 86 x = -x; 87 } 88 if ( x > 21.0 ) { 89 return exp( x ) / 2.0; 90 } 91 return ( exp(x) + exp(-x) ) / 2.0; 92 } 93 94 95 // EXPORTS // 96 97 module.exports = cosh;