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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | 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 5009x 5009x 5009x 5009x 5009x 5009x 5009x 5009x 5009x 5009x 5009x 5009x 5009x 5009x 5009x 5009x 5009x 5009x 5009x 5009x 5009x 5009x 1595x 6x 1x 1x 6x 2x 2x 2x 6x 2x 2x 6x 1595x 796x 796x 1595x 4208x 4208x 5009x 2512x 1308x 654x 654x 654x 654x 654x 654x 654x 654x 2512x 1204x 999x 1204x 205x 205x 1204x 1204x 1204x 1204x 1204x 2512x 2512x 5009x 1000x 1000x 1000x 1696x 696x 696x 3208x 3208x 3208x 3208x 3208x 3208x 3208x 5009x 696x 696x 2512x 2512x 2512x 5009x 654x 654x 5009x 654x 96x 96x 558x 558x 5009x 761x 761x 761x 761x 761x 761x 761x 761x 443x 5009x 169x 169x 169x 5009x 274x 274x 274x 274x 274x 443x 5009x 1x 1x 1x 1x 1x | /**
* @license Apache-2.0
*
* Copyright (c) 2026 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 copyright, license, and long comment were part of the original implementation available as part of [FDLIBM]{@link http://www.netlib.org/fdlibm/s_expm1.c} and [FreeBSD]{@link https://github.com/freebsd/freebsd-src/blob/main/lib/msun/src/s_expm1f.c}. The implementation follows the original, but has been modified for JavaScript.
*
* ```text
* 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.
* ```
*/
/* eslint-disable stdlib/jsdoc-list-item-spacing */
'use strict';
// MODULES //
var toWordf = require( '@stdlib/number/float32/base/to-word' );
var fromWordf = require( '@stdlib/number/float32/base/from-word' );
var f32 = require( '@stdlib/number/float64/base/to-float32' );
var FLOAT32_EXPONENT_BIAS = require( '@stdlib/constants/float32/exponent-bias' );
// VARIABLES //
var OVERFLOW_THRESHOLD = 8.8721679688e+01; // 0x42b17180
var LN2_HI = 6.9313812256e-01; // 0x3f317180
var LN2_LO = 9.0580006145e-06; // 0x3717f7d1
var INV_LN2 = 1.4426950216e+00; // 0x3fb8aa3b
var Q1 = -3.3333212137e-02; // -0x888868.0p-28
var Q2 = 1.5807170421e-03; // 0xcf3010.0p-33
var TINY = 1.0e-30;
// MAIN //
/**
* Computes `exp(x) - 1` for a single-precision floating-point number.
*
* ## Method
*
* 1. Given \\(x\\), we use argument reduction to find \\(r\\) and an integer \\(k\\) such that
*
* ```tex
* x = k \cdot \ln(2) + r
* ```
*
* where
*
* ```tex
* |r| \leq \frac{\ln(2)}{2} \approx 0.34658
* ```
*
* <!-- <note> -->
*
* A correction term \\(c\\) will need to be computed to compensate for the error in \\(r\\) when rounded to a floating-point number.
*
* <!-- </note> -->
*
* 2. To approximate \\(\operatorname{expm1f}(r)\\), we define \\(\operatorname{R1}(r^2)\\) by
*
* ```tex
* r \frac{e^r + 1}{e^r - 1} = 2 + \frac{r^2}{6} \operatorname{R1}(r^2)
* ```
*
* We use a polynomial of degree \\(2\\) in \\(r^2\\) to approximate \\(\mathrm{R1}\\):
*
* ```tex
* \operatorname{R1}(z) \approx 1 + \mathrm{Q1} \cdot z + \mathrm{Q2} \cdot z^2
* ```
*
* where \\(z = r^2\\) and
*
* ```tex
* \begin{align*}
* \mathrm{Q1} &= -3.3333212137\mbox{e-}2 \\
* \mathrm{Q2} &= 1.5807170421\mbox{e-}3
* \end{align*}
* ```
*
* \\(\operatorname{expm1f}(r) = e^r - 1\\) is then computed by
*
* ```tex
* \operatorname{expm1f}(r) = r + \frac{r^2}{2} + \frac{r^3}{2} \biggl( \frac{\mathrm{R1} - t}{6 - r \cdot t} \biggr)
* ```
*
* where \\(t = 3 - \mathrm{R1} \cdot \frac{r}{2}\\).
*
* 3. To scale back to obtain \\(\operatorname{expm1f}(x)\\), we have (from step 1)
*
* ```tex
* \operatorname{expm1f}(x) = 2^k (\operatorname{expm1f}(r) + 1) - 1
* ```
*
* ## Special Cases
*
* ```tex
* \begin{align*}
* \operatorname{expm1f}(\infty) &= \infty \\
* \operatorname{expm1f}(-\infty) &= -1 \\
* \operatorname{expm1f}(\mathrm{NaN}) &= \mathrm{NaN}
* \end{align*}
* ```
*
* ## Notes
*
* - For finite arguments, only \\(\operatorname{expm1f}(0) = 0\\) is exact.
*
* - For \\(|x| < 2^{-25}\\), the function returns \\(x\\) (with inexact floating-point flags raised when \\(x \neq 0\\)).
*
* - The hexadecimal values listed in the source are the intended ones for the implementation constants. Decimal values may be used, provided that the compiler will convert from decimal to binary accurately enough to produce the intended hexadecimal values.
*
* - According to an error analysis, the error is always less than \\(1\\) ulp (unit in the last place).
*
* @param {number} x - input value
* @returns {number} function value
*
* @example
* var v = expm1f( 0.2 );
* // returns ~0.221
*
* @example
* var v = expm1f( -9.0 );
* // returns ~-1.0
*
* @example
* var v = expm1f( 0.0 );
* // returns 0.0
*
* @example
* var v = expm1f( NaN );
* // returns NaN
*/
function expm1f( x ) {
var twopk;
var xsb;
var hfx;
var hxs;
var r1;
var hi;
var lo;
var hx;
var c;
var t;
var e;
var y;
var k;
x = f32( x );
hx = toWordf( x );
xsb = hx & 0x80000000; // sign bit of x
hx = ( hx & 0x7fffffff ) >>> 0; // high word of |x|
// Filter out huge and non-finite argument...
if ( hx >= 0x4195b844 ) {
if ( hx >= 0x42b17218 ) {
if ( hx > 0x7f800000 ) {
return f32( x + x );
}
if ( hx === 0x7f800000 ) {
// exp(+-inf) = {inf, -1}:
return ( xsb === 0 ) ? x : -1.0;
}
if ( x > OVERFLOW_THRESHOLD ) {
return f32( 1.0e30 * 1.0e30 ); // overflow
}
}
if ( xsb !== 0 ) {
return f32( TINY - 1.0 );
}
}
// Argument reduction...
if ( hx > 0x3eb17218 ) {
if ( hx < 0x3F851592 ) {
if ( xsb === 0 ) {
hi = f32( x - LN2_HI );
lo = LN2_LO;
k = 1;
} else {
hi = f32( x + LN2_HI );
lo = -LN2_LO;
k = -1;
}
} else {
if ( xsb === 0 ) {
k = f32( f32( INV_LN2 * x ) + 0.5 );
} else {
k = f32( f32( INV_LN2 * x ) - 0.5 );
}
k |= 0;
t = k;
hi = f32( x - f32( t * LN2_HI ) ); // t*ln2_hi is exact here
lo = f32( t * LN2_LO );
}
x = f32( hi - lo );
c = f32( f32( hi - x ) - lo );
} else if ( hx < 0x33000000 ) {
// when |x| < 2^-25, return x:
t = f32( 1.0e30 + x ); // return x with inexact flags when x != 0
return f32( x - f32( t - f32( 1.0e30 + x ) ) );
} else {
k = 0;
}
// x is now in primary range...
hfx = f32( 0.5 * x );
hxs = f32( x * hfx );
r1 = f32( 1.0 + f32( hxs * f32( Q1 + f32( hxs * Q2 ) ) ) );
t = f32( 3.0 - f32( r1 * hfx ) );
e = f32( hxs * f32( f32( r1 - t ) / f32( 6.0 - f32( x * t ) ) ) );
if ( k === 0 ) {
return f32( x - f32( f32( x * e ) - hxs ) ); // c is 0
}
twopk = fromWordf( ( ( FLOAT32_EXPONENT_BIAS + k ) << 23 ) >>> 0 ); // 2^k
e = f32( f32( x * f32( e - c ) ) - c );
e = f32( e - hxs );
if ( k === -1 ) {
return f32( f32( 0.5 * f32( x - e ) ) - 0.5 );
}
if ( k === 1 ) {
if ( x < -0.25 ) {
return f32( -2.0 * f32( e - f32( x + 0.5 ) ) );
}
return f32( 1.0 + f32( 2.0 * f32( x - e ) ) );
}
if ( k <= -2 || k > 56 ) {
// suffice to return exp(x)-1:
y = f32( 1.0 - f32( e - x ) );
if ( k === 128 ) {
y = f32( f32( y * 2.0 ) * 1.7014118346046923e+38 ); // 0x1p127F
} else {
y = f32( y * twopk );
}
return f32( y - 1.0 );
}
t = 1.0;
if ( k < 23 ) {
t = fromWordf( ( 0x3f800000 - ( ( 0x1000000 >> k ) >>> 0 ) ) >>> 0 ); // t=1-2^-k
y = f32( t - f32( e - x ) );
y = f32( y * twopk );
} else {
t = fromWordf( ( ( FLOAT32_EXPONENT_BIAS - k ) << 23 ) >>> 0 ); // 2^-k
y = f32( x - f32( e + t ) );
y = f32( y + 1.0 );
y = f32( y * twopk );
}
return y;
}
// EXPORTS //
module.exports = expm1f;
|