Skip to content

Commit 4f8b523

Browse files
authored
Apply suggestions from code review
Signed-off-by: Gunj Joshi <[email protected]>
1 parent 0e6f1ad commit 4f8b523

File tree

6 files changed

+157
-22
lines changed

6 files changed

+157
-22
lines changed

lib/node_modules/@stdlib/math/base/special/kernel-log1pf/README.md

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ for ( i = 0; i < x.length; i++ ) {
105105
### Usage
106106

107107
```c
108-
#include "stdlib/math/base/special/kernel_log1p.h"
108+
#include "stdlib/math/base/special/kernel_log1pf.h"
109109
```
110110

111111
#### stdlib_base_kernel_log1pf( f )
@@ -153,7 +153,7 @@ int main( void ) {
153153
float out;
154154
int i;
155155
for ( i = 0; i < 10; i++ ) {
156-
out = stdlib_base_kernel_log1p( x[ i ] );
156+
out = stdlib_base_kernel_log1pf( x[ i ] );
157157
printf ( "x[ i ]: %f, out: %f\n", x[ i ], out );
158158
}
159159
}
@@ -171,13 +171,6 @@ int main( void ) {
171171

172172
<section class="related">
173173

174-
* * *
175-
176-
## See Also
177-
178-
- <span class="package-name">[`@stdlib/math/base/special/kernel-log1p`][@stdlib/math/base/special/kernel-log1p]</span><span class="delimiter">: </span><span class="description">Computes `log(1+f) - f` for `1+f` in `~[sqrt(2)/2, sqrt(2)]`.</span>
179-
- <span class="package-name">[`@stdlib/math/base/special/log1p`][@stdlib/math/base/special/log1p]</span><span class="delimiter">: </span><span class="description">evaluate the natural logarithm of 1+x.</span>
180-
181174
</section>
182175

183176
<!-- /.related -->
@@ -192,10 +185,6 @@ int main( void ) {
192185

193186
<!-- <related-links> -->
194187

195-
[@stdlib/math/base/special/kernel-log1p]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/kernel-log1p
196-
197-
[@stdlib/math/base/special/log1p]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/log1p
198-
199188
<!-- </related-links> -->
200189

201190
</section>

lib/node_modules/@stdlib/math/base/special/kernel-log1pf/benchmark/benchmark.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
var bench = require( '@stdlib/bench' );
2424
var uniform = require( '@stdlib/random/array/uniform' );
25-
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
2626
var pkg = require( './../package.json' ).name;
2727
var kernelLog1pf = require( './../lib' );
2828

@@ -39,12 +39,12 @@ bench( pkg, function benchmark( b ) {
3939
b.tic();
4040
for ( i = 0; i < b.iterations; i++ ) {
4141
y = kernelLog1pf( x[ i % x.length ] );
42-
if ( isnan( y ) ) {
42+
if ( isnanf( y ) ) {
4343
b.fail( 'should not return NaN' );
4444
}
4545
}
4646
b.toc();
47-
if ( isnan( y ) ) {
47+
if ( isnanf( y ) ) {
4848
b.fail( 'should not return NaN' );
4949
}
5050
b.pass( 'benchmark finished' );

lib/node_modules/@stdlib/math/base/special/kernel-log1pf/benchmark/benchmark.native.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
var resolve = require( 'path' ).resolve;
2424
var bench = require( '@stdlib/bench' );
2525
var uniform = require( '@stdlib/random/array/uniform' );
26-
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
2727
var tryRequire = require( '@stdlib/utils/try-require' );
2828
var pkg = require( './../package.json' ).name;
2929

@@ -48,12 +48,12 @@ bench( pkg+'::native', opts, function benchmark( b ) {
4848
b.tic();
4949
for ( i = 0; i < b.iterations; i++ ) {
5050
y = kernelLog1pf( x[ i % x.length ] );
51-
if ( isnan( y ) ) {
51+
if ( isnanf( y ) ) {
5252
b.fail( 'should not return NaN' );
5353
}
5454
}
5555
b.toc();
56-
if ( isnan( y ) ) {
56+
if ( isnanf( y ) ) {
5757
b.fail( 'should not return NaN' );
5858
}
5959
b.pass( 'benchmark finished' );

lib/node_modules/@stdlib/math/base/special/kernel-log1pf/benchmark/c/native/benchmark.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ static double rand_float( void ) {
9191
*/
9292
static double benchmark( void ) {
9393
double elapsed;
94-
double x[ 100 ];
95-
double z;
94+
float x[ 100 ];
9695
double t;
96+
float z;
9797
int i;
9898

9999
for ( i = 0; i < 100; i++ ) {
@@ -102,7 +102,7 @@ static double benchmark( void ) {
102102

103103
t = tic();
104104
for ( i = 0; i < ITERATIONS; i++ ) {
105-
z = stdlib_base_kernel_log1pf( x[ i%100 ] );
105+
z = stdlib_base_kernel_log1pf( x[ i % 100 ] );
106106
if ( z != z ) {
107107
printf( "should not return NaN\n" );
108108
break;

lib/node_modules/@stdlib/math/base/special/kernel-log1pf/lib/main.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,79 @@ var polyvalQ = require( './polyval_q.js' );
4343
/**
4444
* Computes `logf(1+f) - f` for `1+f` in `~[sqrtf(2)/2, sqrtf(2)]`.
4545
*
46+
* ## Method
47+
*
48+
* This function is a helper function for computing logarithms in base \\(e\\), and what follows describes the overall strategy for doing so. The argument reduction and adding the final term of the polynomial are done by the caller for increased accuracy when different bases are used.
49+
*
50+
* 1. Argument Reduction. Find \\(k\\) and \\(f\\) such that
51+
*
52+
* ```tex
53+
* x = 2^k \cdot (1+f)
54+
* ```
55+
*
56+
* where \\(\sqrt(2)/2 < 1+f < \sqrt(2)\\).
57+
*
58+
* 2. Approximation of \\(\operatorname{log}(1+f)\\). Let
59+
*
60+
* ```tex
61+
* \begin{align*}
62+
* s &= \frac{f}{2+f} \\
63+
* &= 2s + \frac{2}{3} s^3 + \frac{2}{5} s^5 + \ldots \\
64+
* &= 2s + s R
65+
* \end{align*}
66+
* ```
67+
*
68+
* based on
69+
*
70+
* ```tex
71+
* \operatorname{log}(1+f) &= \operatorname{log}(1+s) - \operatorname{log}(1-s)
72+
* ```
73+
*
74+
* We use a special Reme algorithm on \\(\[0,0.1716]\\) to generate a polynomial of degree \\(14\\) to approximate \\(R\\). The maximum error of this polynomial approximation is bounded by \\(2^{-58.45}\\). In other words,
75+
*
76+
* ```tex
77+
* R(z) ~ L_{g1} s^2 + L_{g2} s^4 + L_{g3} s^6 + L_{g4} s^8 + L_{g5} s^{10} + L_{g6} s^{12} + L_{g7} s^{14}
78+
* ```
79+
*
80+
* where the values of \\(L_{g1}\\) to \\(L_{g7}\\) are the polynomial coefficients used in the program below and
81+
*
82+
* ```tex
83+
* L_{g1} s^2 + \ldots + L_{g7} s^{14} - R(z) \leq 2^{-58.45}
84+
* ```
85+
*
86+
* Note that
87+
*
88+
* ```tex
89+
* 2s = f - s \cdot f = f - h_{fsq} + (s \cdot h_{fsq})
90+
* ```
91+
*
92+
* where \\(h_{fsq} = f^{2}/2\\).
93+
*
94+
* In order to guarantee an error in \\(\operatorname{log}\\) below 1 ulp, we compute \\(\operatorname{log}\\) by
95+
*
96+
* ```tex
97+
* \begin{align*}
98+
* \operatorname{log}(1+f) &= f - s (f - R) & \textrm{(if f is not too large)} \\
99+
* \operatorname{log}(1+f) &= f - (h_{fsq} - s (h_{fsq}+R)) & \textrm{(better accuracy)}
100+
* \end{align*}
101+
*
102+
* 3. Finally,
103+
*
104+
* ```tex
105+
* \begin{align*}
106+
* \operatorname{log}(x) &= k \cdot \operatorname{ln2} + \operatorname{log}(1+f) \\
107+
* &= k \cdot \operatorname{ln2}_{hi} + (f-(h_{fsq}-(s \cdot (h_{fsq}+R) + k \cdot \operatorname{ln2}_{lo})))
108+
* \end{align*}
109+
* ```
110+
*
111+
* Here, \\(\operatorname{ln2}\\) is split into two floating point numbers:
112+
*
113+
* ```tex
114+
* \operatorname{ln2} = \operatorname{ln2}_{hi} + \operatorname{ln2}_{lo}
115+
* ```
116+
*
117+
* where \\(n \cdot \operatorname{ln2}_{hi}\\) is always exact for \\(|n| < 2000\\).
118+
*
46119
* @param {number} f - input value
47120
* @returns {number} function value
48121
*

lib/node_modules/@stdlib/math/base/special/kernel-log1pf/src/main.c

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,79 @@ static float polyval_q( const float x ) {
8080
/**
8181
* Computes `logf(1+f) - f` for `1+f` in `~[sqrtf(2)/2, sqrtf(2)]`.
8282
*
83+
* ## Method
84+
*
85+
* This function is a helper function for computing logarithms in base \\(e\\), and what follows describes the overall strategy for doing so. The argument reduction and adding the final term of the polynomial are done by the caller for increased accuracy when different bases are used.
86+
*
87+
* 1. Argument Reduction. Find \\(k\\) and \\(f\\) such that
88+
*
89+
* ```tex
90+
* x = 2^k \cdot (1+f)
91+
* ```
92+
*
93+
* where \\(\sqrt(2)/2 < 1+f < \sqrt(2)\\).
94+
*
95+
* 2. Approximation of \\(\operatorname{log}(1+f)\\). Let
96+
*
97+
* ```tex
98+
* \begin{align*}
99+
* s &= \frac{f}{2+f} \\
100+
* &= 2s + \frac{2}{3} s^3 + \frac{2}{5} s^5 + \ldots \\
101+
* &= 2s + s R
102+
* \end{align*}
103+
* ```
104+
*
105+
* based on
106+
*
107+
* ```tex
108+
* \operatorname{log}(1+f) &= \operatorname{log}(1+s) - \operatorname{log}(1-s)
109+
* ```
110+
*
111+
* We use a special Reme algorithm on \\(\[0,0.1716]\\) to generate a polynomial of degree \\(14\\) to approximate \\(R\\). The maximum error of this polynomial approximation is bounded by \\(2^{-58.45}\\). In other words,
112+
*
113+
* ```tex
114+
* R(z) ~ L_{g1} s^2 + L_{g2} s^4 + L_{g3} s^6 + L_{g4} s^8 + L_{g5} s^{10} + L_{g6} s^{12} + L_{g7} s^{14}
115+
* ```
116+
*
117+
* where the values of \\(L_{g1}\\) to \\(L_{g7}\\) are the polynomial coefficients used in the program below and
118+
*
119+
* ```tex
120+
* L_{g1} s^2 + \ldots + L_{g7} s^{14} - R(z) \leq 2^{-58.45}
121+
* ```
122+
*
123+
* Note that
124+
*
125+
* ```tex
126+
* 2s = f - s \cdot f = f - h_{fsq} + (s \cdot h_{fsq})
127+
* ```
128+
*
129+
* where \\(h_{fsq} = f^{2}/2\\).
130+
*
131+
* In order to guarantee an error in \\(\operatorname{log}\\) below 1 ulp, we compute \\(\operatorname{log}\\) by
132+
*
133+
* ```tex
134+
* \begin{align*}
135+
* \operatorname{log}(1+f) &= f - s (f - R) & \textrm{(if f is not too large)} \\
136+
* \operatorname{log}(1+f) &= f - (h_{fsq} - s (h_{fsq}+R)) & \textrm{(better accuracy)}
137+
* \end{align*}
138+
*
139+
* 3. Finally,
140+
*
141+
* ```tex
142+
* \begin{align*}
143+
* \operatorname{log}(x) &= k \cdot \operatorname{ln2} + \operatorname{log}(1+f) \\
144+
* &= k \cdot \operatorname{ln2}_{hi} + (f-(h_{fsq}-(s \cdot (h_{fsq}+R) + k \cdot \operatorname{ln2}_{lo})))
145+
* \end{align*}
146+
* ```
147+
*
148+
* Here, \\(\operatorname{ln2}\\) is split into two floating point numbers:
149+
*
150+
* ```tex
151+
* \operatorname{ln2} = \operatorname{ln2}_{hi} + \operatorname{ln2}_{lo}
152+
* ```
153+
*
154+
* where \\(n \cdot \operatorname{ln2}_{hi}\\) is always exact for \\(|n| < 2000\\).
155+
*
83156
* @param f input value
84157
* @return output value
85158
*

0 commit comments

Comments
 (0)