cosm1.js (2183B)
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 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 cos = require( './../../../../base/special/cos' ); 38 var polyval = require( './polyval_p.js' ); 39 40 41 // VARIABLES // 42 43 var PIO4 = 7.85398163397448309616e-1; // 4/π 44 45 46 // MAIN // 47 48 /** 49 * Computes the cosine of a number minus one. 50 * 51 * @param {number} x - input value (in radians) 52 * @returns {number} cosine minus one 53 * 54 * @example 55 * var v = cosm1( 0.0 ); 56 * // returns 0.0 57 * 58 * @example 59 * var PI = require( '@stdlib/constants/float64/pi' ); 60 * 61 * var v = cosm1( PI/4.0 ); 62 * // returns ~-0.293 63 * 64 * @example 65 * var PI = require( '@stdlib/constants/float64/pi' ); 66 * 67 * var v = cosm1( -PI/6.0 ); 68 * // returns ~-0.134 69 * 70 * @example 71 * var v = cosm1( NaN ); 72 * // returns NaN 73 */ 74 function cosm1( x ) { 75 var x2; 76 if ( x < -PIO4 || x > PIO4 ) { 77 return cos( x ) - 1.0; 78 } 79 x2 = x * x; 80 return ( -0.5*x2 ) + ( x2*x2*polyval( x2 ) ); 81 } 82 83 84 // EXPORTS // 85 86 module.exports = cosm1;