Skip to content

Commit 0a659c4

Browse files
committed
fix: improve numerical accuracy for large x values
This commit preserves user expectation for the naive approach of `expm1(x)/x` for medium-sized `x`, but extends the range of computed values afforded by the division by `x`. Closes: #12436 --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown_pkg_readmes status: na - task: lint_markdown_docs status: na - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: passed - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 8879b1d commit 0a659c4

2 files changed

Lines changed: 44 additions & 4 deletions

File tree

  • lib/node_modules/@stdlib/math/base/special/expm1rel

‎lib/node_modules/@stdlib/math/base/special/expm1rel/lib/main.js‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,28 @@ var PINF = require( '@stdlib/constants/float64/pinf' );
2828

2929
// VARIABLES //
3030

31-
var OVERFLOW_THRESHOLD = 7.09782712893383973096e+02; // 0x40862E42 0xFEFA39EF
31+
var HUGE_VALUE = 7.09782712893383973096e+02; // 0x40862E42 0xFEFA39EF
32+
var OVERFLOW_THRESHOLD = 7.163568913878179e+02;
3233

3334

3435
// MAIN //
3536

3637
/**
3738
* Computes the relative error exponential.
3839
*
40+
* ## Method
41+
*
42+
* To avoid overflow when \\( x \\) exceeds the natural logarithm of the maximum double-precision floating-point number, we can derive the following identity:
43+
*
44+
* ```tex
45+
* \begin{align*}
46+
* \frac{e^x - 1}{x} &= \frac{(e^{x/2} - 1)(e^{x/2} + 1)}{x} \\
47+
* &= \frac{(e^{x/2} - 1)(e^{x/2} - 1 + 2)}{x} \\
48+
* &= \frac{\operatorname{expm1}(x/2) \cdot (\operatorname{expm1}(x/2) + 2)}{x} \\
49+
* &= \frac{\operatorname{expm1}(x/2)}{x} \cdot (\operatorname{expm1}(x/2) + 2)
50+
* \end{align*}
51+
* ```
52+
*
3953
* @param {number} x - input value
4054
* @returns {number} function value
4155
*
@@ -56,13 +70,19 @@ var OVERFLOW_THRESHOLD = 7.09782712893383973096e+02; // 0x40862E42 0xFEFA39EF
5670
* // returns NaN
5771
*/
5872
function expm1rel( x ) {
73+
var tmp;
5974
if ( abs( x ) <= EPS ) {
6075
return 1.0; // L'Hopital's Rule
6176
}
77+
if ( x < HUGE_VALUE ) {
78+
return expm1( x ) / x;
79+
}
6280
if ( x >= OVERFLOW_THRESHOLD ) {
6381
return PINF; // L'Hopital's Rule
6482
}
65-
return expm1( x ) / x;
83+
// HUGE_VALUE <= x < OVERFLOW_THRESHOLD
84+
tmp = expm1( x/2.0 );
85+
return ( tmp/x ) * ( tmp+2.0 ); // note: we keep the `+2` term in order to prevent compiler optimizations from rearranging terms, even though `tmp + 2 = tmp` due to the limits of floating-point precision
6686
}
6787

6888

‎lib/node_modules/@stdlib/math/base/special/expm1rel/src/main.c‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,25 @@
2222
#include "stdlib/constants/float64/eps.h"
2323
#include "stdlib/constants/float64/pinf.h"
2424

25-
static const double OVERFLOW_THRESHOLD = 7.09782712893383973096e+02; // 0x40862E42 0xFEFA39EF
25+
static const double HUGE_THRESHOLD = 7.09782712893383973096e+02; // 0x40862E42 0xFEFA39EF
26+
static const double OVERFLOW_THRESHOLD = 7.163568913878179e+02;
2627

2728
/**
2829
* Computes the relative error exponential.
2930
*
31+
* ## Method
32+
*
33+
* To avoid overflow when \\( x \\) exceeds the natural logarithm of the maximum double-precision floating-point number, we can derive the following identity:
34+
*
35+
* ```tex
36+
* \begin{align*}
37+
* \frac{e^x - 1}{x} &= \frac{(e^{x/2} - 1)(e^{x/2} + 1)}{x} \\
38+
* &= \frac{(e^{x/2} - 1)(e^{x/2} - 1 + 2)}{x} \\
39+
* &= \frac{\operatorname{expm1}(x/2) \cdot (\operatorname{expm1}(x/2) + 2)}{x} \\
40+
* &= \frac{\operatorname{expm1}(x/2)}{x} \cdot (\operatorname{expm1}(x/2) + 2)
41+
* \end{align*}
42+
* ```
43+
*
3044
* @param x input value
3145
* @return output value
3246
*
@@ -35,11 +49,17 @@ static const double OVERFLOW_THRESHOLD = 7.09782712893383973096e+02; // 0x40862E
3549
* // returns 1.0
3650
*/
3751
double stdlib_base_expm1rel( const double x ) {
52+
double tmp;
3853
if ( stdlib_base_abs( x ) <= STDLIB_CONSTANT_FLOAT64_EPS ) {
3954
return 1.0; // L'Hopital's Rule
4055
}
56+
if ( x < HUGE_THRESHOLD ) {
57+
return stdlib_base_expm1( x ) / x;
58+
}
4159
if ( x >= OVERFLOW_THRESHOLD ) {
4260
return STDLIB_CONSTANT_FLOAT64_PINF; // L'Hopital's Rule
4361
}
44-
return stdlib_base_expm1( x ) / x;
62+
// HUGE_VALUE <= x < OVERFLOW_THRESHOLD
63+
tmp = stdlib_base_expm1( x/2.0 );
64+
return ( tmp/x ) * ( tmp+2.0 ); // note: we keep the `+2` term in order to prevent compiler optimizations from rearranging terms, even though `tmp + 2 = tmp` due to the limits of floating-point precision
4565
}

0 commit comments

Comments
 (0)