Skip to content

Commit 3c9f6ff

Browse files
changed the computation methods
1 parent 08ebbbe commit 3c9f6ff

File tree

7 files changed

+141
-156
lines changed

7 files changed

+141
-156
lines changed

lib/node_modules/@stdlib/math/base/special/expf/lib/expmulti.js

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,46 +18,17 @@
1818
*
1919
* ## Notice
2020
*
21-
* The following copyrights, licenses, and long comment were part of the original implementation available as part of [Go]{@link https://github.com/golang/go/blob/cb07765045aed5104a3df31507564ac99e6ddce8/src/math/exp.go}, which in turn was based on an implementation available as part of [FreeBSD]{@link https://svnweb.freebsd.org/base/release/9.3.0/lib/msun/src/e_exp.c}. The implementation follows the original, but has been modified for JavaScript.
22-
*
23-
* ```text
24-
* Copyright (c) 2009 The Go Authors. All rights reserved.
25-
*
26-
* Redistribution and use in source and binary forms, with or without
27-
* modification, are permitted provided that the following conditions are
28-
* met:
29-
*
30-
* * Redistributions of source code must retain the above copyright
31-
* notice, this list of conditions and the following disclaimer.
32-
* * Redistributions in binary form must reproduce the above
33-
* copyright notice, this list of conditions and the following disclaimer
34-
* in the documentation and/or other materials provided with the
35-
* distribution.
36-
* * Neither the name of Google Inc. nor the names of its
37-
* contributors may be used to endorse or promote products derived from
38-
* this software without specific prior written permission.
39-
*
40-
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
41-
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
42-
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
43-
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
44-
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45-
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
46-
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
47-
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
48-
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
49-
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
50-
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51-
* ```
52-
*
53-
* ```text
54-
* Copyright (C) 2004 by Sun Microsystems, Inc. All rights reserved.
21+
* 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.
5522
*
23+
* e_expf.c -- float version of e_exp.c.
24+
* Conversion to float by Ian Lance Taylor, Cygnus Support, [email protected].
25+
* ====================================================
26+
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5627
* Developed at SunPro, a Sun Microsystems, Inc. business.
5728
* Permission to use, copy, modify, and distribute this
5829
* software is freely granted, provided that this notice
5930
* is preserved.
60-
* ```
31+
* ====================================================
6132
*/
6233

6334
'use strict';

lib/node_modules/@stdlib/math/base/special/expf/lib/main.js

Lines changed: 10 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -18,46 +18,17 @@
1818
*
1919
* ## Notice
2020
*
21-
* The following copyrights, licenses, and long comment were part of the original implementation available as part of [Go]{@link https://github.com/golang/go/blob/cb07765045aed5104a3df31507564ac99e6ddce8/src/math/exp.go}, which in turn was based on an implementation available as part of [FreeBSD]{@link https://svnweb.freebsd.org/base/release/9.3.0/lib/msun/src/e_exp.c}. The implementation follows the original, but has been modified for JavaScript.
22-
*
23-
* ```text
24-
* Copyright (c) 2009 The Go Authors. All rights reserved.
25-
*
26-
* Redistribution and use in source and binary forms, with or without
27-
* modification, are permitted provided that the following conditions are
28-
* met:
29-
*
30-
* * Redistributions of source code must retain the above copyright
31-
* notice, this list of conditions and the following disclaimer.
32-
* * Redistributions in binary form must reproduce the above
33-
* copyright notice, this list of conditions and the following disclaimer
34-
* in the documentation and/or other materials provided with the
35-
* distribution.
36-
* * Neither the name of Google Inc. nor the names of its
37-
* contributors may be used to endorse or promote products derived from
38-
* this software without specific prior written permission.
39-
*
40-
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
41-
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
42-
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
43-
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
44-
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45-
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
46-
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
47-
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
48-
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
49-
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
50-
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51-
* ```
52-
*
53-
* ```text
54-
* Copyright (C) 2004 by Sun Microsystems, Inc. All rights reserved.
21+
* 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.
5522
*
23+
* e_expf.c -- float version of e_exp.c.
24+
* Conversion to float by Ian Lance Taylor, Cygnus Support, [email protected].
25+
* ====================================================
26+
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5627
* Developed at SunPro, a Sun Microsystems, Inc. business.
5728
* Permission to use, copy, modify, and distribute this
5829
* software is freely granted, provided that this notice
5930
* is preserved.
60-
* ```
31+
* ====================================================
6132
*/
6233

6334
'use strict';
@@ -74,8 +45,8 @@ var expmulti = require( './expmulti.js' );
7445

7546
// VARIABLES //
7647

77-
var LN2_HI = [6.9314575195e-01, -6.9314575195e-01];
78-
var LN2_LO = [1.4286067653e-06, -1.4286067653e-06];
48+
var LN2_HI = 6.9314575195e-01;
49+
var LN2_LO = 1.4286067653e-06;
7950
var halF = [0.5, -0.5];
8051
var INVLN2 = 1.4426950216e+00;
8152
var OVERFLOW = 8.8721679688e+01;
@@ -218,8 +189,8 @@ function expf( x ) {
218189
// Argument reduction
219190

220191
k = truncf(INVLN2 * x + halF[xsb]);
221-
hi = x - k * LN2_HI[0];
222-
lo = k * LN2_LO[0];
192+
hi = x - k * LN2_HI;
193+
lo = k * LN2_LO;
223194

224195
return expmulti( hi, lo, k );
225196
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
julia 1.5
2+
JSON 0.21

lib/node_modules/@stdlib/math/base/special/expf/scripts/fixtures/julia/data.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env julia
2+
#
3+
# @license Apache-2.0
4+
#
5+
# Copyright (c) 2018 The Stdlib Authors.
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
19+
import JSON
20+
21+
"""
22+
gen( x, filepath )
23+
24+
Generate fixture data and write to file.
25+
26+
# Arguments
27+
28+
* `x`: domain
29+
* `filepath::AbstractString`: filepath of the output file
30+
31+
# Examples
32+
33+
``` julia
34+
julia> x = range( -1000, stop = 1000, length = 2001 );
35+
julia> gen( x, \"./data.json\" );
36+
```
37+
"""
38+
function gen( x, filepath )
39+
y = Array{Float32}( undef, length(x) );
40+
for i in eachindex(x)
41+
y[i] = exp( Float32.(x[i]) );
42+
end
43+
44+
data = Dict([
45+
("x", x),
46+
("expected", y)
47+
]);
48+
49+
outfile = open( filepath, "w" );
50+
write( outfile, JSON.json(data) );
51+
write( outfile, "\n" );
52+
close( outfile );
53+
end
54+
55+
# Get the filename:
56+
file = @__FILE__;
57+
58+
# Extract the directory in which this file resides:
59+
dir = dirname( file );
60+
61+
x = range( -300, stop = 300, length = 10000 );
62+
out = joinpath( dir, "data.json" );
63+
gen( x, out );

lib/node_modules/@stdlib/math/base/special/expf/src/addon.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
* limitations under the License.
1818
*/
1919

20-
#include "stdlib/math/base/special/expf.h"
2120
#include "stdlib/math/base/napi/unary.h"
21+
#include "stdlib/math/base/special/expf.h"
2222

2323
// cppcheck-suppress shadowFunction
2424
STDLIB_MATH_BASE_NAPI_MODULE_D_D( stdlib_base_expf )

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

Lines changed: 58 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -18,63 +18,35 @@
1818
*
1919
* ## Notice
2020
*
21-
* * The following copyrights, licenses, and long comment were part of the original implementation available as part of [Go]{@link https://github.com/golang/go/blob/cb07765045aed5104a3df31507564ac99e6ddce8/src/math/exp.go}, which in turn was based on an implementation available as part of [FreeBSD]{@link https://svnweb.freebsd.org/base/release/9.3.0/lib/msun/src/e_exp.c}. The implementation follows the original, but has been modified according to project conventions.
22-
*
23-
* ```text
24-
* Copyright (c) 2009 The Go Authors. All rights reserved.
25-
*
26-
* Redistribution and use in source and binary forms, with or without
27-
* modification, are permitted provided that the following conditions are
28-
* met:
29-
*
30-
* * Redistributions of source code must retain the above copyright
31-
* notice, this list of conditions and the following disclaimer.
32-
* * Redistributions in binary form must reproduce the above
33-
* copyright notice, this list of conditions and the following disclaimer
34-
* in the documentation and/or other materials provided with the
35-
* distribution.
36-
* * Neither the name of Google Inc. nor the names of its
37-
* contributors may be used to endorse or promote products derived from
38-
* this software without specific prior written permission.
39-
*
40-
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
41-
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
42-
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
43-
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
44-
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45-
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
46-
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
47-
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
48-
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
49-
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
50-
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51-
* ```
52-
*
53-
* ```text
54-
* Copyright (C) 2004 by Sun Microsystems, Inc. All rights reserved.
21+
* 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.
5522
*
23+
* e_expf.c -- float version of e_exp.c.
24+
* Conversion to float by Ian Lance Taylor, Cygnus Support, [email protected].
25+
* ====================================================
26+
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5627
* Developed at SunPro, a Sun Microsystems, Inc. business.
5728
* Permission to use, copy, modify, and distribute this
5829
* software is freely granted, provided that this notice
5930
* is preserved.
60-
* ```
31+
* ====================================================
6132
*/
6233

63-
#include "stdlib/math/base/special/expf.h"
64-
#include "stdlib/math/base/assert/is_nan.h"
65-
#include "stdlib/math/base/special/truncf.h"
6634
#include "stdlib/constants/float32/ninf.h"
6735
#include "stdlib/constants/float32/pinf.h"
36+
#include "stdlib/math/base/assert/is_nan.h"
37+
#include "stdlib/math/base/special/expf.h"
6838
#include "stdlib/math/base/special/ldexpf.h"
39+
#include "stdlib/math/base/special/truncf.h"
6940
#include <stdint.h>
7041

71-
static const float LN2_HI = 0.69314718f;
72-
static const float LN2_LO = 1.90821484e-10f;
73-
static const float LOG2_E = 1.44269504f;
74-
static const float EXP_OVERFLOW = 88.722839f;
75-
static const float EXP_UNDERFLOW = -103.97208f;
76-
static const float NEARZERO = 1.0f / (1 << 14);
77-
static const float NEG_NEARZERO = -NEARZERO;
42+
static const float LN2_HI = 0.69314718;
43+
static const float LN2_LO = 1.4286067653e-06;
44+
static const float LOG2_E = 1.4426950216;
45+
static const float half[ 2 ] = { 0.5, -0.5 };
46+
static const float EXP_OVERFLOW = 88.721679688;
47+
static const float EXP_UNDERFLOW = -103.97208405;
48+
static const float NEARZERO = 1.0 / ( 1 << 14 );
49+
static const float NEG_NEARZERO = -1.0 / ( 1 << 14 );
7850

7951
/* Begin auto-generated functions. The following functions are auto-generated. Do not edit directly. */
8052

@@ -93,7 +65,12 @@ static const float NEG_NEARZERO = -NEARZERO;
9365
* @return evaluated polynomial
9466
*/
9567
static float polyval_p( const float x ) {
96-
return 0.16666667f + (x * (-0.0027777778f + (x * (0.000066137566f + (x * (-0.000001653439f + (x * 4.1381369e-8f)))))));
68+
static const float P1 = 1.6666625440e-1;
69+
static const float P2 = -2.7667332906e-3;
70+
if ( x == 0.0 ) {
71+
return P1;
72+
}
73+
return P1 + x * P2;
9774
}
9875

9976
// END: polyval_p
@@ -109,17 +86,19 @@ static float polyval_p( const float x ) {
10986
* @return function value
11087
*/
11188
static float expmulti( const float hi, const float lo, const int32_t k ) {
112-
float r;
113-
float t;
114-
float c;
115-
float y;
89+
float r;
90+
float t;
91+
float c;
92+
float y;
93+
float twom100;
11694

117-
r = hi - lo;
118-
t = r * r;
119-
c = r - (t * polyval_p( t ));
120-
y = 1.0f - (lo - ((r * c) / (2.0f - c)) - hi);
95+
twom100 = 7.8886090522e-31;
96+
r = hi - lo;
97+
t = r * r;
98+
c = r - ( t * polyval_p( t ) );
99+
y = 1.0 - ( lo - ( ( r * c ) / ( 2.0 - c ) ) - hi );
121100

122-
return stdlib_base_ldexpf( y, k );
101+
return stdlib_base_ldexpf( y, k + 100 ) * twom100 * twom100;
123102
}
124103

125104
/**
@@ -221,33 +200,31 @@ static float expmulti( const float hi, const float lo, const int32_t k ) {
221200
* // returns 1.0f
222201
*/
223202
float stdlib_base_expf( const float x ) {
224-
float hi;
225-
float lo;
226-
int32_t k;
203+
float hi;
204+
float lo;
205+
int32_t k;
206+
int xsb;
227207

208+
xsb = ( x < 0.0 ) ? 1 : 0;
228209
if ( stdlib_base_is_nan( x ) || x == STDLIB_CONSTANT_FLOAT32_PINF ) {
229-
return x;
230-
}
231-
if ( x == STDLIB_CONSTANT_FLOAT32_NINF ) {
232-
return 0.0f;
233-
}
234-
if ( x > EXP_OVERFLOW ) {
235-
return STDLIB_CONSTANT_FLOAT32_PINF;
236-
}
237-
if ( x < EXP_UNDERFLOW ) {
238-
return 0.0f;
239-
}
240-
if ( x > NEG_NEARZERO && x < NEARZERO ) {
241-
return 1.0f + x;
242-
}
243-
// Reduce and compute `r = hi - lo` for extra precision...
244-
if ( x < 0.0f ) {
245-
k = stdlib_base_truncf( (LOG2_E * x) - 0.5f );
246-
} else {
247-
k = stdlib_base_truncf( (LOG2_E * x) + 0.5f );
248-
}
249-
hi = x - (k * LN2_HI);
250-
lo = k * LN2_LO;
210+
return x;
211+
}
212+
if ( x == STDLIB_CONSTANT_FLOAT32_NINF ) {
213+
return 0.0;
214+
}
215+
if ( x > EXP_OVERFLOW ) {
216+
return STDLIB_CONSTANT_FLOAT32_PINF;
217+
}
218+
if ( x < EXP_UNDERFLOW ) {
219+
return 0.0;
220+
}
221+
if ( x > NEG_NEARZERO && x < NEARZERO ) {
222+
return 1.0 + x;
223+
}
224+
// Reduce and compute `r = hi - lo` for extra precision...
225+
k = stdlib_base_truncf( ( LOG2_E * x ) + half[ xsb ] );
226+
hi = x - ( k * LN2_HI );
227+
lo = k * LN2_LO;
251228

252-
return expmulti( hi, lo, k );
229+
return expmulti( hi, lo, k );
253230
}

0 commit comments

Comments
 (0)