Skip to content

Commit a2e6cd9

Browse files
chore: refactor implementation
1 parent 6244749 commit a2e6cd9

File tree

2 files changed

+18
-26
lines changed

2 files changed

+18
-26
lines changed

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

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,7 @@
4040
#include "stdlib/constants/float64/ninf.h"
4141
#include <stdint.h>
4242

43-
static const double one = 1.0;
44-
45-
static const double zero = 0.0;
46-
47-
static const double huge = 1e300;
48-
49-
// 0x3ff00000 = 1072693248 => 0 01111111111 00000000000000000000 => biased exponent: 1023 = 0+1023 => 2^0 = 1
50-
static const int32_t HIGH_BIASED_EXP_0 = 0x3ff00000;
51-
52-
// 0x3e300000 = 1040187392 => 0 01111100011 00000000000000000000 => biased exponent: 100 = -23+127
53-
static const int32_t HIGH_BIASED_EXP_NEG_7 = 0x3e300000;
54-
55-
// 0x3fe00000 = 1071644672 => 0 01111111110 00000000000000000000 => biased exponent: 1022 = -1+1023 => 2^-1
56-
static const int32_t HIGH_BIASED_EXP_NEG_1 = 0x3fe00000;
43+
static const double NEAR_ZERO = 1.0 / (1 << 28); // 2**-28
5744

5845
/**
5946
* Computes the hyperbolic arctangent of a double-precision floating-point number.
@@ -95,37 +82,42 @@ static const int32_t HIGH_BIASED_EXP_NEG_1 = 0x3fe00000;
9582
* // returns ~1.472
9683
*/
9784
double stdlib_base_atanh( const double x ) {
85+
uint32_t ahx;
9886
uint32_t lx;
9987
uint32_t hx;
100-
uint32_t ahx;
10188
double ax;
10289
double t;
90+
10391
if ( stdlib_base_is_nan( x ) || x < -1.0 || x > 1.0 ) {
10492
return 0.0 / 0.0; // NaN
10593
}
94+
10695
// Split `x` into high and low words:
10796
stdlib_base_float64_to_words( x, &hx, &lx );
10897

98+
ax = x;
10999
ahx = hx & STDLIB_CONSTANT_FLOAT64_HIGH_WORD_ABS_MASK;
110100

111-
if( ( ahx | ( ( lx | (-lx) )>>31 ))> HIGH_BIASED_EXP_0 ) {
112-
return ( x - x ) / ( x - x ); // |x|>1
101+
// special cases
102+
if ( ax == 1.0 ) {
103+
return STDLIB_CONSTANT_FLOAT64_PINF;
113104
}
114-
if( ahx == HIGH_BIASED_EXP_0 ) {
115-
return x / zero;
105+
if ( ax == -1.0 ) {
106+
return STDLIB_CONSTANT_FLOAT64_NINF;
116107
}
117-
if( ahx < HIGH_BIASED_EXP_NEG_7 && ( huge+x ) > zero) {
118-
return x; // x<2**-28
108+
109+
// Case: |x| < 2**-28
110+
if ( ax < NEAR_ZERO ) {
111+
return ( hx == 1 ) ? -ax : ax;
119112
}
120113

121-
ax = x;
122114
stdlib_base_float64_set_high_word( ahx, &ax );
123115

124-
if( ahx < HIGH_BIASED_EXP_NEG_1 ) {
116+
if( ahx < 0.5 ) {
125117
t = ax + ax;
126-
t = 0.5 * stdlib_base_log1p( t + ( t * ax / ( one - ax ) ) );
118+
t = 0.5 * stdlib_base_log1p( t + ( t * ax / ( 1.0 - ax ) ) );
127119
} else {
128-
t = 0.5 * stdlib_base_log1p( ( ax + ax ) / ( one - ax ) );
120+
t = 0.5 * stdlib_base_log1p( ( ax + ax ) / ( 1.0 - ax ) );
129121
}
130122
return ( hx == 1 ) ? -t : t;
131123
}

lib/node_modules/@stdlib/math/base/special/atanh/test/test.native.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ tape( 'the function computes the hyperbolic arctangent (negative values)', opts,
7474
t.strictEqual( y, expected[ i ], 'x: '+x[i]+'. E: '+expected[i] );
7575
} else {
7676
delta = abs( y - expected[i] );
77-
tol = 10.0 * EPS * abs( expected[i] );
77+
tol = 1.0 * EPS * abs( expected[i] );
7878
t.ok( delta <= tol, 'within tolerance. x: '+x[i]+'. y: '+y+'. E: '+expected[i]+'. Δ: '+delta+'. tol: '+tol+'.' );
7979
}
8080
}

0 commit comments

Comments
 (0)