Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5003x 5003x 5003x 5003x 5003x 5003x 5003x 5003x 5003x 2x 2x 5003x 1x 1x 5003x 5003x 5000x 5003x 3001x 5003x 1002x 1002x 3998x 3998x 3998x 3998x 3998x 3998x 3998x 5003x 1x 1x 1x 1x 1x | /** * @license Apache-2.0 * * Copyright (c) 2025 The Stdlib Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * * ## Notice * * The following copyrights, licenses, and long comment were part of the original implementation available as part of{@link https://svnweb.freebsd.org/base/release/12.2.0/lib/msun/src/e_expf.c?view=markup}, The implementation follows the original, but has been modified for JavaScript. * * e_expf.c -- float version of e_exp.c. * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. * ==================================================== * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. * Developed at SunPro, a Sun Microsystems, Inc. business. * Permission to use, copy, modify, and distribute this * software is freely granted, provided that this notice * is preserved. * ==================================================== */ 'use strict'; // MODULES // var isnan = require( '@stdlib/math/base/assert/is-nan' ); var truncf = require( '@stdlib/math/base/special/truncf' ); var NINF = require( '@stdlib/constants/float32/ninf' ); var PINF = require( '@stdlib/constants/float32/pinf' ); var expmulti = require( './expmulti.js' ); // VARIABLES // var LN2_HI = 6.9314575195e-01; var LN2_LO = 1.4286067653e-06; var halF = [ 0.5, -0.5 ]; var INVLN2 = 1.4426950216e+00; var OVERFLOW = 8.8721679688e+01; var UNDERFLOW = -1.0397208405e+02; var NEARZERO = 1.0 / (1 << 14); var NEG_NEARZERO = -NEARZERO; // MAIN // /** * Evaluates the natural exponential function as a single-precision floating-point number. * * ## Method * * 1. We reduce \\( x \\) to an \\( r \\) so that \\( |r| \leq 0.5 \cdot \ln(2) \approx 0.34658 \\). Given \\( x \\), we find an \\( r \\) and integer \\( k \\) such that * * ```tex * \begin{align*} * x &= k \cdot \ln(2) + r \\ * |r| &\leq 0.5 \cdot \ln(2) * \end{align*} * ``` * * <!-- <note> --> * * \\( r \\) can be represented as \\( r = \mathrm{hi} - \mathrm{lo} \\) for better accuracy. * * <!-- </note> --> * * 2. We approximate of \\( e^{r} \\) by a special rational function on the interval \\(\[0,0.34658]\\): * * ```tex * \begin{align*} * R\left(r^2\right) &= r \cdot \frac{ e^{r}+1 }{ e^{r}-1 } \\ * &= 2 + \frac{r^2}{6} - \frac{r^4}{360} + \ldots * \end{align*} * ``` * * We use a special Remes algorithm on \\(\[0,0.34658]\\) to generate a polynomial of degree \\(5\\) to approximate \\(R\\). The maximum error of this polynomial approximation is bounded by \\(2^{-59}\\). In other words, * * ```tex * R(z) \sim 2 + P_1 z + P_2 z^2 + P_3 z^3 + P_4 z^4 + P_5 z^5 * ``` * * where \\( z = r^2 \\) and * * ```tex * \left| 2 + P_1 z + \ldots + P_5 z^5 - R(z) \right| \leq 2^{-59} * ``` * * <!-- <note> --> * * The values of \\( P_1 \\) to \\( P_5 \\) are listed in the source code. * * <!-- </note> --> * * The computation of \\( e^{r} \\) thus becomes * * ```tex * \begin{align*} * e^{r} &= 1 + \frac{2r}{R-r} \\ * &= 1 + r + \frac{r \cdot R_1(r)}{2 - R_1(r)}\ \text{for better accuracy} * \end{align*} * ``` * * where * * ```tex * R_1(r) = r - P_1\ r^2 + P_2\ r^4 + \ldots + P_5\ r^{10} * ``` * * 3. We scale back to obtain \\( e^{x} \\). From step 1, we have * * ```tex * e^{x} = 2^k e^{r} * ``` * * ## Special Cases * * ```tex * \begin{align*} * e^\infty &= \infty \\ * e^{-\infty} &= 0 \\ * e^{\mathrm{NaN}} &= \mathrm{NaN} \\ * e^0 &= 1\ \mathrm{is\ exact\ for\ finite\ argument\ only} * \end{align*} * ``` * * ## Notes * * - The hexadecimal values included in the source code are the intended ones for the used constants. Decimal values may be used, provided that the compiler will convert from decimal to binary accurately enough to produce the intended hexadecimal values. * * @param {number} x - input value * @returns {number} function value * * @example * var v = expf( 4.0 ); * // returns 54.598148345947266 * * @example * var v = expf( -9.0 ); * // returns 0.00012340980174485594 * * @example * var v = expf( 0.0 ); * // returns 1.0 * * @example * var v = expf( NaN ); * // returns NaN */ function expf( x ) { var xsb; var hi; var lo; var k; xsb = ( x < 0.0 ) ? 1 : 0; if ( isnan( x ) || x === PINF ) { return x; } if ( x === NINF ) { return 0.0; } if ( x > OVERFLOW ) { return PINF; } if ( x < UNDERFLOW ) { return 0.0; } if ( x > NEG_NEARZERO && x < NEARZERO ) { return 1.0 + x; } // Argument reduction k = truncf( (INVLN2 * x) + halF[ xsb ] ); hi = x - (k * LN2_HI); lo = k * LN2_LO; return expmulti( hi, lo, k ); } // EXPORTS // module.exports = expf; |