exp2.js (3483B)
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 // TODO: replace with TOMS (Openlibm) algo (updating license header and long comment) 36 37 // MODULES // 38 39 var FLOAT64_MAX_BASE2_EXPONENT = require( '@stdlib/constants/float64/max-base2-exponent' ); // eslint-disable-line id-length 40 var FLOAT64_MIN_BASE2_EXPONENT = require( '@stdlib/constants/float64/min-base2-exponent' ); // eslint-disable-line id-length 41 var round = require( './../../../../base/special/round' ); 42 var ldexp = require( './../../../../base/special/ldexp' ); 43 var isnan = require( './../../../../base/assert/is-nan' ); 44 var PINF = require( '@stdlib/constants/float64/pinf' ); 45 var polyvalP = require( './polyval_p.js' ); 46 var polyvalQ = require( './polyval_q.js' ); 47 48 49 // MAIN // 50 51 /** 52 * Evaluates the base `2` exponential function. 53 * 54 * ## Method 55 * 56 * - Range reduction is accomplished by separating the argument into an integer \\( k \\) and fraction \\( f \\) such that 57 * 58 * ```tex 59 * 2^x = 2^k 2^f 60 * ``` 61 * 62 * - A Pade' approximate 63 * 64 * ```tex 65 * 1 + 2x \frac{\mathrm{P}\left(x^2\right)}{\mathrm{Q}\left(x^2\right) - x \mathrm{P}\left(x^2\right)} 66 * ``` 67 * 68 * approximates \\( 2^x \\) in the basic range \\( \[-0.5, 0.5] \\). 69 * 70 * 71 * ## Notes 72 * 73 * - Relative error: 74 * 75 * | arithmetic | domain | # trials | peak | rms | 76 * |:----------:|:-----------:|:--------:|:-------:|:-------:| 77 * | IEEE | -1022,+1024 | 30000 | 1.8e-16 | 5.4e-17 | 78 * 79 * 80 * @param {number} x - input value 81 * @returns {number} function value 82 * 83 * @example 84 * var v = exp2( 3.0 ); 85 * // returns 8.0 86 * 87 * @example 88 * var v = exp2( -9.0 ); 89 * // returns ~0.002 90 * 91 * @example 92 * var v = exp2( 0.0 ); 93 * // returns 1.0 94 * 95 * @example 96 * var v = exp2( NaN ); 97 * // returns NaN 98 */ 99 function exp2( x ) { 100 var px; 101 var xx; 102 var n; 103 if ( isnan( x ) ) { 104 return x; 105 } 106 if ( x > FLOAT64_MAX_BASE2_EXPONENT ) { 107 return PINF; 108 } 109 if ( x < FLOAT64_MIN_BASE2_EXPONENT ) { 110 return 0.0; 111 } 112 // Separate into integer and fractional parts... 113 n = round( x ); 114 x -= n; 115 116 xx = x * x; 117 px = x * polyvalP( xx ); 118 x = px / ( polyvalQ( xx ) - px ); 119 x = 1.0 + ldexp( x, 1 ); 120 121 // Scale by power of 2: 122 return ldexp( x, n ); 123 } 124 125 126 // EXPORTS // 127 128 module.exports = exp2;