Skip to content

Commit 4bdea42

Browse files
committed
feat: add javascript and c implementation
--- 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 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: passed - 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: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 3647160 commit 4bdea42

File tree

4 files changed

+127
-46
lines changed

4 files changed

+127
-46
lines changed

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

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,33 @@
3333

3434
// MODULES //
3535

36-
var exp10 = require( '@stdlib/math/base/special/exp10' );
37-
var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' );
36+
var floorf = require( '@stdlib/math/base/special/floorf' );
37+
var ldexpf = require( '@stdlib/math/base/special/ldexpf' );
38+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
39+
var PINF = require( '@stdlib/constants/float32/pinf' );
40+
var polyval = require( './polyval.js' );
41+
42+
43+
// VARIABLES //
44+
45+
var LOG210 = 3.32192809488736234787e0;
46+
var LG102A = 3.00781250000000000000E-1;
47+
var LG102B = 2.48745663981195213739E-4;
48+
var MAXL10 = 38.230809449325611792;
3849

3950

4051
// MAIN //
4152

4253
/**
4354
* Computes `10^x` for a single-precision floating-point number.
4455
*
56+
* ## Method
57+
*
58+
* - Range reduction is accomplished by expressing the argument as \\( 10^x = 2^n 10^f \\), with \\( |f| < 0.5 \\log_{10}(2) \\).
59+
* - A polynomial is used to approximate \\( 10^f \\).
60+
*
4561
* @param {number} x - input value
46-
* @returns {number} single-precision result
62+
* @returns {number} function value
4763
*
4864
* @example
4965
* var v = exp10f( 3.0 );
@@ -58,7 +74,33 @@ var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' );
5874
* // returns NaN
5975
*/
6076
function exp10f( x ) {
61-
return float64ToFloat32( exp10( float64ToFloat32( x ) ) );
77+
var px;
78+
var n;
79+
80+
if ( isnanf( x ) ) {
81+
return x;
82+
}
83+
84+
if ( x === 0.0 ) {
85+
return 1.0;
86+
}
87+
if ( x > MAXL10 ) {
88+
return PINF;
89+
}
90+
if ( x < (-1 * MAXL10) ) {
91+
return 0.0;
92+
}
93+
94+
px = floorf( (LOG210*x) + 0.5 );
95+
n = px;
96+
x -= px * LG102A;
97+
x -= px * LG102B;
98+
99+
// Polynomial approximation:
100+
x = 1.0 + ( x * polyval( x ) );
101+
102+
// Multiply by power of 2:
103+
return ldexpf( x, n );
62104
}
63105

64106

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/* This is a generated file. Do not edit directly. */
20+
'use strict';
21+
22+
// MAIN //
23+
24+
/**
25+
* Evaluates a polynomial.
26+
*
27+
* ## Notes
28+
*
29+
* - The implementation uses [Horner's rule][horners-method] for efficient computation.
30+
*
31+
* [horners-method]: https://en.wikipedia.org/wiki/Horner%27s_method
32+
*
33+
* @private
34+
* @param {number} x - value at which to evaluate the polynomial
35+
* @returns {number} evaluated polynomial
36+
*/
37+
function evalpoly( x ) {
38+
if ( x === 0.0 ) {
39+
return 2.302585167056758;
40+
}
41+
return 2.302585167056758 + (x * (2.650948748208892 + (x * (2.034649854009453 + (x * (1.171292686296281 + (x * (0.5420251702225484 + (x * 0.2063216740311022))))))))); // eslint-disable-line max-len
42+
}
43+
44+
45+
// EXPORTS //
46+
47+
module.exports = evalpoly;

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

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,6 @@
1919
*
2020
* The original C code, long comment, copyright, license, and constants are from [Cephes]{@link http://www.netlib.org/cephes}.
2121
* The implementation follows the original, but has been modified according to project conventions.
22-
*
23-
* Copyright 1984, 1991, 2000 by Stephen L. Moshier
24-
*
25-
* Some software in this archive may be from the book _Methods and Programs for Mathematical Functions_
26-
* (Prentice-Hall or Simon & Schuster International, 1989) or from the Cephes Mathematical Library,
27-
* a commercial product. In either event, it is copyrighted by the author.
28-
* What you see here may be used freely but it comes with no support or guarantee.
29-
*
30-
* Stephen L. Moshier
31-
3222
*/
3323

3424
#include "stdlib/math/base/special/exp10f.h"
@@ -40,62 +30,64 @@
4030
#include "stdlib/math/base/special/ldexpf.h"
4131
#include <stdint.h>
4232

43-
// Constants:
44-
static const float LOG210F = 3.3219281f;
45-
static const float LG102AF = 0.30102539f;
46-
static const float LG102BF = 4.6050378e-06f;
33+
// Cephes constants:
4734

48-
// Evaluate polynomial P(x):
49-
static float polyval_p( const float x ) {
50-
return 2394.2374f + (x * (406.7173f + (x * (11.745273f + (x * 0.04099625f)))));
35+
static float LOG210 = 3.32192809488736234787e0;
36+
static float LG102A = 3.00781250000000000000E-1;
37+
static float LG102B = 2.48745663981195213739E-4;
38+
static float MAXL10 = 38.230809449325611792;
39+
/**
40+
* Evaluates a polynomial using Horner's rule.
41+
* P(x)=a5​+x(a4​+x(a3​+x(a2​+x(a1​+xa0​))))
42+
*
43+
* @param x input value
44+
* @return result of polynomial evaluation
45+
*/
46+
static float polyval( const float x ) {
47+
return 2.302585167056758e+0f + x * (2.650948748208892e+0f + x * (2.034649854009453e+0f + x * (1.171292686296281e+0f + x * (5.420251702225484e-1f + x * 2.063216740311022e-1f))));
5148
}
5249

53-
// Evaluate polynomial Q(x):
54-
static float polyval_q( const float x ) {
55-
return 2079.6082f + (x * (1272.0927f + (x * (85.09362f + (x * 1.0f)))));
56-
}
5750

5851
/**
59-
* Returns 10 raised to the x power (single-precision).
52+
* Computes 10^x for a single-precision floating-point number.
6053
*
6154
* ## Method
6255
*
63-
* - Range reduction is done using 10^x = 2^n * 10^f, with |f| < 0.5 * log10(2)
64-
* - The reduced part is evaluated with a rational approximation:
65-
*
66-
* 1 + 2x P(x^2) / (Q(x^2) - P(x^2))
56+
* - Performs range reduction: 10^x = 2^n * 10^f, where |f| < 0.5*log10(2)
57+
* - Approximates 10^f using a polynomial expansion from Cephes
6758
*
6859
* @param x input value
69-
* @return 10^x
60+
* @return single-precision result of 10^x
7061
*/
7162
float stdlib_base_exp10f( const float x ) {
7263
float xc;
7364
float px;
74-
float xx;
7565
int32_t n;
7666

7767
if ( stdlib_base_is_nanf( x ) ) {
7868
return 0.0f / 0.0f;
7969
}
80-
if ( x > STDLIB_CONSTANT_FLOAT32_MAX_BASE10_EXPONENT ) {
70+
if ( x > MAXL10 ) {
8171
return STDLIB_CONSTANT_FLOAT32_PINF;
8272
}
73+
if ( x < -MAXL10 ) {
74+
return 0.0f;
75+
}
76+
if ( x == 0.0f ) {
77+
return 1.0f;
78+
}
8379

84-
// Compute n = round(x * log2(10)):
85-
px = stdlib_base_floorf( (LOG210F * x) + 0.5f );
80+
// Range reduction:
81+
px = stdlib_base_floorf( (LOG210 * x) + 0.5f );
8682
n = (int32_t)px;
87-
88-
// Compute f = x - n * log10(2):
8983
xc = x;
90-
xc -= (px * LG102AF);
91-
xc -= (px * LG102BF);
84+
xc -= px * LG102A;
85+
xc -= px * LG102B;
9286

93-
// Evaluate rational approximation for 10^f:
94-
xx = xc * xc;
95-
px = xc * polyval_p( xx );
96-
xc = px / (polyval_q( xx ) - px);
97-
xc = 1.0f + stdlib_base_ldexpf( xc, 1 );
87+
// Polynomial approximation: 10^f ≈ 1 + f * P(f)
88+
px = xc * polyval( xc );
89+
xc = 1.0f + px;
9890

99-
// Return result:
91+
// Multiply by power of 2: 10^x = 10^f * 2^n
10092
return stdlib_base_ldexpf( xc, n );
10193
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ tape( 'the function returns `0.0` for negative large `x`', opts, function test(
172172

173173
tape( 'the function returns `0.0` if provided `-infinity`', opts, function test( t ) {
174174
var val = exp10f( NINF );
175-
t.strictEqual( isPositiveZerof( val ), false, 'returns expected value' );
175+
t.strictEqual( isPositiveZerof( val ), true, 'returns expected value' );
176176
t.end();
177177
});
178178

0 commit comments

Comments
 (0)