@stdlib/math-base-special-roundsd 0.2.1 → 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/NOTICE CHANGED
@@ -1 +1 @@
1
- Copyright (c) 2016-2024 The Stdlib Authors.
1
+ Copyright (c) 2016-2026 The Stdlib Authors.
package/README.md CHANGED
@@ -33,7 +33,7 @@ limitations under the License.
33
33
 
34
34
  [![NPM version][npm-image]][npm-url] [![Build Status][test-image]][test-url] [![Coverage Status][coverage-image]][coverage-url] <!-- [![dependencies][dependencies-image]][dependencies-url] -->
35
35
 
36
- > Round a numeric value to the nearest number with `n` significant figures.
36
+ > Round a double-precision floating-point number to the nearest value with `n` significant figures.
37
37
 
38
38
  <section class="installation">
39
39
 
@@ -55,7 +55,7 @@ var roundsd = require( '@stdlib/math-base-special-roundsd' );
55
55
 
56
56
  #### roundsd( x, n\[, b] )
57
57
 
58
- Rounds a `numeric` value to the nearest `number` with `n` significant figures.
58
+ Rounds a double-precision floating-point number to the nearest value with `n` significant figures.
59
59
 
60
60
  ```javascript
61
61
  var v = roundsd( 3.141592653589793, 3 );
@@ -92,17 +92,98 @@ var v = roundsd( 0.0313, 2, 2 );
92
92
  <!-- eslint no-undef: "error" -->
93
93
 
94
94
  ```javascript
95
- var randu = require( '@stdlib/random-base-randu' );
95
+ var uniform = require( '@stdlib/random-array-uniform' );
96
+ var logEachMap = require( '@stdlib/console-log-each-map' );
96
97
  var roundsd = require( '@stdlib/math-base-special-roundsd' );
97
98
 
98
- var x;
99
- var y;
100
- var i;
99
+ var opts = {
100
+ 'dtype': 'float64'
101
+ };
102
+ var x = uniform( 100, -5000.0, 5000.0, opts );
101
103
 
102
- for ( i = 0; i < 100; i++ ) {
103
- x = (randu()*10000.0) - 5000.0;
104
- y = roundsd( x, 5 );
105
- console.log( 'x: %d. Rounded: %d.', x, y );
104
+ logEachMap( 'x: %0.4f. y: %d. Rounded: %0.4f.', x, 5, roundsd );
105
+ ```
106
+
107
+ </section>
108
+
109
+ <!-- /.examples -->
110
+
111
+ <!-- C interface documentation. -->
112
+
113
+ * * *
114
+
115
+ <section class="c">
116
+
117
+ ## C APIs
118
+
119
+ <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
120
+
121
+ <section class="intro">
122
+
123
+ </section>
124
+
125
+ <!-- /.intro -->
126
+
127
+ <!-- C usage documentation. -->
128
+
129
+ <section class="usage">
130
+
131
+ ### Usage
132
+
133
+ ```c
134
+ #include "stdlib/math/base/special/roundsd.h"
135
+ ```
136
+
137
+ #### stdlib_base_roundsd( x, n, b )
138
+
139
+ Rounds a double-precision floating-point number to the nearest value with `n` significant figures.
140
+
141
+ ```c
142
+ double v = stdlib_base_roundsd( 3.141592653589793, 3, 10 );
143
+ // returns 3.14
144
+ ```
145
+
146
+ The function accepts the following arguments:
147
+
148
+ - **x**: `[in] double` input value.
149
+ - **n**: `[in] int32_t` number of significant figures.
150
+ - **b**: `[in] int32_t` base.
151
+
152
+ ```c
153
+ double stdlib_base_roundsd( const double x, const int32_t n, const int32_t b );
154
+ ```
155
+
156
+ </section>
157
+
158
+ <!-- /.usage -->
159
+
160
+ <!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
161
+
162
+ <section class="notes">
163
+
164
+ </section>
165
+
166
+ <!-- /.notes -->
167
+
168
+ <!-- C API usage examples. -->
169
+
170
+ <section class="examples">
171
+
172
+ ### Examples
173
+
174
+ ```c
175
+ #include "stdlib/math/base/special/roundsd.h"
176
+ #include <stdio.h>
177
+
178
+ int main( void ) {
179
+ const double x[] = { 3.143546, -3.142635, 0.0, 0.0/0.0 };
180
+
181
+ double y;
182
+ int i;
183
+ for ( i = 0; i < 4; i++ ) {
184
+ y = stdlib_base_roundsd( x[ i ], 2, 10 );
185
+ printf( "roundsd(%lf) = %lf\n", x[ i ], y );
186
+ }
106
187
  }
107
188
  ```
108
189
 
@@ -110,6 +191,10 @@ for ( i = 0; i < 100; i++ ) {
110
191
 
111
192
  <!-- /.examples -->
112
193
 
194
+ </section>
195
+
196
+ <!-- /.c -->
197
+
113
198
  <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
114
199
 
115
200
  <section class="related">
@@ -153,7 +238,7 @@ See [LICENSE][stdlib-license].
153
238
 
154
239
  ## Copyright
155
240
 
156
- Copyright &copy; 2016-2024. The Stdlib [Authors][stdlib-authors].
241
+ Copyright &copy; 2016-2026. The Stdlib [Authors][stdlib-authors].
157
242
 
158
243
  </section>
159
244
 
@@ -166,8 +251,8 @@ Copyright &copy; 2016-2024. The Stdlib [Authors][stdlib-authors].
166
251
  [npm-image]: http://img.shields.io/npm/v/@stdlib/math-base-special-roundsd.svg
167
252
  [npm-url]: https://npmjs.org/package/@stdlib/math-base-special-roundsd
168
253
 
169
- [test-image]: https://github.com/stdlib-js/math-base-special-roundsd/actions/workflows/test.yml/badge.svg?branch=v0.2.1
170
- [test-url]: https://github.com/stdlib-js/math-base-special-roundsd/actions/workflows/test.yml?query=branch:v0.2.1
254
+ [test-image]: https://github.com/stdlib-js/math-base-special-roundsd/actions/workflows/test.yml/badge.svg?branch=v0.3.0
255
+ [test-url]: https://github.com/stdlib-js/math-base-special-roundsd/actions/workflows/test.yml?query=branch:v0.3.0
171
256
 
172
257
  [coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/math-base-special-roundsd/main.svg
173
258
  [coverage-url]: https://codecov.io/github/stdlib-js/math-base-special-roundsd?branch=main
@@ -179,8 +264,8 @@ Copyright &copy; 2016-2024. The Stdlib [Authors][stdlib-authors].
179
264
 
180
265
  -->
181
266
 
182
- [chat-image]: https://img.shields.io/gitter/room/stdlib-js/stdlib.svg
183
- [chat-url]: https://app.gitter.im/#/room/#stdlib-js_stdlib:gitter.im
267
+ [chat-image]: https://img.shields.io/badge/zulip-join_chat-brightgreen.svg
268
+ [chat-url]: https://stdlib.zulipchat.com
184
269
 
185
270
  [stdlib]: https://github.com/stdlib-js/stdlib
186
271
 
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 log10 = require( '@stdlib/math-base-special-log10' );\nvar ln = require( '@stdlib/math-base-special-ln' );\nvar abs = require( '@stdlib/math-base-special-abs' );\nvar floor = require( '@stdlib/math-base-special-floor' );\nvar exponent = require( '@stdlib/number-float64-base-exponent' );\nvar round = require( '@stdlib/math-base-special-round' );\n\n\n// MAIN //\n\n/**\n* Rounds a numeric value to the nearest number with \\\\(n\\\\) significant figures.\n*\n* @param {number} x - input value\n* @param {PositiveInteger} n - number of significant figures\n* @param {PositiveInteger} [b=10] - base\n* @returns {number} rounded value\n*\n* @example\n* var v = roundsd( 3.141592653589793, 3 );\n* // returns 3.14\n*\n* @example\n* var v = roundsd( 3.141592653589793, 1 );\n* // returns 3.0\n*\n* @example\n* var v = roundsd( 12368.0, 2 );\n* // returns 12000.0\n*\n* @example\n* var v = roundsd( 0.0313, 2, 2 );\n* // returns 0.03125\n*/\nfunction roundsd( x, n, b ) {\n\tvar base;\n\tvar exp;\n\tvar s;\n\tvar y;\n\tif (\n\t\tisnan( x ) ||\n\t\tisnan( n ) ||\n\t\tn < 1 ||\n\t\tisInfinite( n )\n\t) {\n\t\treturn NaN;\n\t}\n\tif ( arguments.length > 2 ) {\n\t\tif (\n\t\t\tisnan( b ) ||\n\t\t\tb <= 0 ||\n\t\t\tisInfinite( b )\n\t\t) {\n\t\t\treturn NaN;\n\t\t}\n\t\tbase = b;\n\t} else {\n\t\tbase = 10;\n\t}\n\tif ( isInfinite( x ) || x === 0.0 ) {\n\t\treturn x;\n\t}\n\tif ( base === 10 ) {\n\t\texp = log10( abs( x ) );\n\t}\n\telse if ( base === 2 ) {\n\t\texp = exponent( abs( x ) );\n\t}\n\telse {\n\t\texp = ln( abs(x) ) / ln( base );\n\t}\n\texp = floor( exp - n + 1.0 );\n\ts = pow( base, abs( exp ) );\n\n\t// Check for overflow:\n\tif ( isInfinite( s ) ) {\n\t\treturn x;\n\t}\n\t// To avoid numerical stability issues due to floating-point rounding error (e.g., 3.55/0.1-35.5 = -7.105427357601e-15 and 3.55*10-35.5 = 0), we must treat positive and negative exponents separately.\n\tif ( exp < 0 ) {\n\t\ty = round( x * s ) / s;\n\t} else {\n\t\ty = round( x / s ) * s;\n\t}\n\t// Check for overflow:\n\tif ( isInfinite( y ) ) {\n\t\treturn x;\n\t}\n\treturn y;\n}\n\n\n// EXPORTS //\n\nmodule.exports = roundsd;\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 number with `n` significant figures.\n*\n* @module @stdlib/math-base-special-roundsd\n*\n* @example\n* var roundsd = require( '@stdlib/math-base-special-roundsd' );\n*\n* var v = roundsd( 3.141592653589793, 3 );\n* // returns 3.14\n*\n* v = roundsd( 3.141592653589793, 1 );\n* // returns 3.0\n*\n* v = roundsd( 12368.0, 2 );\n* // returns 12000.0\n*\n* v = roundsd( 0.0313, 2, 2 );\n* // returns 0.03125\n*/\n\n// MODULES //\n\nvar main = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n"],
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 log10 = require( '@stdlib/math-base-special-log10' );\nvar ln = require( '@stdlib/math-base-special-ln' );\nvar abs = require( '@stdlib/math-base-special-abs' );\nvar floor = require( '@stdlib/math-base-special-floor' );\nvar exponent = require( '@stdlib/number-float64-base-exponent' );\nvar round = require( '@stdlib/math-base-special-round' );\n\n\n// MAIN //\n\n/**\n* Rounds a double-precision floating-point number to the nearest value with `n` significant figures.\n*\n* @param {number} x - input value\n* @param {PositiveInteger} n - number of significant figures\n* @param {PositiveInteger} [b=10] - base\n* @returns {number} rounded value\n*\n* @example\n* var v = roundsd( 3.141592653589793, 3 );\n* // returns 3.14\n*\n* @example\n* var v = roundsd( 3.141592653589793, 1 );\n* // returns 3.0\n*\n* @example\n* var v = roundsd( 12368.0, 2 );\n* // returns 12000.0\n*\n* @example\n* var v = roundsd( 0.0313, 2, 2 );\n* // returns 0.03125\n*/\nfunction roundsd( x, n, b ) {\n\tvar base;\n\tvar exp;\n\tvar s;\n\tvar y;\n\tif (\n\t\tisnan( x ) ||\n\t\tisnan( n ) ||\n\t\tn < 1 ||\n\t\tisInfinite( n )\n\t) {\n\t\treturn NaN;\n\t}\n\tif ( arguments.length > 2 ) {\n\t\tif (\n\t\t\tisnan( b ) ||\n\t\t\tb <= 0 ||\n\t\t\tisInfinite( b )\n\t\t) {\n\t\t\treturn NaN;\n\t\t}\n\t\tbase = b;\n\t} else {\n\t\tbase = 10;\n\t}\n\tif ( isInfinite( x ) || x === 0.0 ) {\n\t\treturn x;\n\t}\n\tif ( base === 10 ) {\n\t\texp = log10( abs( x ) );\n\t}\n\telse if ( base === 2 ) {\n\t\texp = exponent( abs( x ) );\n\t}\n\telse {\n\t\texp = ln( abs(x) ) / ln( base );\n\t}\n\texp = floor( exp - n + 1.0 );\n\ts = pow( base, abs( exp ) );\n\n\t// Check for overflow:\n\tif ( isInfinite( s ) ) {\n\t\treturn x;\n\t}\n\t// To avoid numerical stability issues due to floating-point rounding error (e.g., 3.55/0.1-35.5 = -7.105427357601e-15 and 3.55*10-35.5 = 0), we must treat positive and negative exponents separately.\n\tif ( exp < 0 ) {\n\t\ty = round( x * s ) / s;\n\t} else {\n\t\ty = round( x / s ) * s;\n\t}\n\t// Check for overflow:\n\tif ( isInfinite( y ) ) {\n\t\treturn x;\n\t}\n\treturn y;\n}\n\n\n// EXPORTS //\n\nmodule.exports = roundsd;\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 double-precision floating-point number to the nearest value with `n` significant figures.\n*\n* @module @stdlib/math-base-special-roundsd\n*\n* @example\n* var roundsd = require( '@stdlib/math-base-special-roundsd' );\n*\n* var v = roundsd( 3.141592653589793, 3 );\n* // returns 3.14\n*\n* v = roundsd( 3.141592653589793, 1 );\n* // returns 3.0\n*\n* v = roundsd( 12368.0, 2 );\n* // returns 12000.0\n*\n* v = roundsd( 0.0313, 2, 2 );\n* // returns 0.03125\n*/\n\n// MODULES //\n\nvar main = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n"],
5
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,EAAK,QAAS,8BAA+B,EAC7CC,EAAM,QAAS,+BAAgC,EAC/CC,EAAQ,QAAS,iCAAkC,EACnDC,EAAW,QAAS,sCAAuC,EAC3DC,EAAQ,QAAS,iCAAkC,EA6BvD,SAASC,EAASC,EAAGC,EAAGC,EAAI,CAC3B,IAAIC,EACAC,EACAC,EACAC,EACJ,GACChB,EAAOU,CAAE,GACTV,EAAOW,CAAE,GACTA,EAAI,GACJV,EAAYU,CAAE,EAEd,MAAO,KAER,GAAK,UAAU,OAAS,EAAI,CAC3B,GACCX,EAAOY,CAAE,GACTA,GAAK,GACLX,EAAYW,CAAE,EAEd,MAAO,KAERC,EAAOD,CACR,MACCC,EAAO,GA4BR,OA1BKZ,EAAYS,CAAE,GAAKA,IAAM,IAGzBG,IAAS,GACbC,EAAMX,EAAOE,EAAKK,CAAE,CAAE,EAEbG,IAAS,EAClBC,EAAMP,EAAUF,EAAKK,CAAE,CAAE,EAGzBI,EAAMV,EAAIC,EAAIK,CAAC,CAAE,EAAIN,EAAIS,CAAK,EAE/BC,EAAMR,EAAOQ,EAAMH,EAAI,CAAI,EAC3BI,EAAIb,EAAKW,EAAMR,EAAKS,CAAI,CAAE,EAGrBb,EAAYc,CAAE,KAIdD,EAAM,EACVE,EAAIR,EAAOE,EAAIK,CAAE,EAAIA,EAErBC,EAAIR,EAAOE,EAAIK,CAAE,EAAIA,EAGjBd,EAAYe,CAAE,GACXN,EAEDM,CACR,CAKAjB,EAAO,QAAUU,IC5EjB,IAAIQ,EAAO,IAKX,OAAO,QAAUA",
6
6
  "names": ["require_main", "__commonJSMin", "exports", "module", "isnan", "isInfinite", "pow", "log10", "ln", "abs", "floor", "exponent", "round", "roundsd", "x", "n", "b", "base", "exp", "s", "y", "main"]
7
7
  }
@@ -19,7 +19,7 @@
19
19
  // TypeScript Version: 4.1
20
20
 
21
21
  /**
22
- * Rounds a numeric value to the nearest number with \\(n\\) significant figures.
22
+ * Rounds a double-precision floating-point number to the nearest value with `n` significant figures.
23
23
  *
24
24
  * @param x - input value
25
25
  * @param n - number of significant figures
@@ -0,0 +1,40 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2025 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_ROUNDSD_H
20
+ #define STDLIB_MATH_BASE_SPECIAL_ROUNDSD_H
21
+
22
+ #include <stdint.h>
23
+
24
+ /*
25
+ * If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
26
+ */
27
+ #ifdef __cplusplus
28
+ extern "C" {
29
+ #endif
30
+
31
+ /**
32
+ * Rounds a double-precision floating-point number to the nearest value with `n` significant figures.
33
+ */
34
+ double stdlib_base_roundsd( const double x, const int32_t n, const int32_t b );
35
+
36
+ #ifdef __cplusplus
37
+ }
38
+ #endif
39
+
40
+ #endif // !STDLIB_MATH_BASE_SPECIAL_ROUNDSD_H
package/lib/index.js CHANGED
@@ -19,7 +19,7 @@
19
19
  'use strict';
20
20
 
21
21
  /**
22
- * Round a numeric value to the nearest number with `n` significant figures.
22
+ * Round a double-precision floating-point number to the nearest value with `n` significant figures.
23
23
  *
24
24
  * @module @stdlib/math-base-special-roundsd
25
25
  *
package/lib/main.js CHANGED
@@ -34,7 +34,7 @@ var round = require( '@stdlib/math-base-special-round' );
34
34
  // MAIN //
35
35
 
36
36
  /**
37
- * Rounds a numeric value to the nearest number with \\(n\\) significant figures.
37
+ * Rounds a double-precision floating-point number to the nearest value with `n` significant figures.
38
38
  *
39
39
  * @param {number} x - input value
40
40
  * @param {PositiveInteger} n - number of significant figures
package/lib/native.js ADDED
@@ -0,0 +1,60 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2025 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 double-precision floating-point number to the nearest value with `n` significant figures.
30
+ *
31
+ * @private
32
+ * @param {number} x - input value
33
+ * @param {PositiveInteger} n - number of significant figures
34
+ * @param {PositiveInteger} b - base
35
+ * @returns {number} rounded value
36
+ *
37
+ * @example
38
+ * var v = roundsd( 3.141592653589793, 3, 10 );
39
+ * // returns 3.14
40
+ *
41
+ * @example
42
+ * var v = roundsd( 3.141592653589793, 1, 10 );
43
+ * // returns 3.0
44
+ *
45
+ * @example
46
+ * var v = roundsd( 12368.0, 2, 10 );
47
+ * // returns 12000.0
48
+ *
49
+ * @example
50
+ * var v = roundsd( 0.0313, 2, 2 );
51
+ * // returns 0.03125
52
+ */
53
+ function roundsd( x, n, b ) {
54
+ return addon( x, n, b );
55
+ }
56
+
57
+
58
+ // EXPORTS //
59
+
60
+ module.exports = roundsd;
package/manifest.json ADDED
@@ -0,0 +1,102 @@
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
+ "-lm"
38
+ ],
39
+ "libpath": [],
40
+ "dependencies": [
41
+ "@stdlib/math-base-napi-ternary",
42
+ "@stdlib/math-base-special-abs",
43
+ "@stdlib/math-base-special-pow",
44
+ "@stdlib/math-base-special-log10",
45
+ "@stdlib/math-base-special-ln",
46
+ "@stdlib/math-base-special-floor",
47
+ "@stdlib/number-float64-base-exponent",
48
+ "@stdlib/math-base-assert-is-infinite",
49
+ "@stdlib/math-base-special-round",
50
+ "@stdlib/math-base-assert-is-nan"
51
+ ]
52
+ },
53
+ {
54
+ "task": "benchmark",
55
+ "src": [
56
+ "./src/main.c"
57
+ ],
58
+ "include": [
59
+ "./include"
60
+ ],
61
+ "libraries": [
62
+ "-lm"
63
+ ],
64
+ "libpath": [],
65
+ "dependencies": [
66
+ "@stdlib/math-base-special-abs",
67
+ "@stdlib/math-base-special-pow",
68
+ "@stdlib/math-base-special-log10",
69
+ "@stdlib/math-base-special-ln",
70
+ "@stdlib/math-base-special-floor",
71
+ "@stdlib/number-float64-base-exponent",
72
+ "@stdlib/math-base-assert-is-infinite",
73
+ "@stdlib/math-base-special-round",
74
+ "@stdlib/math-base-assert-is-nan"
75
+ ]
76
+ },
77
+ {
78
+ "task": "examples",
79
+ "src": [
80
+ "./src/main.c"
81
+ ],
82
+ "include": [
83
+ "./include"
84
+ ],
85
+ "libraries": [
86
+ "-lm"
87
+ ],
88
+ "libpath": [],
89
+ "dependencies": [
90
+ "@stdlib/math-base-special-abs",
91
+ "@stdlib/math-base-special-pow",
92
+ "@stdlib/math-base-special-log10",
93
+ "@stdlib/math-base-special-ln",
94
+ "@stdlib/math-base-special-floor",
95
+ "@stdlib/number-float64-base-exponent",
96
+ "@stdlib/math-base-assert-is-infinite",
97
+ "@stdlib/math-base-special-round",
98
+ "@stdlib/math-base-assert-is-nan"
99
+ ]
100
+ }
101
+ ]
102
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@stdlib/math-base-special-roundsd",
3
- "version": "0.2.1",
4
- "description": "Round a numeric value to the nearest number with N significant figures.",
3
+ "version": "0.3.0",
4
+ "description": "Round a double-precision floating-point number to the nearest value with N significant figures.",
5
5
  "license": "Apache-2.0",
6
6
  "author": {
7
7
  "name": "The Stdlib Authors",
@@ -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/math-base-assert-is-infinite": "^0.2.1",
34
- "@stdlib/math-base-assert-is-nan": "^0.2.1",
35
- "@stdlib/math-base-special-abs": "^0.2.1",
36
- "@stdlib/math-base-special-floor": "^0.2.1",
37
- "@stdlib/math-base-special-ln": "^0.2.1",
38
- "@stdlib/math-base-special-log10": "^0.2.1",
39
- "@stdlib/math-base-special-pow": "^0.2.1",
40
- "@stdlib/math-base-special-round": "^0.2.1",
41
- "@stdlib/number-float64-base-exponent": "^0.2.1"
36
+ "@stdlib/math-base-assert-is-infinite": "^0.2.2",
37
+ "@stdlib/math-base-assert-is-nan": "^0.2.2",
38
+ "@stdlib/math-base-napi-ternary": "^0.3.1",
39
+ "@stdlib/math-base-special-abs": "^0.2.2",
40
+ "@stdlib/math-base-special-floor": "^0.2.3",
41
+ "@stdlib/math-base-special-ln": "^0.2.4",
42
+ "@stdlib/math-base-special-log10": "^0.3.0",
43
+ "@stdlib/math-base-special-pow": "^0.3.0",
44
+ "@stdlib/math-base-special-round": "^0.3.0",
45
+ "@stdlib/number-float64-base-exponent": "^0.2.2",
46
+ "@stdlib/utils-library-manifest": "^0.2.3"
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) 2025 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/roundsd.h"
20
+ #include "stdlib/math/base/napi/ternary.h"
21
+
22
+ STDLIB_MATH_BASE_NAPI_MODULE_DII_D( stdlib_base_roundsd )
package/src/main.c ADDED
@@ -0,0 +1,79 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2025 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/roundsd.h"
20
+ #include "stdlib/math/base/assert/is_nan.h"
21
+ #include "stdlib/math/base/special/pow.h"
22
+ #include "stdlib/math/base/assert/is_infinite.h"
23
+ #include "stdlib/math/base/special/log10.h"
24
+ #include "stdlib/math/base/special/ln.h"
25
+ #include "stdlib/math/base/special/abs.h"
26
+ #include "stdlib/math/base/special/floor.h"
27
+ #include "stdlib/number/float64/base/exponent.h"
28
+ #include "stdlib/math/base/special/round.h"
29
+ #include <stdint.h>
30
+
31
+ /**
32
+ * Rounds a double-precision floating-point number to the nearest value with `n` significant figures.
33
+ *
34
+ * @param x input value
35
+ * @param n number of significant figures
36
+ * @param b base
37
+ * @return rounded value
38
+ *
39
+ * @example
40
+ * double v = stdlib_base_roundsd( 3.141592653589793, 3, 10 );
41
+ * // returns 3.14
42
+ */
43
+ double stdlib_base_roundsd( const double x, const int32_t n, const int32_t b ) {
44
+ double exp;
45
+ double s;
46
+ double y;
47
+
48
+ if ( stdlib_base_is_nan( x ) || n < 1 ) {
49
+ return 0.0/0.0; // NaN
50
+ }
51
+ if ( stdlib_base_is_infinite( x ) || x == 0.0 ) {
52
+ return x;
53
+ }
54
+ if ( b == 10 ) {
55
+ exp = stdlib_base_log10( stdlib_base_abs( x ) );
56
+ } else if ( b == 2 ) {
57
+ exp = stdlib_base_float64_exponent( stdlib_base_abs( x ) );
58
+ } else {
59
+ exp = stdlib_base_ln( stdlib_base_abs( x ) ) / stdlib_base_ln( b );
60
+ }
61
+ exp = stdlib_base_floor( exp - n + 1.0 );
62
+ s = stdlib_base_pow( b, stdlib_base_abs( exp ) );
63
+
64
+ // Check for overflow:
65
+ if ( stdlib_base_is_infinite( s ) ) {
66
+ return x;
67
+ }
68
+ // To avoid numerical stability issues due to floating-point rounding error (e.g., 3.55/0.1-35.5 = -7.105427357601e-15 and 3.55*10-35.5 = 0), we must treat positive and negative exponents separately.
69
+ if ( exp < 0 ) {
70
+ y = stdlib_base_round( x * s ) / s;
71
+ } else {
72
+ y = stdlib_base_round( x / s ) * s;
73
+ }
74
+ // Check for overflow:
75
+ if ( stdlib_base_is_infinite( y ) ) {
76
+ return x;
77
+ }
78
+ return y;
79
+ }