@stdlib/math-base-special-round10 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +87 -2
- package/dist/index.js.map +2 -2
- package/include/stdlib/math/base/special/round10.h +38 -0
- package/lib/main.js +4 -1
- package/lib/native.js +54 -0
- package/manifest.json +93 -0
- package/package.json +17 -31
- package/src/addon.c +23 -0
- package/src/main.c +93 -0
package/README.md
CHANGED
|
@@ -124,6 +124,91 @@ for ( i = 0; i < 100; i++ ) {
|
|
|
124
124
|
|
|
125
125
|
<!-- /.examples -->
|
|
126
126
|
|
|
127
|
+
<!-- C interface documentation. -->
|
|
128
|
+
|
|
129
|
+
* * *
|
|
130
|
+
|
|
131
|
+
<section class="c">
|
|
132
|
+
|
|
133
|
+
## C APIs
|
|
134
|
+
|
|
135
|
+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
|
|
136
|
+
|
|
137
|
+
<section class="intro">
|
|
138
|
+
|
|
139
|
+
</section>
|
|
140
|
+
|
|
141
|
+
<!-- /.intro -->
|
|
142
|
+
|
|
143
|
+
<!-- C usage documentation. -->
|
|
144
|
+
|
|
145
|
+
<section class="usage">
|
|
146
|
+
|
|
147
|
+
### Usage
|
|
148
|
+
|
|
149
|
+
```c
|
|
150
|
+
#include "stdlib/math/base/special/round10.h"
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
#### stdlib_base_round10( x )
|
|
154
|
+
|
|
155
|
+
Rounds a `numeric` value to the nearest power of 10 on a linear scale.
|
|
156
|
+
|
|
157
|
+
```c
|
|
158
|
+
double out = stdlib_base_round10( -4.2 );
|
|
159
|
+
// returns -1.0
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
The function accepts the following arguments:
|
|
163
|
+
|
|
164
|
+
- **x**: `[in] double` input value.
|
|
165
|
+
|
|
166
|
+
```c
|
|
167
|
+
double stdlib_base_round10( const double x );
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
</section>
|
|
171
|
+
|
|
172
|
+
<!-- /.usage -->
|
|
173
|
+
|
|
174
|
+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
|
|
175
|
+
|
|
176
|
+
<section class="notes">
|
|
177
|
+
|
|
178
|
+
</section>
|
|
179
|
+
|
|
180
|
+
<!-- /.notes -->
|
|
181
|
+
|
|
182
|
+
<!-- C API usage examples. -->
|
|
183
|
+
|
|
184
|
+
<section class="examples">
|
|
185
|
+
|
|
186
|
+
### Examples
|
|
187
|
+
|
|
188
|
+
```c
|
|
189
|
+
#include "stdlib/math/base/special/round10.h"
|
|
190
|
+
#include <stdio.h>
|
|
191
|
+
|
|
192
|
+
int main( void ) {
|
|
193
|
+
const double x[] = { -5.0, -3.89, -2.78, -1.67, -0.56, 0.56, 1.67, 2.78, 3.89, 5.0 };
|
|
194
|
+
|
|
195
|
+
double v;
|
|
196
|
+
int i;
|
|
197
|
+
for ( i = 0; i < 10; i++ ) {
|
|
198
|
+
v = stdlib_base_round10( x[ i ] );
|
|
199
|
+
printf( "round10(%lf) = %lf\n", x[ i ], v );
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
</section>
|
|
205
|
+
|
|
206
|
+
<!-- /.examples -->
|
|
207
|
+
|
|
208
|
+
</section>
|
|
209
|
+
|
|
210
|
+
<!-- /.c -->
|
|
211
|
+
|
|
127
212
|
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
|
|
128
213
|
|
|
129
214
|
<section class="related">
|
|
@@ -180,8 +265,8 @@ Copyright © 2016-2024. The Stdlib [Authors][stdlib-authors].
|
|
|
180
265
|
[npm-image]: http://img.shields.io/npm/v/@stdlib/math-base-special-round10.svg
|
|
181
266
|
[npm-url]: https://npmjs.org/package/@stdlib/math-base-special-round10
|
|
182
267
|
|
|
183
|
-
[test-image]: https://github.com/stdlib-js/math-base-special-round10/actions/workflows/test.yml/badge.svg?branch=v0.
|
|
184
|
-
[test-url]: https://github.com/stdlib-js/math-base-special-round10/actions/workflows/test.yml?query=branch:v0.
|
|
268
|
+
[test-image]: https://github.com/stdlib-js/math-base-special-round10/actions/workflows/test.yml/badge.svg?branch=v0.3.0
|
|
269
|
+
[test-url]: https://github.com/stdlib-js/math-base-special-round10/actions/workflows/test.yml?query=branch:v0.3.0
|
|
185
270
|
|
|
186
271
|
[coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/math-base-special-round10/main.svg
|
|
187
272
|
[coverage-url]: https://codecov.io/github/stdlib-js/math-base-special-round10?branch=main
|
package/dist/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../lib/main.js", "../lib/index.js"],
|
|
4
|
-
"sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isnan = require( '@stdlib/math-base-assert-is-nan' );\nvar isInfinite = require( '@stdlib/math-base-assert-is-infinite' );\nvar pow = require( '@stdlib/math-base-special-pow' );\nvar floor = require( '@stdlib/math-base-special-floor' );\nvar ceil = require( '@stdlib/math-base-special-ceil' );\nvar log10 = require( '@stdlib/math-base-special-log10' );\nvar MAX_EXP = require( '@stdlib/constants-float64-max-base10-exponent' );\nvar MIN_EXP_SUBNORMAL = require( '@stdlib/constants-float64-min-base10-exponent-subnormal' );\n\n\n// VARIABLES //\n\n// 10^308:\nvar HUGE = 1.0e308;\n\n// 10^-323\nvar TINY = 1.0e-323;\n\n\n// MAIN //\n\n/**\n* Rounds a numeric value to the nearest power of `10` on a linear scale.\n*\n* @param {number} x - input value\n* @returns {number} rounded value\n*\n* @example\n* var v = round10( 3.141592653589793 );\n* // returns 1.0\n*\n* @example\n* var v = round10( 13.0 );\n* // returns 10.0\n*\n* @example\n* var v = round10( -0.314 );\n* // returns -0.1\n*/\nfunction round10( x ) {\n\tvar sign;\n\tvar half;\n\tvar p1;\n\tvar p2;\n\tvar y1;\n\tvar y2;\n\tvar p;\n\tif (\n\t\tisnan( x ) ||\n\t\tisInfinite( x ) ||\n\t\tx === 0.0\n\t) {\n\t\treturn x;\n\t}\n\tif ( x < 0 ) {\n\t\tx = -x;\n\t\tsign = -1.0;\n\t} else {\n\t\tsign = 1.0;\n\t}\n\t// Solve the equation `10^p = x` for `p`:\n\tp = log10( x );\n\n\t// Find the previous and next integer powers:\n\tp1 = floor( p );\n\tp2 = ceil( p );\n\n\t// Handle tiny:\n\tif ( p1 === MIN_EXP_SUBNORMAL ) {\n\t\treturn sign * TINY;\n\t}\n\t// Handle huge:\n\tif ( p1 === MAX_EXP ) {\n\t\treturn sign * HUGE;\n\t}\n\t// Compute previous and next powers of 10:\n\ty1 = pow( 10.0, p1 );\n\ty2 = pow( 10.0, p2 );\n\n\t// Find the closest power of 10:\n\thalf = ( y2 - y1 ) / 2.0;\n\tif ( y1+half > x ) {\n\t\treturn sign * y1;\n\t}\n\treturn sign * y2;\n}\n\n\n// EXPORTS //\n\nmodule.exports = round10;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Round a numeric value to the nearest power of `10` on a linear scale.\n*\n* @module @stdlib/math-base-special-round10\n*\n* @example\n* var round10 = require( '@stdlib/math-base-special-round10' );\n*\n* var v = round10( 3.141592653589793 );\n* // returns 1.0\n*\n* v = round10( 13.0 );\n* // returns 10.0\n*\n* v = round10( -0.314 );\n* // returns -0.1\n*/\n\n// MODULES //\n\nvar round10 = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = round10;\n"],
|
|
5
|
-
"mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAQ,QAAS,iCAAkC,EACnDC,EAAa,QAAS,sCAAuC,EAC7DC,EAAM,QAAS,+BAAgC,EAC/CC,EAAQ,QAAS,iCAAkC,EACnDC,EAAO,QAAS,gCAAiC,EACjDC,EAAQ,QAAS,iCAAkC,EACnDC,EAAU,QAAS,+CAAgD,EACnEC,EAAoB,QAAS,yDAA0D,EAMvFC,EAAO,MAGPC,EAAO,OAuBX,SAASC,EAASC,EAAI,CACrB,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACJ,OACClB,EAAOW,CAAE,GACTV,EAAYU,CAAE,GACdA,IAAM,EAECA,GAEHA,EAAI,GACRA,EAAI,CAACA,EACLC,EAAO,IAEPA,EAAO,
|
|
4
|
+
"sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isnan = require( '@stdlib/math-base-assert-is-nan' );\nvar isInfinite = require( '@stdlib/math-base-assert-is-infinite' );\nvar pow = require( '@stdlib/math-base-special-pow' );\nvar floor = require( '@stdlib/math-base-special-floor' );\nvar ceil = require( '@stdlib/math-base-special-ceil' );\nvar log10 = require( '@stdlib/math-base-special-log10' );\nvar MAX_EXP = require( '@stdlib/constants-float64-max-base10-exponent' );\nvar MIN_EXP_SUBNORMAL = require( '@stdlib/constants-float64-min-base10-exponent-subnormal' );\n\n\n// VARIABLES //\n\n// 10^308:\nvar HUGE = 1.0e308;\n\n// 10^-323\nvar TINY = 1.0e-323;\n\n\n// MAIN //\n\n/**\n* Rounds a numeric value to the nearest power of `10` on a linear scale.\n*\n* @param {number} x - input value\n* @returns {number} rounded value\n*\n* @example\n* var v = round10( 3.141592653589793 );\n* // returns 1.0\n*\n* @example\n* var v = round10( 13.0 );\n* // returns 10.0\n*\n* @example\n* var v = round10( -0.314 );\n* // returns -0.1\n*/\nfunction round10( x ) {\n\tvar sign;\n\tvar half;\n\tvar p1;\n\tvar p2;\n\tvar y1;\n\tvar y2;\n\tvar p;\n\tif (\n\t\tisnan( x ) ||\n\t\tisInfinite( x ) ||\n\t\tx === 0.0\n\t) {\n\t\treturn x;\n\t}\n\tif ( x < 0 ) {\n\t\tx = -x;\n\t\tsign = -1.0;\n\t} else {\n\t\tsign = 1.0;\n\t}\n\n\t// Solve the equation `10^p = x` for `p`:\n\tp = log10( x );\n\n\t// Find the previous and next integer powers:\n\tp1 = floor( p );\n\tp2 = ceil( p );\n\n\t// Handle tiny:\n\tif ( p1 === MIN_EXP_SUBNORMAL ) {\n\t\treturn sign * TINY;\n\t}\n\n\t// Handle huge:\n\tif ( p1 === MAX_EXP ) {\n\t\treturn sign * HUGE;\n\t}\n\n\t// Compute previous and next powers of 10:\n\ty1 = pow( 10.0, p1 );\n\ty2 = pow( 10.0, p2 );\n\n\t// Find the closest power of 10:\n\thalf = ( y2 - y1 ) / 2.0;\n\tif ( y1 + half > x ) {\n\t\treturn sign * y1;\n\t}\n\treturn sign * y2;\n}\n\n\n// EXPORTS //\n\nmodule.exports = round10;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Round a numeric value to the nearest power of `10` on a linear scale.\n*\n* @module @stdlib/math-base-special-round10\n*\n* @example\n* var round10 = require( '@stdlib/math-base-special-round10' );\n*\n* var v = round10( 3.141592653589793 );\n* // returns 1.0\n*\n* v = round10( 13.0 );\n* // returns 10.0\n*\n* v = round10( -0.314 );\n* // returns -0.1\n*/\n\n// MODULES //\n\nvar round10 = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = round10;\n"],
|
|
5
|
+
"mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAQ,QAAS,iCAAkC,EACnDC,EAAa,QAAS,sCAAuC,EAC7DC,EAAM,QAAS,+BAAgC,EAC/CC,EAAQ,QAAS,iCAAkC,EACnDC,EAAO,QAAS,gCAAiC,EACjDC,EAAQ,QAAS,iCAAkC,EACnDC,EAAU,QAAS,+CAAgD,EACnEC,EAAoB,QAAS,yDAA0D,EAMvFC,EAAO,MAGPC,EAAO,OAuBX,SAASC,EAASC,EAAI,CACrB,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACJ,OACClB,EAAOW,CAAE,GACTV,EAAYU,CAAE,GACdA,IAAM,EAECA,GAEHA,EAAI,GACRA,EAAI,CAACA,EACLC,EAAO,IAEPA,EAAO,EAIRM,EAAIb,EAAOM,CAAE,EAGbG,EAAKX,EAAOe,CAAE,EACdH,EAAKX,EAAMc,CAAE,EAGRJ,IAAOP,EACJK,EAAOH,EAIVK,IAAOR,EACJM,EAAOJ,GAIfQ,EAAKd,EAAK,GAAMY,CAAG,EACnBG,EAAKf,EAAK,GAAMa,CAAG,EAGnBF,GAASI,EAAKD,GAAO,EAChBA,EAAKH,EAAOF,EACTC,EAAOI,EAERJ,EAAOK,GACf,CAKAlB,EAAO,QAAUW,IC3EjB,IAAIS,EAAU,IAKd,OAAO,QAAUA",
|
|
6
6
|
"names": ["require_main", "__commonJSMin", "exports", "module", "isnan", "isInfinite", "pow", "floor", "ceil", "log10", "MAX_EXP", "MIN_EXP_SUBNORMAL", "HUGE", "TINY", "round10", "x", "sign", "half", "p1", "p2", "y1", "y2", "p", "round10"]
|
|
7
7
|
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2024 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
|
+
#ifndef STDLIB_MATH_BASE_SPECIAL_ROUND10_H
|
|
20
|
+
#define STDLIB_MATH_BASE_SPECIAL_ROUND10_H
|
|
21
|
+
|
|
22
|
+
/*
|
|
23
|
+
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
|
|
24
|
+
*/
|
|
25
|
+
#ifdef __cplusplus
|
|
26
|
+
extern "C" {
|
|
27
|
+
#endif
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Rounds a numeric value to the nearest power of `10` on a linear scale.
|
|
31
|
+
*/
|
|
32
|
+
double stdlib_base_round10( const double x );
|
|
33
|
+
|
|
34
|
+
#ifdef __cplusplus
|
|
35
|
+
}
|
|
36
|
+
#endif
|
|
37
|
+
|
|
38
|
+
#endif // !STDLIB_MATH_BASE_SPECIAL_ROUND10_H
|
package/lib/main.js
CHANGED
|
@@ -80,6 +80,7 @@ function round10( x ) {
|
|
|
80
80
|
} else {
|
|
81
81
|
sign = 1.0;
|
|
82
82
|
}
|
|
83
|
+
|
|
83
84
|
// Solve the equation `10^p = x` for `p`:
|
|
84
85
|
p = log10( x );
|
|
85
86
|
|
|
@@ -91,17 +92,19 @@ function round10( x ) {
|
|
|
91
92
|
if ( p1 === MIN_EXP_SUBNORMAL ) {
|
|
92
93
|
return sign * TINY;
|
|
93
94
|
}
|
|
95
|
+
|
|
94
96
|
// Handle huge:
|
|
95
97
|
if ( p1 === MAX_EXP ) {
|
|
96
98
|
return sign * HUGE;
|
|
97
99
|
}
|
|
100
|
+
|
|
98
101
|
// Compute previous and next powers of 10:
|
|
99
102
|
y1 = pow( 10.0, p1 );
|
|
100
103
|
y2 = pow( 10.0, p2 );
|
|
101
104
|
|
|
102
105
|
// Find the closest power of 10:
|
|
103
106
|
half = ( y2 - y1 ) / 2.0;
|
|
104
|
-
if ( y1+half > x ) {
|
|
107
|
+
if ( y1 + half > x ) {
|
|
105
108
|
return sign * y1;
|
|
106
109
|
}
|
|
107
110
|
return sign * y2;
|
package/lib/native.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2024 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
|
+
'use strict';
|
|
20
|
+
|
|
21
|
+
// MODULES //
|
|
22
|
+
|
|
23
|
+
var addon = require( './../src/addon.node' );
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
// MAIN //
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Rounds a numeric value to the nearest power of `10` on a linear scale.
|
|
30
|
+
*
|
|
31
|
+
* @private
|
|
32
|
+
* @param {number} x - input value
|
|
33
|
+
* @returns {number} rounded value
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* var v = round10( 3.141592653589793 );
|
|
37
|
+
* // returns 1.0
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* var v = round10( 13.0 );
|
|
41
|
+
* // returns 10.0
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* var v = round10( -0.314 );
|
|
45
|
+
* // returns -0.1
|
|
46
|
+
*/
|
|
47
|
+
function round10( x ) {
|
|
48
|
+
return addon( x );
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
// EXPORTS //
|
|
53
|
+
|
|
54
|
+
module.exports = round10;
|
package/manifest.json
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
{
|
|
2
|
+
"options": {
|
|
3
|
+
"task": "build"
|
|
4
|
+
},
|
|
5
|
+
"fields": [
|
|
6
|
+
{
|
|
7
|
+
"field": "src",
|
|
8
|
+
"resolve": true,
|
|
9
|
+
"relative": true
|
|
10
|
+
},
|
|
11
|
+
{
|
|
12
|
+
"field": "include",
|
|
13
|
+
"resolve": true,
|
|
14
|
+
"relative": true
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
"field": "libraries",
|
|
18
|
+
"resolve": false,
|
|
19
|
+
"relative": false
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
"field": "libpath",
|
|
23
|
+
"resolve": true,
|
|
24
|
+
"relative": false
|
|
25
|
+
}
|
|
26
|
+
],
|
|
27
|
+
"confs": [
|
|
28
|
+
{
|
|
29
|
+
"task": "build",
|
|
30
|
+
"src": [
|
|
31
|
+
"./src/main.c"
|
|
32
|
+
],
|
|
33
|
+
"include": [
|
|
34
|
+
"./include"
|
|
35
|
+
],
|
|
36
|
+
"libraries": [],
|
|
37
|
+
"libpath": [],
|
|
38
|
+
"dependencies": [
|
|
39
|
+
"@stdlib/math-base-napi-unary",
|
|
40
|
+
"@stdlib/math-base-assert-is-nan",
|
|
41
|
+
"@stdlib/math-base-assert-is-infinite",
|
|
42
|
+
"@stdlib/math-base-special-floor",
|
|
43
|
+
"@stdlib/math-base-special-pow",
|
|
44
|
+
"@stdlib/math-base-special-ceil",
|
|
45
|
+
"@stdlib/math-base-special-log10",
|
|
46
|
+
"@stdlib/constants-float64-max-base10-exponent",
|
|
47
|
+
"@stdlib/constants-float64-min-base10-exponent-subnormal"
|
|
48
|
+
]
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
"task": "benchmark",
|
|
52
|
+
"src": [
|
|
53
|
+
"./src/main.c"
|
|
54
|
+
],
|
|
55
|
+
"include": [
|
|
56
|
+
"./include"
|
|
57
|
+
],
|
|
58
|
+
"libraries": [],
|
|
59
|
+
"libpath": [],
|
|
60
|
+
"dependencies": [
|
|
61
|
+
"@stdlib/math-base-assert-is-nan",
|
|
62
|
+
"@stdlib/math-base-assert-is-infinite",
|
|
63
|
+
"@stdlib/math-base-special-floor",
|
|
64
|
+
"@stdlib/math-base-special-pow",
|
|
65
|
+
"@stdlib/math-base-special-ceil",
|
|
66
|
+
"@stdlib/math-base-special-log10",
|
|
67
|
+
"@stdlib/constants-float64-max-base10-exponent",
|
|
68
|
+
"@stdlib/constants-float64-min-base10-exponent-subnormal"
|
|
69
|
+
]
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
"task": "examples",
|
|
73
|
+
"src": [
|
|
74
|
+
"./src/main.c"
|
|
75
|
+
],
|
|
76
|
+
"include": [
|
|
77
|
+
"./include"
|
|
78
|
+
],
|
|
79
|
+
"libraries": [],
|
|
80
|
+
"libpath": [],
|
|
81
|
+
"dependencies": [
|
|
82
|
+
"@stdlib/math-base-assert-is-nan",
|
|
83
|
+
"@stdlib/math-base-assert-is-infinite",
|
|
84
|
+
"@stdlib/math-base-special-floor",
|
|
85
|
+
"@stdlib/math-base-special-pow",
|
|
86
|
+
"@stdlib/math-base-special-ceil",
|
|
87
|
+
"@stdlib/math-base-special-log10",
|
|
88
|
+
"@stdlib/constants-float64-max-base10-exponent",
|
|
89
|
+
"@stdlib/constants-float64-min-base10-exponent-subnormal"
|
|
90
|
+
]
|
|
91
|
+
}
|
|
92
|
+
]
|
|
93
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stdlib/math-base-special-round10",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Round a numeric value to the nearest power of 10 on a linear scale.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": {
|
|
@@ -14,20 +14,16 @@
|
|
|
14
14
|
}
|
|
15
15
|
],
|
|
16
16
|
"main": "./lib",
|
|
17
|
+
"gypfile": false,
|
|
17
18
|
"directories": {
|
|
18
|
-
"benchmark": "./benchmark",
|
|
19
19
|
"doc": "./docs",
|
|
20
|
-
"
|
|
20
|
+
"include": "./include",
|
|
21
21
|
"lib": "./lib",
|
|
22
|
-
"
|
|
22
|
+
"src": "./src",
|
|
23
|
+
"dist": "./dist"
|
|
23
24
|
},
|
|
24
25
|
"types": "./docs/types",
|
|
25
|
-
"scripts": {
|
|
26
|
-
"test": "make test",
|
|
27
|
-
"test-cov": "make test-cov",
|
|
28
|
-
"examples": "make examples",
|
|
29
|
-
"benchmark": "make benchmark"
|
|
30
|
-
},
|
|
26
|
+
"scripts": {},
|
|
31
27
|
"homepage": "https://stdlib.io",
|
|
32
28
|
"repository": {
|
|
33
29
|
"type": "git",
|
|
@@ -37,28 +33,18 @@
|
|
|
37
33
|
"url": "https://github.com/stdlib-js/stdlib/issues"
|
|
38
34
|
},
|
|
39
35
|
"dependencies": {
|
|
40
|
-
"@stdlib/constants-float64-max-base10-exponent": "^0.2.
|
|
41
|
-
"@stdlib/constants-float64-min-base10-exponent-subnormal": "^0.2.
|
|
42
|
-
"@stdlib/math-base-assert-is-infinite": "^0.2.
|
|
43
|
-
"@stdlib/math-base-assert-is-nan": "^0.2.
|
|
44
|
-
"@stdlib/math-base-
|
|
45
|
-
"@stdlib/math-base-special-
|
|
46
|
-
"@stdlib/math-base-special-
|
|
47
|
-
"@stdlib/math-base-special-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
"@stdlib/constants-float64-ninf": "^0.1.1",
|
|
51
|
-
"@stdlib/constants-float64-pinf": "^0.1.1",
|
|
52
|
-
"@stdlib/constants-float64-smallest-subnormal": "^0.2.0",
|
|
53
|
-
"@stdlib/math-base-assert-is-negative-zero": "^0.1.1",
|
|
54
|
-
"@stdlib/math-base-assert-is-positive-zero": "^0.1.1",
|
|
55
|
-
"@stdlib/random-base-randu": "^0.1.0",
|
|
56
|
-
"tape": "git+https://github.com/kgryte/tape.git#fix/globby",
|
|
57
|
-
"istanbul": "^0.4.1",
|
|
58
|
-
"tap-min": "git+https://github.com/Planeshifter/tap-min.git",
|
|
59
|
-
"@stdlib/bench-harness": "^0.2.0",
|
|
60
|
-
"@stdlib/bench": "^0.3.1"
|
|
36
|
+
"@stdlib/constants-float64-max-base10-exponent": "^0.2.2",
|
|
37
|
+
"@stdlib/constants-float64-min-base10-exponent-subnormal": "^0.2.1",
|
|
38
|
+
"@stdlib/math-base-assert-is-infinite": "^0.2.2",
|
|
39
|
+
"@stdlib/math-base-assert-is-nan": "^0.2.2",
|
|
40
|
+
"@stdlib/math-base-napi-unary": "^0.2.3",
|
|
41
|
+
"@stdlib/math-base-special-ceil": "^0.2.2",
|
|
42
|
+
"@stdlib/math-base-special-floor": "^0.2.3",
|
|
43
|
+
"@stdlib/math-base-special-log10": "^0.3.0",
|
|
44
|
+
"@stdlib/math-base-special-pow": "^0.3.0",
|
|
45
|
+
"@stdlib/utils-library-manifest": "^0.2.2"
|
|
61
46
|
},
|
|
47
|
+
"devDependencies": {},
|
|
62
48
|
"engines": {
|
|
63
49
|
"node": ">=0.10.0",
|
|
64
50
|
"npm": ">2.7.0"
|
package/src/addon.c
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2024 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
|
+
#include "stdlib/math/base/special/round10.h"
|
|
20
|
+
#include "stdlib/math/base/napi/unary.h"
|
|
21
|
+
|
|
22
|
+
// cppcheck-suppress shadowFunction
|
|
23
|
+
STDLIB_MATH_BASE_NAPI_MODULE_D_D( stdlib_base_round10 )
|
package/src/main.c
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2024 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
|
+
#include "stdlib/math/base/special/round10.h"
|
|
20
|
+
#include "stdlib/math/base/assert/is_nan.h"
|
|
21
|
+
#include "stdlib/math/base/assert/is_infinite.h"
|
|
22
|
+
#include "stdlib/math/base/special/floor.h"
|
|
23
|
+
#include "stdlib/math/base/special/pow.h"
|
|
24
|
+
#include "stdlib/math/base/special/ceil.h"
|
|
25
|
+
#include "stdlib/math/base/special/log10.h"
|
|
26
|
+
#include "stdlib/constants/float64/max_base10_exponent.h"
|
|
27
|
+
#include "stdlib/constants/float64/min_base10_exponent_subnormal.h"
|
|
28
|
+
|
|
29
|
+
// 10^308:
|
|
30
|
+
static const double HUGE_VALUE = 1.0e308;
|
|
31
|
+
|
|
32
|
+
// 10^-323
|
|
33
|
+
static const double TINY_VALUE = 1.0e-323;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Rounds a numeric value to the nearest power of `10` on a linear scale.
|
|
37
|
+
*
|
|
38
|
+
* @param x input value
|
|
39
|
+
* @return rounded value
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* double out = stdlib_base_round10( 3.141592653589793 );
|
|
43
|
+
* // returns 1.0
|
|
44
|
+
*/
|
|
45
|
+
double stdlib_base_round10( const double x ) {
|
|
46
|
+
double sign;
|
|
47
|
+
double half;
|
|
48
|
+
double p1;
|
|
49
|
+
double p2;
|
|
50
|
+
double y1;
|
|
51
|
+
double y2;
|
|
52
|
+
double xc;
|
|
53
|
+
double p;
|
|
54
|
+
|
|
55
|
+
if ( stdlib_base_is_nan( x ) || stdlib_base_is_infinite( x ) || x == 0.0 ) {
|
|
56
|
+
return x;
|
|
57
|
+
}
|
|
58
|
+
xc = x;
|
|
59
|
+
if ( xc < 0 ) {
|
|
60
|
+
xc = -xc;
|
|
61
|
+
sign = -1.0;
|
|
62
|
+
} else {
|
|
63
|
+
sign = 1.0;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Solve the equation `10^p = x` for `p`:
|
|
67
|
+
p = stdlib_base_log10( xc );
|
|
68
|
+
|
|
69
|
+
// Find the previous and next integer powers:
|
|
70
|
+
p1 = stdlib_base_floor( p );
|
|
71
|
+
p2 = stdlib_base_ceil( p );
|
|
72
|
+
|
|
73
|
+
// Handle tiny:
|
|
74
|
+
if ( p1 == STDLIB_CONSTANT_FLOAT64_MIN_BASE10_EXPONENT_SUBNORMAL ) {
|
|
75
|
+
return sign * TINY_VALUE;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Handle huge:
|
|
79
|
+
if ( p1 == STDLIB_CONSTANT_FLOAT64_MAX_BASE10_EXPONENT ) {
|
|
80
|
+
return sign * HUGE_VALUE;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Compute previous and next powers of 10:
|
|
84
|
+
y1 = stdlib_base_pow( 10.0, p1 );
|
|
85
|
+
y2 = stdlib_base_pow( 10.0, p2 );
|
|
86
|
+
|
|
87
|
+
// Find the closest power of 10:
|
|
88
|
+
half = ( y2 - y1 ) / 2.0;
|
|
89
|
+
if ( y1 + half > xc ) {
|
|
90
|
+
return sign * y1;
|
|
91
|
+
}
|
|
92
|
+
return sign * y2;
|
|
93
|
+
}
|