@stdlib/math-base-special-round2 0.2.1 → 0.3.1
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/NOTICE +1 -1
- package/README.md +96 -13
- package/dist/index.js.map +2 -2
- package/include/stdlib/math/base/special/round2.h +38 -0
- package/lib/main.js +4 -1
- package/lib/native.js +54 -0
- package/manifest.json +96 -0
- package/package.json +15 -10
- package/src/addon.c +22 -0
- package/src/main.c +97 -0
package/NOTICE
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Copyright (c) 2016-
|
|
1
|
+
Copyright (c) 2016-2026 The Stdlib Authors.
|
package/README.md
CHANGED
|
@@ -106,17 +106,96 @@ v = round2( NaN );
|
|
|
106
106
|
<!-- eslint no-undef: "error" -->
|
|
107
107
|
|
|
108
108
|
```javascript
|
|
109
|
-
var
|
|
109
|
+
var uniform = require( '@stdlib/random-array-uniform' );
|
|
110
|
+
var logEachMap = require( '@stdlib/console-log-each-map' );
|
|
110
111
|
var round2 = require( '@stdlib/math-base-special-round2' );
|
|
111
112
|
|
|
112
|
-
var
|
|
113
|
-
|
|
114
|
-
|
|
113
|
+
var opts = {
|
|
114
|
+
'dtype': 'float64'
|
|
115
|
+
};
|
|
116
|
+
var x = uniform( 100, -50.0, 50.0, opts );
|
|
115
117
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
118
|
+
logEachMap( 'x: %0.4f. Rounded: %0.4f.', x, round2 );
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
</section>
|
|
122
|
+
|
|
123
|
+
<!-- /.examples -->
|
|
124
|
+
|
|
125
|
+
<!-- C interface documentation. -->
|
|
126
|
+
|
|
127
|
+
* * *
|
|
128
|
+
|
|
129
|
+
<section class="c">
|
|
130
|
+
|
|
131
|
+
## C APIs
|
|
132
|
+
|
|
133
|
+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
|
|
134
|
+
|
|
135
|
+
<section class="intro">
|
|
136
|
+
|
|
137
|
+
</section>
|
|
138
|
+
|
|
139
|
+
<!-- /.intro -->
|
|
140
|
+
|
|
141
|
+
<!-- C usage documentation. -->
|
|
142
|
+
|
|
143
|
+
<section class="usage">
|
|
144
|
+
|
|
145
|
+
### Usage
|
|
146
|
+
|
|
147
|
+
```c
|
|
148
|
+
#include "stdlib/math/base/special/round2.h"
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
#### stdlib_base_round2( x )
|
|
152
|
+
|
|
153
|
+
Rounds a `numeric` value to the nearest power of two on a linear scale.
|
|
154
|
+
|
|
155
|
+
```c
|
|
156
|
+
double out = stdlib_base_round2( -4.2 );
|
|
157
|
+
// returns -4.0
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
The function accepts the following arguments:
|
|
161
|
+
|
|
162
|
+
- **x**: `[in] double` input value.
|
|
163
|
+
|
|
164
|
+
```c
|
|
165
|
+
double stdlib_base_round2( const double x );
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
</section>
|
|
169
|
+
|
|
170
|
+
<!-- /.usage -->
|
|
171
|
+
|
|
172
|
+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
|
|
173
|
+
|
|
174
|
+
<section class="notes">
|
|
175
|
+
|
|
176
|
+
</section>
|
|
177
|
+
|
|
178
|
+
<!-- /.notes -->
|
|
179
|
+
|
|
180
|
+
<!-- C API usage examples. -->
|
|
181
|
+
|
|
182
|
+
<section class="examples">
|
|
183
|
+
|
|
184
|
+
### Examples
|
|
185
|
+
|
|
186
|
+
```c
|
|
187
|
+
#include "stdlib/math/base/special/round2.h"
|
|
188
|
+
#include <stdio.h>
|
|
189
|
+
|
|
190
|
+
int main( void ) {
|
|
191
|
+
const double x[] = { -5.0, -3.89, -2.78, -1.67, -0.56, 0.56, 1.67, 2.78, 3.89, 5.0 };
|
|
192
|
+
|
|
193
|
+
double v;
|
|
194
|
+
int i;
|
|
195
|
+
for ( i = 0; i < 10; i++ ) {
|
|
196
|
+
v = stdlib_base_round2( x[ i ] );
|
|
197
|
+
printf( "round2(%lf) = %lf\n", x[ i ], v );
|
|
198
|
+
}
|
|
120
199
|
}
|
|
121
200
|
```
|
|
122
201
|
|
|
@@ -124,6 +203,10 @@ for ( i = 0; i < 100; i++ ) {
|
|
|
124
203
|
|
|
125
204
|
<!-- /.examples -->
|
|
126
205
|
|
|
206
|
+
</section>
|
|
207
|
+
|
|
208
|
+
<!-- /.c -->
|
|
209
|
+
|
|
127
210
|
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
|
|
128
211
|
|
|
129
212
|
<section class="related">
|
|
@@ -167,7 +250,7 @@ See [LICENSE][stdlib-license].
|
|
|
167
250
|
|
|
168
251
|
## Copyright
|
|
169
252
|
|
|
170
|
-
Copyright © 2016-
|
|
253
|
+
Copyright © 2016-2026. The Stdlib [Authors][stdlib-authors].
|
|
171
254
|
|
|
172
255
|
</section>
|
|
173
256
|
|
|
@@ -180,8 +263,8 @@ Copyright © 2016-2024. The Stdlib [Authors][stdlib-authors].
|
|
|
180
263
|
[npm-image]: http://img.shields.io/npm/v/@stdlib/math-base-special-round2.svg
|
|
181
264
|
[npm-url]: https://npmjs.org/package/@stdlib/math-base-special-round2
|
|
182
265
|
|
|
183
|
-
[test-image]: https://github.com/stdlib-js/math-base-special-round2/actions/workflows/test.yml/badge.svg?branch=v0.
|
|
184
|
-
[test-url]: https://github.com/stdlib-js/math-base-special-round2/actions/workflows/test.yml?query=branch:v0.
|
|
266
|
+
[test-image]: https://github.com/stdlib-js/math-base-special-round2/actions/workflows/test.yml/badge.svg?branch=v0.3.1
|
|
267
|
+
[test-url]: https://github.com/stdlib-js/math-base-special-round2/actions/workflows/test.yml?query=branch:v0.3.1
|
|
185
268
|
|
|
186
269
|
[coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/math-base-special-round2/main.svg
|
|
187
270
|
[coverage-url]: https://codecov.io/github/stdlib-js/math-base-special-round2?branch=main
|
|
@@ -193,8 +276,8 @@ Copyright © 2016-2024. The Stdlib [Authors][stdlib-authors].
|
|
|
193
276
|
|
|
194
277
|
-->
|
|
195
278
|
|
|
196
|
-
[chat-image]: https://img.shields.io/
|
|
197
|
-
[chat-url]: https://
|
|
279
|
+
[chat-image]: https://img.shields.io/badge/zulip-join_chat-brightgreen.svg
|
|
280
|
+
[chat-url]: https://stdlib.zulipchat.com
|
|
198
281
|
|
|
199
282
|
[stdlib]: https://github.com/stdlib-js/stdlib
|
|
200
283
|
|
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 log2 = require( '@stdlib/math-base-special-log2' );\nvar MAX_EXP = require( '@stdlib/constants-float64-max-base2-exponent' );\nvar MIN_EXP_SUBNORMAL = require( '@stdlib/constants-float64-min-base2-exponent-subnormal' );\nvar PINF = require( '@stdlib/constants-float64-pinf' );\n\n\n// VARIABLES //\n\n// 2^1023:\nvar HUGE = pow( 2.0, MAX_EXP );\nvar HALF_HUGE = HUGE / 2.0;\n\n\n// MAIN //\n\n/**\n* Rounds a numeric value to the nearest power of two on a linear scale.\n*\n* @param {number} x - input value\n* @returns {number} rounded value\n*\n* @example\n* var v = round2( 3.141592653589793 );\n* // returns 4.0\n*\n* @example\n* var v = round2( 13.0 );\n* // returns 16.0\n*\n* @example\n* var v = round2( -0.314 );\n* // returns -0.25\n*/\nfunction round2( 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 `2^p = x` for `p`:\n\tp = log2( x );\n\n\t// If provided the smallest subnormal, no rounding possible:\n\tif ( p === MIN_EXP_SUBNORMAL ) {\n\t\treturn x;\n\t}\n\t// Find the previous and next integer powers:\n\tp1 = floor( p );\n\tp2 = ceil( p );\n\n\t// Handle overflow:\n\tif ( p1 === MAX_EXP ) {\n\t\tif ( x - HUGE >= HALF_HUGE ) {\n\t\t\treturn sign * PINF; // sign-preserving\n\t\t}\n\t\treturn sign * HUGE;\n\t}\n\t// Compute previous and next powers of two:\n\ty1 = pow( 2.0, p1 );\n\ty2 = pow( 2.0, p2 );\n\n\t// Find the closest power of two:\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 = round2;\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 two on a linear scale.\n*\n* @module @stdlib/math-base-special-round2\n*\n* @example\n* var round2 = require( '@stdlib/math-base-special-round2' );\n*\n* var v = round2( 3.141592653589793 );\n* // returns 4.0\n*\n* v = round2( 13.0 );\n* // returns 16.0\n*\n* v = round2( -0.314 );\n* // returns -0.25\n*/\n\n// MODULES //\n\nvar round2 = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = round2;\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,EAAO,QAAS,gCAAiC,EACjDC,EAAU,QAAS,8CAA+C,EAClEC,EAAoB,QAAS,wDAAyD,EACtFC,EAAO,QAAS,gCAAiC,EAMjDC,EAAOP,EAAK,EAAKI,CAAQ,EACzBI,EAAYD,EAAO,EAuBvB,SAASE,EAAQC,EAAI,CACpB,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,
|
|
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 log2 = require( '@stdlib/math-base-special-log2' );\nvar MAX_EXP = require( '@stdlib/constants-float64-max-base2-exponent' );\nvar MIN_EXP_SUBNORMAL = require( '@stdlib/constants-float64-min-base2-exponent-subnormal' );\nvar PINF = require( '@stdlib/constants-float64-pinf' );\n\n\n// VARIABLES //\n\n// 2^1023:\nvar HUGE = pow( 2.0, MAX_EXP );\nvar HALF_HUGE = HUGE / 2.0;\n\n\n// MAIN //\n\n/**\n* Rounds a numeric value to the nearest power of two on a linear scale.\n*\n* @param {number} x - input value\n* @returns {number} rounded value\n*\n* @example\n* var v = round2( 3.141592653589793 );\n* // returns 4.0\n*\n* @example\n* var v = round2( 13.0 );\n* // returns 16.0\n*\n* @example\n* var v = round2( -0.314 );\n* // returns -0.25\n*/\nfunction round2( 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 `2^p = x` for `p`:\n\tp = log2( x );\n\n\t// If provided the smallest subnormal, no rounding possible:\n\tif ( p === MIN_EXP_SUBNORMAL ) {\n\t\treturn x;\n\t}\n\n\t// Find the previous and next integer powers:\n\tp1 = floor( p );\n\tp2 = ceil( p );\n\n\t// Handle overflow:\n\tif ( p1 === MAX_EXP ) {\n\t\tif ( x - HUGE >= HALF_HUGE ) {\n\t\t\treturn sign * PINF; // sign-preserving\n\t\t}\n\t\treturn sign * HUGE;\n\t}\n\n\t// Compute previous and next powers of two:\n\ty1 = pow( 2.0, p1 );\n\ty2 = pow( 2.0, p2 );\n\n\t// Find the closest power of two:\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 = round2;\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 two on a linear scale.\n*\n* @module @stdlib/math-base-special-round2\n*\n* @example\n* var round2 = require( '@stdlib/math-base-special-round2' );\n*\n* var v = round2( 3.141592653589793 );\n* // returns 4.0\n*\n* v = round2( 13.0 );\n* // returns 16.0\n*\n* v = round2( -0.314 );\n* // returns -0.25\n*/\n\n// MODULES //\n\nvar round2 = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = round2;\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,EAAO,QAAS,gCAAiC,EACjDC,EAAU,QAAS,8CAA+C,EAClEC,EAAoB,QAAS,wDAAyD,EACtFC,EAAO,QAAS,gCAAiC,EAMjDC,EAAOP,EAAK,EAAKI,CAAQ,EACzBI,EAAYD,EAAO,EAuBvB,SAASE,EAAQC,EAAI,CACpB,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAmBJ,OAjBCnB,EAAOY,CAAE,GACTX,EAAYW,CAAE,GACdA,IAAM,IAIFA,EAAI,GACRA,EAAI,CAACA,EACLC,EAAO,IAEPA,EAAO,EAIRM,EAAId,EAAMO,CAAE,EAGPO,IAAMZ,GACHK,GAIRG,EAAKZ,EAAOgB,CAAE,EACdH,EAAKZ,EAAMe,CAAE,EAGRJ,IAAOT,EACNM,EAAIH,GAAQC,EACTG,EAAOL,EAERK,EAAOJ,GAIfQ,EAAKf,EAAK,EAAKa,CAAG,EAClBG,EAAKhB,EAAK,EAAKc,CAAG,EAGlBF,GAASI,EAAKD,GAAO,EAChBA,EAAKH,EAAOF,EACTC,EAAOI,EAERJ,EAAOK,GACf,CAKAnB,EAAO,QAAUY,IC7EjB,IAAIS,EAAS,IAKb,OAAO,QAAUA",
|
|
6
6
|
"names": ["require_main", "__commonJSMin", "exports", "module", "isnan", "isInfinite", "pow", "floor", "ceil", "log2", "MAX_EXP", "MIN_EXP_SUBNORMAL", "PINF", "HUGE", "HALF_HUGE", "round2", "x", "sign", "half", "p1", "p2", "y1", "y2", "p", "round2"]
|
|
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_ROUND2_H
|
|
20
|
+
#define STDLIB_MATH_BASE_SPECIAL_ROUND2_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 two on a linear scale.
|
|
31
|
+
*/
|
|
32
|
+
double stdlib_base_round2( const double x );
|
|
33
|
+
|
|
34
|
+
#ifdef __cplusplus
|
|
35
|
+
}
|
|
36
|
+
#endif
|
|
37
|
+
|
|
38
|
+
#endif // !STDLIB_MATH_BASE_SPECIAL_ROUND2_H
|
package/lib/main.js
CHANGED
|
@@ -79,6 +79,7 @@ function round2( x ) {
|
|
|
79
79
|
} else {
|
|
80
80
|
sign = 1.0;
|
|
81
81
|
}
|
|
82
|
+
|
|
82
83
|
// Solve the equation `2^p = x` for `p`:
|
|
83
84
|
p = log2( x );
|
|
84
85
|
|
|
@@ -86,6 +87,7 @@ function round2( x ) {
|
|
|
86
87
|
if ( p === MIN_EXP_SUBNORMAL ) {
|
|
87
88
|
return x;
|
|
88
89
|
}
|
|
90
|
+
|
|
89
91
|
// Find the previous and next integer powers:
|
|
90
92
|
p1 = floor( p );
|
|
91
93
|
p2 = ceil( p );
|
|
@@ -97,13 +99,14 @@ function round2( x ) {
|
|
|
97
99
|
}
|
|
98
100
|
return sign * HUGE;
|
|
99
101
|
}
|
|
102
|
+
|
|
100
103
|
// Compute previous and next powers of two:
|
|
101
104
|
y1 = pow( 2.0, p1 );
|
|
102
105
|
y2 = pow( 2.0, p2 );
|
|
103
106
|
|
|
104
107
|
// Find the closest power of two:
|
|
105
108
|
half = ( y2 - y1 ) / 2.0;
|
|
106
|
-
if ( y1+half > x ) {
|
|
109
|
+
if ( y1 + half > x ) {
|
|
107
110
|
return sign * y1;
|
|
108
111
|
}
|
|
109
112
|
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 two on a linear scale.
|
|
30
|
+
*
|
|
31
|
+
* @private
|
|
32
|
+
* @param {number} x - input value
|
|
33
|
+
* @returns {number} rounded value
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* var v = round2( 3.141592653589793 );
|
|
37
|
+
* // returns 4.0
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* var v = round2( 13.0 );
|
|
41
|
+
* // returns 16.0
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* var v = round2( -0.314 );
|
|
45
|
+
* // returns -0.25
|
|
46
|
+
*/
|
|
47
|
+
function round2( x ) {
|
|
48
|
+
return addon( x );
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
// EXPORTS //
|
|
53
|
+
|
|
54
|
+
module.exports = round2;
|
package/manifest.json
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
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-log2",
|
|
46
|
+
"@stdlib/constants-float64-max-base2-exponent",
|
|
47
|
+
"@stdlib/constants-float64-min-base2-exponent-subnormal",
|
|
48
|
+
"@stdlib/constants-float64-pinf"
|
|
49
|
+
]
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
"task": "benchmark",
|
|
53
|
+
"src": [
|
|
54
|
+
"./src/main.c"
|
|
55
|
+
],
|
|
56
|
+
"include": [
|
|
57
|
+
"./include"
|
|
58
|
+
],
|
|
59
|
+
"libraries": [],
|
|
60
|
+
"libpath": [],
|
|
61
|
+
"dependencies": [
|
|
62
|
+
"@stdlib/math-base-assert-is-nan",
|
|
63
|
+
"@stdlib/math-base-assert-is-infinite",
|
|
64
|
+
"@stdlib/math-base-special-floor",
|
|
65
|
+
"@stdlib/math-base-special-pow",
|
|
66
|
+
"@stdlib/math-base-special-ceil",
|
|
67
|
+
"@stdlib/math-base-special-log2",
|
|
68
|
+
"@stdlib/constants-float64-max-base2-exponent",
|
|
69
|
+
"@stdlib/constants-float64-min-base2-exponent-subnormal",
|
|
70
|
+
"@stdlib/constants-float64-pinf"
|
|
71
|
+
]
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
"task": "examples",
|
|
75
|
+
"src": [
|
|
76
|
+
"./src/main.c"
|
|
77
|
+
],
|
|
78
|
+
"include": [
|
|
79
|
+
"./include"
|
|
80
|
+
],
|
|
81
|
+
"libraries": [],
|
|
82
|
+
"libpath": [],
|
|
83
|
+
"dependencies": [
|
|
84
|
+
"@stdlib/math-base-assert-is-nan",
|
|
85
|
+
"@stdlib/math-base-assert-is-infinite",
|
|
86
|
+
"@stdlib/math-base-special-floor",
|
|
87
|
+
"@stdlib/math-base-special-pow",
|
|
88
|
+
"@stdlib/math-base-special-ceil",
|
|
89
|
+
"@stdlib/math-base-special-log2",
|
|
90
|
+
"@stdlib/constants-float64-max-base2-exponent",
|
|
91
|
+
"@stdlib/constants-float64-min-base2-exponent-subnormal",
|
|
92
|
+
"@stdlib/constants-float64-pinf"
|
|
93
|
+
]
|
|
94
|
+
}
|
|
95
|
+
]
|
|
96
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stdlib/math-base-special-round2",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "Round a numeric value to the nearest power of two on a linear scale.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": {
|
|
@@ -14,9 +14,12 @@
|
|
|
14
14
|
}
|
|
15
15
|
],
|
|
16
16
|
"main": "./lib",
|
|
17
|
+
"gypfile": false,
|
|
17
18
|
"directories": {
|
|
18
19
|
"doc": "./docs",
|
|
20
|
+
"include": "./include",
|
|
19
21
|
"lib": "./lib",
|
|
22
|
+
"src": "./src",
|
|
20
23
|
"dist": "./dist"
|
|
21
24
|
},
|
|
22
25
|
"types": "./docs/types",
|
|
@@ -30,15 +33,17 @@
|
|
|
30
33
|
"url": "https://github.com/stdlib-js/stdlib/issues"
|
|
31
34
|
},
|
|
32
35
|
"dependencies": {
|
|
33
|
-
"@stdlib/constants-float64-max-base2-exponent": "^0.2.
|
|
34
|
-
"@stdlib/constants-float64-min-base2-exponent-subnormal": "^0.2.
|
|
35
|
-
"@stdlib/constants-float64-pinf": "^0.2.
|
|
36
|
-
"@stdlib/math-base-assert-is-infinite": "^0.2.
|
|
37
|
-
"@stdlib/math-base-assert-is-nan": "^0.2.
|
|
38
|
-
"@stdlib/math-base-
|
|
39
|
-
"@stdlib/math-base-special-
|
|
40
|
-
"@stdlib/math-base-special-
|
|
41
|
-
"@stdlib/math-base-special-
|
|
36
|
+
"@stdlib/constants-float64-max-base2-exponent": "^0.2.3",
|
|
37
|
+
"@stdlib/constants-float64-min-base2-exponent-subnormal": "^0.2.1",
|
|
38
|
+
"@stdlib/constants-float64-pinf": "^0.2.3",
|
|
39
|
+
"@stdlib/math-base-assert-is-infinite": "^0.2.3",
|
|
40
|
+
"@stdlib/math-base-assert-is-nan": "^0.2.3",
|
|
41
|
+
"@stdlib/math-base-napi-unary": "^0.2.7",
|
|
42
|
+
"@stdlib/math-base-special-ceil": "^0.2.3",
|
|
43
|
+
"@stdlib/math-base-special-floor": "^0.2.4",
|
|
44
|
+
"@stdlib/math-base-special-log2": "^0.3.1",
|
|
45
|
+
"@stdlib/math-base-special-pow": "^0.3.0",
|
|
46
|
+
"@stdlib/utils-library-manifest": "^0.2.4"
|
|
42
47
|
},
|
|
43
48
|
"devDependencies": {},
|
|
44
49
|
"engines": {
|
package/src/addon.c
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
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/round2.h"
|
|
20
|
+
#include "stdlib/math/base/napi/unary.h"
|
|
21
|
+
|
|
22
|
+
STDLIB_MATH_BASE_NAPI_MODULE_D_D( stdlib_base_round2 )
|
package/src/main.c
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
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/round2.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/log2.h"
|
|
26
|
+
#include "stdlib/constants/float64/max_base2_exponent.h"
|
|
27
|
+
#include "stdlib/constants/float64/min_base2_exponent_subnormal.h"
|
|
28
|
+
#include "stdlib/constants/float64/pinf.h"
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Rounds a numeric value to the nearest power of two on a linear scale.
|
|
32
|
+
*
|
|
33
|
+
* @param x input value
|
|
34
|
+
* @return rounded value
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* double out = stdlib_base_round2( 3.141592653589793 );
|
|
38
|
+
* // returns 4.0
|
|
39
|
+
*/
|
|
40
|
+
double stdlib_base_round2( const double x ) {
|
|
41
|
+
double HALF_HUGE_VALUE;
|
|
42
|
+
double HUGE_VALUE;
|
|
43
|
+
double sign;
|
|
44
|
+
double half;
|
|
45
|
+
double p1;
|
|
46
|
+
double p2;
|
|
47
|
+
double y1;
|
|
48
|
+
double y2;
|
|
49
|
+
double xc;
|
|
50
|
+
double p;
|
|
51
|
+
|
|
52
|
+
if ( stdlib_base_is_nan( x ) || stdlib_base_is_infinite( x ) || x == 0.0 ) {
|
|
53
|
+
return x;
|
|
54
|
+
}
|
|
55
|
+
xc = x;
|
|
56
|
+
if ( xc < 0 ) {
|
|
57
|
+
xc = -xc;
|
|
58
|
+
sign = -1.0;
|
|
59
|
+
} else {
|
|
60
|
+
sign = 1.0;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Solve the equation `2^p = x` for `p`:
|
|
64
|
+
p = stdlib_base_log2( xc );
|
|
65
|
+
|
|
66
|
+
// If provided the smallest subnormal, no rounding possible:
|
|
67
|
+
if ( p == STDLIB_CONSTANT_FLOAT64_MIN_BASE2_EXPONENT_SUBNORMAL ) {
|
|
68
|
+
return xc;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Find the previous and next integer powers:
|
|
72
|
+
p1 = stdlib_base_floor( p );
|
|
73
|
+
p2 = stdlib_base_ceil( p );
|
|
74
|
+
|
|
75
|
+
// 2^1023:
|
|
76
|
+
HUGE_VALUE = stdlib_base_pow( 2.0, STDLIB_CONSTANT_FLOAT64_MAX_BASE2_EXPONENT );
|
|
77
|
+
HALF_HUGE_VALUE = HUGE_VALUE / 2.0;
|
|
78
|
+
|
|
79
|
+
// Handle overflow:
|
|
80
|
+
if ( p1 == STDLIB_CONSTANT_FLOAT64_MAX_BASE2_EXPONENT ) {
|
|
81
|
+
if ( xc - HUGE_VALUE >= HALF_HUGE_VALUE ) {
|
|
82
|
+
return sign * STDLIB_CONSTANT_FLOAT64_PINF; // sign-preserving
|
|
83
|
+
}
|
|
84
|
+
return sign * HUGE_VALUE;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Compute previous and next powers of two:
|
|
88
|
+
y1 = stdlib_base_pow( 2.0, p1 );
|
|
89
|
+
y2 = stdlib_base_pow( 2.0, p2 );
|
|
90
|
+
|
|
91
|
+
// Find the closest power of two:
|
|
92
|
+
half = ( y2 - y1 ) / 2.0;
|
|
93
|
+
if ( y1 + half > xc ) {
|
|
94
|
+
return sign * y1;
|
|
95
|
+
}
|
|
96
|
+
return sign * y2;
|
|
97
|
+
}
|