acos.js (3088B)
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 1984, 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 asin = require( './../../../../base/special/asin' ); 39 var sqrt = require( './../../../../base/special/sqrt' ); 40 var PIO4 = require( '@stdlib/constants/float64/fourth-pi' ); 41 42 43 // VARIABLES // 44 45 var MOREBITS = 6.123233995736765886130e-17; // pi/2 = PIO2 + MOREBITS. 46 47 48 // MAIN // 49 50 /** 51 * Computes the arccosine of a number. 52 * 53 * ## Method 54 * 55 * - Analytically, 56 * 57 * ```tex 58 * \operatorname{acos}(x) = \frac{\pi}{2} - \operatorname{asin}(x) 59 * ``` 60 * 61 * However, if \\(\|x\|\\) is near \\(1\\), there is cancellation error in subtracting \\(\opertorname{asin}(x)\\) from \\(\pi/2\\). Hence, if \\(x < -0.5\\), 62 * 63 * ```tex 64 * \operatorname{acos}(x) = \pi - 2.0 \cdot \operatorname{asin}(\sqrt{(1+x)/2}) 65 * ``` 66 * 67 * or, if \\(x > +0.5\\), 68 * 69 * ```tex 70 * \operatorname{acos}(x) = 2.0 \cdot \operatorname{asin}( \sqrt{(1-x)/2} )} 71 * ``` 72 * 73 * ## Notes 74 * 75 * - Relative error: 76 * 77 * | arithmetic | domain | # trials | peak | rms | 78 * |:-----------|:------:|:---------|:--------|:--------| 79 * | DEC | -1, 1 | 50000 | 3.3e-17 | 8.2e-18 | 80 * | IEEE | -1, 1 | 10^6 | 2.2e-16 | 6.5e-17 | 81 * 82 * 83 * @param {number} x - input value 84 * @returns {number} arccosine (in radians) 85 * 86 * @example 87 * var v = acos( 1.0 ); 88 * // returns 0.0 89 * 90 * @example 91 * var v = acos( 0.707 ); // ~pi/4 92 * // returns ~0.7855 93 * 94 * @example 95 * var v = acos( NaN ); 96 * // returns NaN 97 */ 98 function acos( x ) { 99 var z; 100 if ( isnan( x ) ) { 101 return NaN; 102 } 103 if ( x < -1.0 || x > 1.0 ) { 104 return NaN; 105 } 106 if ( x > 0.5 ) { 107 return 2.0 * asin( sqrt( 0.5 - (0.5*x) ) ); 108 } 109 z = PIO4 - asin( x ); 110 z += MOREBITS; 111 z += PIO4; 112 return z; 113 } 114 115 116 // EXPORTS // 117 118 module.exports = acos;