@stdlib/math-base-special-ceilsd 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/README.md +98 -12
- package/dist/index.js +3 -3
- package/dist/index.js.map +3 -3
- package/docs/types/index.d.ts +5 -5
- package/include/stdlib/math/base/special/ceilsd.h +40 -0
- package/lib/index.js +3 -3
- package/lib/main.js +12 -22
- package/lib/native.js +60 -0
- package/manifest.json +96 -0
- package/package.json +15 -10
- package/src/addon.c +23 -0
- package/src/main.c +81 -0
package/README.md
CHANGED
|
@@ -53,25 +53,21 @@ npm install @stdlib/math-base-special-ceilsd
|
|
|
53
53
|
var ceilsd = require( '@stdlib/math-base-special-ceilsd' );
|
|
54
54
|
```
|
|
55
55
|
|
|
56
|
-
#### ceilsd( x, n
|
|
56
|
+
#### ceilsd( x, n, b )
|
|
57
57
|
|
|
58
58
|
Rounds a `numeric` value to the nearest `number` toward positive infinity with `n` significant figures.
|
|
59
59
|
|
|
60
60
|
```javascript
|
|
61
|
-
var v = ceilsd( 3.141592653589793, 5 );
|
|
61
|
+
var v = ceilsd( 3.141592653589793, 5, 10 );
|
|
62
62
|
// returns 3.1416
|
|
63
63
|
|
|
64
|
-
v = ceilsd( 3.141592653589793, 1 );
|
|
64
|
+
v = ceilsd( 3.141592653589793, 1, 10 );
|
|
65
65
|
// returns 4.0
|
|
66
66
|
|
|
67
|
-
v = ceilsd( 12368.0, 2 );
|
|
67
|
+
v = ceilsd( 12368.0, 2, 10 );
|
|
68
68
|
// returns 13000.0
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
The default base is `10` (decimal). To round using a different base, provide a third argument.
|
|
72
69
|
|
|
73
|
-
|
|
74
|
-
var v = ceilsd( 0.0313, 2, 2 );
|
|
70
|
+
v = ceilsd( 0.0313, 2, 2 );
|
|
75
71
|
// returns 0.046875
|
|
76
72
|
```
|
|
77
73
|
|
|
@@ -101,7 +97,7 @@ var i;
|
|
|
101
97
|
|
|
102
98
|
for ( i = 0; i < 100; i++ ) {
|
|
103
99
|
x = (randu()*10000.0) - 5000.0;
|
|
104
|
-
y = ceilsd( x, 5 );
|
|
100
|
+
y = ceilsd( x, 5, 10 );
|
|
105
101
|
console.log( 'x: %d. Rounded: %d.', x, y );
|
|
106
102
|
}
|
|
107
103
|
```
|
|
@@ -110,6 +106,96 @@ for ( i = 0; i < 100; i++ ) {
|
|
|
110
106
|
|
|
111
107
|
<!-- /.examples -->
|
|
112
108
|
|
|
109
|
+
<!-- C interface documentation. -->
|
|
110
|
+
|
|
111
|
+
* * *
|
|
112
|
+
|
|
113
|
+
<section class="c">
|
|
114
|
+
|
|
115
|
+
## C APIs
|
|
116
|
+
|
|
117
|
+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
|
|
118
|
+
|
|
119
|
+
<section class="intro">
|
|
120
|
+
|
|
121
|
+
</section>
|
|
122
|
+
|
|
123
|
+
<!-- /.intro -->
|
|
124
|
+
|
|
125
|
+
<!-- C usage documentation. -->
|
|
126
|
+
|
|
127
|
+
<section class="usage">
|
|
128
|
+
|
|
129
|
+
### Usage
|
|
130
|
+
|
|
131
|
+
```c
|
|
132
|
+
#include "stdlib/math/base/special/ceilsd.h"
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
#### stdlib_base_ceilsd( x, n, b )
|
|
136
|
+
|
|
137
|
+
Rounds a `numeric` value to the nearest `number` toward negative infinity with `n` significant figures.
|
|
138
|
+
|
|
139
|
+
```c
|
|
140
|
+
double out = stdlib_base_ceilsd( 0.0313, 2, 2 );
|
|
141
|
+
// returns 0.046875
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
The function accepts the following arguments:
|
|
145
|
+
|
|
146
|
+
- **x**: `[in] double` input value.
|
|
147
|
+
- **n**: `[in] int32_t` number of significant figures.
|
|
148
|
+
- **b**: `[in] int32_t` base.
|
|
149
|
+
|
|
150
|
+
```c
|
|
151
|
+
double stdlib_base_ceilsd( const double x, const int32_t n, const int32_t b );
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
</section>
|
|
155
|
+
|
|
156
|
+
<!-- /.usage -->
|
|
157
|
+
|
|
158
|
+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
|
|
159
|
+
|
|
160
|
+
<section class="notes">
|
|
161
|
+
|
|
162
|
+
</section>
|
|
163
|
+
|
|
164
|
+
<!-- /.notes -->
|
|
165
|
+
|
|
166
|
+
<!-- C API usage examples. -->
|
|
167
|
+
|
|
168
|
+
<section class="examples">
|
|
169
|
+
|
|
170
|
+
### Examples
|
|
171
|
+
|
|
172
|
+
```c
|
|
173
|
+
#include "stdlib/math/base/special/ceilsd.h"
|
|
174
|
+
#include <stdio.h>
|
|
175
|
+
#include <stdint.h>
|
|
176
|
+
|
|
177
|
+
int main( void ) {
|
|
178
|
+
const double x[] = { -5.0, -3.89, -2.78, -1.67, -0.56, 0.56, 1.67, 2.78, 3.89, 5.0 };
|
|
179
|
+
const int32_t n[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
|
|
180
|
+
const int32_t b[] = { 20, 19, 18, 17, 16, 15, 14, 13, 12, 11 };
|
|
181
|
+
|
|
182
|
+
double v;
|
|
183
|
+
int i;
|
|
184
|
+
for ( i = 0; i < 10; i++ ) {
|
|
185
|
+
v = stdlib_base_ceilsd( x[ i ], n[ i ], b[ i ] );
|
|
186
|
+
printf( "ceilsd(%lf, %d, %d) = %lf\n", x[ i ], n[ i ], b[ i ], v );
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
</section>
|
|
192
|
+
|
|
193
|
+
<!-- /.examples -->
|
|
194
|
+
|
|
195
|
+
</section>
|
|
196
|
+
|
|
197
|
+
<!-- /.c -->
|
|
198
|
+
|
|
113
199
|
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
|
|
114
200
|
|
|
115
201
|
<section class="related">
|
|
@@ -166,8 +252,8 @@ Copyright © 2016-2024. The Stdlib [Authors][stdlib-authors].
|
|
|
166
252
|
[npm-image]: http://img.shields.io/npm/v/@stdlib/math-base-special-ceilsd.svg
|
|
167
253
|
[npm-url]: https://npmjs.org/package/@stdlib/math-base-special-ceilsd
|
|
168
254
|
|
|
169
|
-
[test-image]: https://github.com/stdlib-js/math-base-special-ceilsd/actions/workflows/test.yml/badge.svg?branch=v0.
|
|
170
|
-
[test-url]: https://github.com/stdlib-js/math-base-special-ceilsd/actions/workflows/test.yml?query=branch:v0.
|
|
255
|
+
[test-image]: https://github.com/stdlib-js/math-base-special-ceilsd/actions/workflows/test.yml/badge.svg?branch=v0.3.0
|
|
256
|
+
[test-url]: https://github.com/stdlib-js/math-base-special-ceilsd/actions/workflows/test.yml?query=branch:v0.3.0
|
|
171
257
|
|
|
172
258
|
[coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/math-base-special-ceilsd/main.svg
|
|
173
259
|
[coverage-url]: https://codecov.io/github/stdlib-js/math-base-special-ceilsd?branch=main
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
"use strict";var
|
|
2
|
-
var
|
|
3
|
-
});var w=
|
|
1
|
+
"use strict";var c=function(r,e){return function(){return e||r((e={exports:{}}).exports,e),e.exports}};var o=c(function(I,l){
|
|
2
|
+
var n=require('@stdlib/math-base-assert-is-nan/dist'),s=require('@stdlib/math-base-assert-is-infinite/dist'),p=require('@stdlib/math-base-special-pow/dist'),N=require('@stdlib/math-base-special-log10/dist'),f=require('@stdlib/math-base-special-ln/dist'),v=require('@stdlib/math-base-special-abs/dist'),d=require('@stdlib/math-base-special-floor/dist'),g=require('@stdlib/number-float64-base-exponent/dist'),q=require('@stdlib/math-base-special-ceil/dist');function m(r,e,i){var u,a,t;return n(r)||n(e)||e<1||s(e)||n(i)||i<=0||s(i)?NaN:s(r)||r===0||(i===10?u=N(v(r)):i===2?u=g(v(r)):u=f(v(r))/f(i),u=d(u-e+1),a=p(i,v(u)),s(a))||(u<0?t=q(r*a)/a:t=q(r/a)*a,s(t))?r:t}l.exports=m
|
|
3
|
+
});var w=o();module.exports=w;
|
|
4
4
|
/** @license Apache-2.0 */
|
|
5
5
|
//# sourceMappingURL=index.js.map
|
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 ceil = require( '@stdlib/math-base-special-ceil' );\n\n\n// MAIN //\n\n/**\n* Rounds a numeric value to the nearest number toward positive infinity with \\\\(n\\\\) significant figures.\n*\n* @param {number} x - input value\n* @param {PositiveInteger} n - number of significant figures\n* @param {PositiveInteger}
|
|
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,EAAO,QAAS,gCAAiC,EA6BrD,SAASC,EAAQC,EAAGC,EAAGC,EAAI,CAC1B,IAAIC,EACAC,EACAC,
|
|
6
|
-
"names": ["require_main", "__commonJSMin", "exports", "module", "isnan", "isInfinite", "pow", "log10", "ln", "abs", "floor", "exponent", "ceil", "ceilsd", "x", "n", "b", "
|
|
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 ceil = require( '@stdlib/math-base-special-ceil' );\n\n\n// MAIN //\n\n/**\n* Rounds a numeric value to the nearest number toward positive infinity with \\\\(n\\\\) significant figures.\n*\n* @param {number} x - input value\n* @param {PositiveInteger} n - number of significant figures\n* @param {PositiveInteger} b - base\n* @returns {number} rounded value\n*\n* @example\n* var v = ceilsd( 3.141592653589793, 5, 10 );\n* // returns 3.1416\n*\n* @example\n* var v = ceilsd( 3.141592653589793, 1, 10 );\n* // returns 4.0\n*\n* @example\n* var v = ceilsd( 12368.0, 2, 10 );\n* // returns 13000.0\n*\n* @example\n* var v = ceilsd( 0.0313, 2, 2 );\n* // returns 0.046875\n*/\nfunction ceilsd( x, n, b ) {\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\tisnan( b ) ||\n\t\tb <= 0 ||\n\t\tisInfinite( b )\n\t) {\n\t\treturn NaN;\n\t}\n\tif ( isInfinite( x ) || x === 0.0 ) {\n\t\treturn x;\n\t}\n\tif ( b === 10 ) {\n\t\texp = log10( abs( x ) );\n\t}\n\telse if ( b === 2 ) {\n\t\texp = exponent( abs( x ) );\n\t}\n\telse {\n\t\texp = ln( abs(x) ) / ln( b );\n\t}\n\texp = floor( exp - n + 1.0 );\n\ts = pow( b, 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 = ceil( x * s ) / s;\n\t} else {\n\t\ty = ceil( 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 = ceilsd;\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 toward positive infinity with `n` significant figures.\n*\n* @module @stdlib/math-base-special-ceilsd\n*\n* @example\n* var ceilsd = require( '@stdlib/math-base-special-ceilsd' );\n*\n* var v = ceilsd( 3.141592653589793, 5, 10 );\n* // returns 3.1416\n*\n* v = ceilsd( 3.141592653589793, 1, 10 );\n* // returns 4.0\n*\n* v = ceilsd( 12368.0, 2, 10 );\n* // returns 13000.0\n*\n* v = ceilsd( 0.0313, 2, 2 );\n* // returns 0.046875\n*/\n\n// MODULES //\n\nvar main = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = main;\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,EAAK,QAAS,8BAA+B,EAC7CC,EAAM,QAAS,+BAAgC,EAC/CC,EAAQ,QAAS,iCAAkC,EACnDC,EAAW,QAAS,sCAAuC,EAC3DC,EAAO,QAAS,gCAAiC,EA6BrD,SAASC,EAAQC,EAAGC,EAAGC,EAAI,CAC1B,IAAIC,EACAC,EACAC,EACJ,OACCf,EAAOU,CAAE,GACTV,EAAOW,CAAE,GACTA,EAAI,GACJV,EAAYU,CAAE,GACdX,EAAOY,CAAE,GACTA,GAAK,GACLX,EAAYW,CAAE,EAEP,IAEHX,EAAYS,CAAE,GAAKA,IAAM,IAGzBE,IAAM,GACVC,EAAMV,EAAOE,EAAKK,CAAE,CAAE,EAEbE,IAAM,EACfC,EAAMN,EAAUF,EAAKK,CAAE,CAAE,EAGzBG,EAAMT,EAAIC,EAAIK,CAAC,CAAE,EAAIN,EAAIQ,CAAE,EAE5BC,EAAMP,EAAOO,EAAMF,EAAI,CAAI,EAC3BG,EAAIZ,EAAKU,EAAGP,EAAKQ,CAAI,CAAE,EAGlBZ,EAAYa,CAAE,KAIdD,EAAM,EACVE,EAAIP,EAAME,EAAII,CAAE,EAAIA,EAEpBC,EAAIP,EAAME,EAAII,CAAE,EAAIA,EAGhBb,EAAYc,CAAE,GACXL,EAEDK,CACR,CAKAhB,EAAO,QAAUU,IClEjB,IAAIO,EAAO,IAKX,OAAO,QAAUA",
|
|
6
|
+
"names": ["require_main", "__commonJSMin", "exports", "module", "isnan", "isInfinite", "pow", "log10", "ln", "abs", "floor", "exponent", "ceil", "ceilsd", "x", "n", "b", "exp", "s", "y", "main"]
|
|
7
7
|
}
|
package/docs/types/index.d.ts
CHANGED
|
@@ -23,26 +23,26 @@
|
|
|
23
23
|
*
|
|
24
24
|
* @param x - input value
|
|
25
25
|
* @param n - number of significant figures
|
|
26
|
-
* @param b - base
|
|
26
|
+
* @param b - base
|
|
27
27
|
* @returns rounded value
|
|
28
28
|
*
|
|
29
29
|
* @example
|
|
30
|
-
* var v = ceilsd( 3.141592653589793, 5 );
|
|
30
|
+
* var v = ceilsd( 3.141592653589793, 5, 10 );
|
|
31
31
|
* // returns 3.1416
|
|
32
32
|
*
|
|
33
33
|
* @example
|
|
34
|
-
* var v = ceilsd( 3.141592653589793, 1 );
|
|
34
|
+
* var v = ceilsd( 3.141592653589793, 1, 10 );
|
|
35
35
|
* // returns 4.0
|
|
36
36
|
*
|
|
37
37
|
* @example
|
|
38
|
-
* var v = ceilsd( 12368.0, 2 );
|
|
38
|
+
* var v = ceilsd( 12368.0, 2, 10 );
|
|
39
39
|
* // returns 13000.0
|
|
40
40
|
*
|
|
41
41
|
* @example
|
|
42
42
|
* var v = ceilsd( 0.0313, 2, 2 );
|
|
43
43
|
* // returns 0.046875
|
|
44
44
|
*/
|
|
45
|
-
declare function ceilsd( x: number, n: number, b
|
|
45
|
+
declare function ceilsd( x: number, n: number, b: number ): number;
|
|
46
46
|
|
|
47
47
|
|
|
48
48
|
// EXPORTS //
|
|
@@ -0,0 +1,40 @@
|
|
|
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_CEILSD_H
|
|
20
|
+
#define STDLIB_MATH_BASE_SPECIAL_CEILSD_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 numeric value to the nearest number toward positive infinity with \\(n\\) significant figures.
|
|
33
|
+
*/
|
|
34
|
+
double stdlib_base_ceilsd( 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_CEILSD_H
|
package/lib/index.js
CHANGED
|
@@ -26,13 +26,13 @@
|
|
|
26
26
|
* @example
|
|
27
27
|
* var ceilsd = require( '@stdlib/math-base-special-ceilsd' );
|
|
28
28
|
*
|
|
29
|
-
* var v = ceilsd( 3.141592653589793, 5 );
|
|
29
|
+
* var v = ceilsd( 3.141592653589793, 5, 10 );
|
|
30
30
|
* // returns 3.1416
|
|
31
31
|
*
|
|
32
|
-
* v = ceilsd( 3.141592653589793, 1 );
|
|
32
|
+
* v = ceilsd( 3.141592653589793, 1, 10 );
|
|
33
33
|
* // returns 4.0
|
|
34
34
|
*
|
|
35
|
-
* v = ceilsd( 12368.0, 2 );
|
|
35
|
+
* v = ceilsd( 12368.0, 2, 10 );
|
|
36
36
|
* // returns 13000.0
|
|
37
37
|
*
|
|
38
38
|
* v = ceilsd( 0.0313, 2, 2 );
|
package/lib/main.js
CHANGED
|
@@ -38,19 +38,19 @@ var ceil = require( '@stdlib/math-base-special-ceil' );
|
|
|
38
38
|
*
|
|
39
39
|
* @param {number} x - input value
|
|
40
40
|
* @param {PositiveInteger} n - number of significant figures
|
|
41
|
-
* @param {PositiveInteger}
|
|
41
|
+
* @param {PositiveInteger} b - base
|
|
42
42
|
* @returns {number} rounded value
|
|
43
43
|
*
|
|
44
44
|
* @example
|
|
45
|
-
* var v = ceilsd( 3.141592653589793, 5 );
|
|
45
|
+
* var v = ceilsd( 3.141592653589793, 5, 10 );
|
|
46
46
|
* // returns 3.1416
|
|
47
47
|
*
|
|
48
48
|
* @example
|
|
49
|
-
* var v = ceilsd( 3.141592653589793, 1 );
|
|
49
|
+
* var v = ceilsd( 3.141592653589793, 1, 10 );
|
|
50
50
|
* // returns 4.0
|
|
51
51
|
*
|
|
52
52
|
* @example
|
|
53
|
-
* var v = ceilsd( 12368.0, 2 );
|
|
53
|
+
* var v = ceilsd( 12368.0, 2, 10 );
|
|
54
54
|
* // returns 13000.0
|
|
55
55
|
*
|
|
56
56
|
* @example
|
|
@@ -58,7 +58,6 @@ var ceil = require( '@stdlib/math-base-special-ceil' );
|
|
|
58
58
|
* // returns 0.046875
|
|
59
59
|
*/
|
|
60
60
|
function ceilsd( x, n, b ) {
|
|
61
|
-
var base;
|
|
62
61
|
var exp;
|
|
63
62
|
var s;
|
|
64
63
|
var y;
|
|
@@ -66,36 +65,27 @@ function ceilsd( x, n, b ) {
|
|
|
66
65
|
isnan( x ) ||
|
|
67
66
|
isnan( n ) ||
|
|
68
67
|
n < 1 ||
|
|
69
|
-
isInfinite( n )
|
|
68
|
+
isInfinite( n ) ||
|
|
69
|
+
isnan( b ) ||
|
|
70
|
+
b <= 0 ||
|
|
71
|
+
isInfinite( b )
|
|
70
72
|
) {
|
|
71
73
|
return NaN;
|
|
72
74
|
}
|
|
73
|
-
if ( arguments.length > 2 ) {
|
|
74
|
-
if (
|
|
75
|
-
isnan( b ) ||
|
|
76
|
-
b <= 0 ||
|
|
77
|
-
isInfinite( b )
|
|
78
|
-
) {
|
|
79
|
-
return NaN;
|
|
80
|
-
}
|
|
81
|
-
base = b;
|
|
82
|
-
} else {
|
|
83
|
-
base = 10;
|
|
84
|
-
}
|
|
85
75
|
if ( isInfinite( x ) || x === 0.0 ) {
|
|
86
76
|
return x;
|
|
87
77
|
}
|
|
88
|
-
if (
|
|
78
|
+
if ( b === 10 ) {
|
|
89
79
|
exp = log10( abs( x ) );
|
|
90
80
|
}
|
|
91
|
-
else if (
|
|
81
|
+
else if ( b === 2 ) {
|
|
92
82
|
exp = exponent( abs( x ) );
|
|
93
83
|
}
|
|
94
84
|
else {
|
|
95
|
-
exp = ln( abs(x) ) / ln(
|
|
85
|
+
exp = ln( abs(x) ) / ln( b );
|
|
96
86
|
}
|
|
97
87
|
exp = floor( exp - n + 1.0 );
|
|
98
|
-
s = pow(
|
|
88
|
+
s = pow( b, abs( exp ) );
|
|
99
89
|
|
|
100
90
|
// Check for overflow:
|
|
101
91
|
if ( isInfinite( s ) ) {
|
package/lib/native.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
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 number toward positive infinity 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 = ceilsd( 3.141592653589793, 5, 10 );
|
|
39
|
+
* // returns 3.1416
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* var v = ceilsd( 3.141592653589793, 1, 10 );
|
|
43
|
+
* // returns 4.0
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* var v = ceilsd( 12368.0, 2, 10 );
|
|
47
|
+
* // returns 13000.0
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* var v = ceilsd( 0.0313, 2, 2 );
|
|
51
|
+
* // returns 0.046875
|
|
52
|
+
*/
|
|
53
|
+
function ceilsd( x, n, b ) {
|
|
54
|
+
return addon( x, n, b );
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
// EXPORTS //
|
|
59
|
+
|
|
60
|
+
module.exports = ceilsd;
|
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-ternary",
|
|
40
|
+
"@stdlib/math-base-assert-is-nan",
|
|
41
|
+
"@stdlib/math-base-assert-is-infinite",
|
|
42
|
+
"@stdlib/math-base-special-pow",
|
|
43
|
+
"@stdlib/math-base-special-log10",
|
|
44
|
+
"@stdlib/math-base-special-ln",
|
|
45
|
+
"@stdlib/math-base-special-abs",
|
|
46
|
+
"@stdlib/math-base-special-floor",
|
|
47
|
+
"@stdlib/math-base-special-ceil",
|
|
48
|
+
"@stdlib/number-float64-base-exponent"
|
|
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-pow",
|
|
65
|
+
"@stdlib/math-base-special-log10",
|
|
66
|
+
"@stdlib/math-base-special-ln",
|
|
67
|
+
"@stdlib/math-base-special-abs",
|
|
68
|
+
"@stdlib/math-base-special-floor",
|
|
69
|
+
"@stdlib/math-base-special-ceil",
|
|
70
|
+
"@stdlib/number-float64-base-exponent"
|
|
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-pow",
|
|
87
|
+
"@stdlib/math-base-special-log10",
|
|
88
|
+
"@stdlib/math-base-special-ln",
|
|
89
|
+
"@stdlib/math-base-special-abs",
|
|
90
|
+
"@stdlib/math-base-special-floor",
|
|
91
|
+
"@stdlib/math-base-special-ceil",
|
|
92
|
+
"@stdlib/number-float64-base-exponent"
|
|
93
|
+
]
|
|
94
|
+
}
|
|
95
|
+
]
|
|
96
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stdlib/math-base-special-ceilsd",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Round a numeric value to the nearest number toward positive infinity with N significant figures.",
|
|
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/math-base-assert-is-infinite": "^0.2.
|
|
34
|
-
"@stdlib/math-base-assert-is-nan": "^0.2.
|
|
35
|
-
"@stdlib/math-base-
|
|
36
|
-
"@stdlib/math-base-special-
|
|
37
|
-
"@stdlib/math-base-special-
|
|
38
|
-
"@stdlib/math-base-special-
|
|
39
|
-
"@stdlib/math-base-special-
|
|
40
|
-
"@stdlib/math-base-special-
|
|
41
|
-
"@stdlib/
|
|
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.0",
|
|
39
|
+
"@stdlib/math-base-special-abs": "^0.2.2",
|
|
40
|
+
"@stdlib/math-base-special-ceil": "^0.2.2",
|
|
41
|
+
"@stdlib/math-base-special-floor": "^0.2.3",
|
|
42
|
+
"@stdlib/math-base-special-ln": "^0.2.4",
|
|
43
|
+
"@stdlib/math-base-special-log10": "^0.3.0",
|
|
44
|
+
"@stdlib/math-base-special-pow": "^0.3.0",
|
|
45
|
+
"@stdlib/number-float64-base-exponent": "^0.2.2",
|
|
46
|
+
"@stdlib/utils-library-manifest": "^0.2.2"
|
|
42
47
|
},
|
|
43
48
|
"devDependencies": {},
|
|
44
49
|
"engines": {
|
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/ceilsd.h"
|
|
20
|
+
#include "stdlib/math/base/napi/ternary.h"
|
|
21
|
+
|
|
22
|
+
// cppcheck-suppress shadowFunction
|
|
23
|
+
STDLIB_MATH_BASE_NAPI_MODULE_DII_D( stdlib_base_ceilsd )
|
package/src/main.c
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
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/ceilsd.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/pow.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/math/base/special/ceil.h"
|
|
28
|
+
#include "stdlib/number/float64/base/exponent.h"
|
|
29
|
+
#include <stdint.h>
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Rounds a numeric value to the nearest number toward positive infinity 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 out = stdlib_base_ceilsd( 3.141592653589793, 5, 10 );
|
|
41
|
+
* // returns 3.1416
|
|
42
|
+
*/
|
|
43
|
+
double stdlib_base_ceilsd( 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 || b <= 0 ) {
|
|
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( (double)b );
|
|
60
|
+
}
|
|
61
|
+
exp = stdlib_base_floor( exp - (double)n + 1.0 );
|
|
62
|
+
s = stdlib_base_pow( (double)b, stdlib_base_abs( exp ) );
|
|
63
|
+
|
|
64
|
+
// Check for overflow:
|
|
65
|
+
if ( stdlib_base_is_infinite( s ) ) {
|
|
66
|
+
return x;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// 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.
|
|
70
|
+
if ( exp < 0 ) {
|
|
71
|
+
y = stdlib_base_ceil( x * s ) / s;
|
|
72
|
+
} else {
|
|
73
|
+
y = stdlib_base_ceil( x / s ) * s;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Check for overflow:
|
|
77
|
+
if ( stdlib_base_is_infinite( y ) ) {
|
|
78
|
+
return x;
|
|
79
|
+
}
|
|
80
|
+
return y;
|
|
81
|
+
}
|