Skip to content

Commit 5983a13

Browse files
committed
feat: add math/base/special/roundsdf
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: missing_dependencies - task: lint_c_examples status: missing_dependencies - task: lint_c_benchmarks status: missing_dependencies - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 68446dc commit 5983a13

File tree

24 files changed

+1964
-0
lines changed

24 files changed

+1964
-0
lines changed
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# roundsdf
22+
23+
> Round a single-precision floating-point number to the nearest value with `n` significant figures.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var roundsdf = require( '@stdlib/math/base/special/roundsdf' );
31+
```
32+
33+
#### roundsdf( x, n )
34+
35+
Rounds a single-precision floating-point number to the nearest value with `n` significant figures.
36+
37+
```javascript
38+
var v = roundsdf( 3.141592653589793, 3 );
39+
// returns 3.14
40+
41+
v = roundsdf( 3.141592653589793, 1 );
42+
// returns 3.0
43+
44+
v = roundsdf( 12368.0, 2 );
45+
// returns 12000.0
46+
47+
v = roundsdf( -12368.0, 2 );
48+
// returns -12000.0
49+
```
50+
51+
</section>
52+
53+
<!-- /.usage -->
54+
55+
<section class="notes">
56+
57+
</section>
58+
59+
<!-- /.notes -->
60+
61+
<section class="examples">
62+
63+
## Examples
64+
65+
<!-- eslint no-undef: "error" -->
66+
67+
```javascript
68+
var uniform = require( '@stdlib/random/array/uniform' );
69+
var logEachMap = require( '@stdlib/console/log-each-map' );
70+
var roundsdf = require( '@stdlib/math/base/special/roundsdf' );
71+
72+
var opts = {
73+
'dtype': 'float32'
74+
};
75+
76+
var x = uniform( 100, -5000.0, 5000.0, opts );
77+
78+
logEachMap( 'x: %0.4f. n: 3. rounded: %0.4f.', x, 3, roundsdf );
79+
```
80+
81+
</section>
82+
83+
<!-- /.examples -->
84+
85+
<!-- C interface documentation. -->
86+
87+
* * *
88+
89+
<section class="c">
90+
91+
## C APIs
92+
93+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
94+
95+
<section class="intro">
96+
97+
</section>
98+
99+
<!-- /.intro -->
100+
101+
<!-- C usage documentation. -->
102+
103+
<section class="usage">
104+
105+
### Usage
106+
107+
```c
108+
#include "stdlib/math/base/special/roundsdf.h"
109+
```
110+
111+
#### stdlib_base_roundsdf( x, n )
112+
113+
Rounds a single-precision floating-point number to the nearest value with `n` significant figures
114+
115+
```c
116+
float v = stdlib_base_roundsdf( 3.1415927f, 3 );
117+
// returns 3.14f
118+
```
119+
120+
The function accepts the following arguments:
121+
122+
- **x**: `[in] double` input value.
123+
- **n**: `[in] int32_t` number of significant figures.
124+
125+
```c
126+
float stdlib_base_roundsdf( float x, int32_t n );
127+
```
128+
129+
</section>
130+
131+
<!-- /.usage -->
132+
133+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
134+
135+
<section class="notes">
136+
137+
</section>
138+
139+
<!-- /.notes -->
140+
141+
<!-- C API usage examples. -->
142+
143+
<section class="examples">
144+
145+
### Examples
146+
147+
```c
148+
#include "stdlib/math/base/special/roundsdf.h"
149+
#include <stdio.h>
150+
#include <math.h>
151+
152+
int main( void ) {
153+
const float x[] = { 3.143546f, -3.142635f, 0.0f, NAN };
154+
155+
float y;
156+
int i;
157+
158+
for ( i = 0; i < 4; i++ ) {
159+
y = stdlib_base_roundsdf( x[ i ], 2 );
160+
printf( "roundsdf(%f) = %f\n", x[ i ], y );
161+
}
162+
}
163+
```
164+
165+
</section>
166+
167+
<!-- /.examples -->
168+
169+
</section>
170+
171+
<!-- /.c -->
172+
173+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
174+
175+
<section class="related">
176+
177+
* * *
178+
179+
## See Also
180+
181+
- <span class="package-name">[`@stdlib/math/base/special/ceilsd`][@stdlib/math/base/special/ceilsd]</span><span class="delimiter">: </span><span class="description">round a numeric value to the nearest number toward positive infinity with N significant figures.</span>
182+
- <span class="package-name">[`@stdlib/math/base/special/floorsd`][@stdlib/math/base/special/floorsd]</span><span class="delimiter">: </span><span class="description">round a numeric value to the nearest number toward negative infinity with N significant figures.</span>
183+
- <span class="package-name">[`@stdlib/math/base/special/round`][@stdlib/math/base/special/round]</span><span class="delimiter">: </span><span class="description">round a numeric value to the nearest integer.</span>
184+
- <span class="package-name">[`@stdlib/math/base/special/truncsd`][@stdlib/math/base/special/truncsd]</span><span class="delimiter">: </span><span class="description">round a numeric value to the nearest number toward zero with N significant figures.</span>
185+
186+
</section>
187+
188+
<!-- /.related -->
189+
190+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
191+
192+
<section class="links">
193+
194+
<!-- <related-links> -->
195+
196+
[@stdlib/math/base/special/ceilsd]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/ceilsd
197+
198+
[@stdlib/math/base/special/floorsd]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/floorsd
199+
200+
[@stdlib/math/base/special/round]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/round
201+
202+
[@stdlib/math/base/special/truncsd]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/truncsd
203+
204+
<!-- </related-links> -->
205+
206+
</section>
207+
208+
<!-- /.links -->
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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 bench = require( '@stdlib/bench' );
24+
var randu = require( '@stdlib/random/base/randu' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var format = require( '@stdlib/string/format' );
27+
var pkg = require( './../package.json' ).name;
28+
var roundsdf = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( format( '%s::n=1', pkg ), function benchmark( b ) {
34+
var x;
35+
var y;
36+
var i;
37+
38+
b.tic();
39+
for ( i = 0; i < b.iterations; i++ ) {
40+
x = ( randu() * 10000.0 ) - 5000.0;
41+
y = roundsdf( x, 1 );
42+
}
43+
b.toc();
44+
45+
if ( isnan( y ) ) {
46+
b.fail( 'should not return NaN' );
47+
}
48+
b.pass( 'benchmark finished' );
49+
b.end();
50+
});
51+
52+
bench( format( '%s::n=3', pkg ), function benchmark( b ) {
53+
var x;
54+
var y;
55+
var i;
56+
57+
b.tic();
58+
for ( i = 0; i < b.iterations; i++ ) {
59+
x = ( randu() * 10000.0 ) - 5000.0;
60+
y = roundsdf( x, 3 );
61+
}
62+
b.toc();
63+
64+
if ( isnan( y ) ) {
65+
b.fail( 'should not return NaN' );
66+
}
67+
b.pass( 'benchmark finished' );
68+
b.end();
69+
});
70+
71+
bench( format( '%s::n=5', pkg ), function benchmark( b ) {
72+
var x;
73+
var y;
74+
var i;
75+
76+
b.tic();
77+
for ( i = 0; i < b.iterations; i++ ) {
78+
x = ( randu() * 10000.0 ) - 5000.0;
79+
y = roundsdf( x, 5 );
80+
}
81+
b.toc();
82+
83+
if ( isnan( y ) ) {
84+
b.fail( 'should not return NaN' );
85+
}
86+
b.pass( 'benchmark finished' );
87+
b.end();
88+
});
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
'use strict';
20+
21+
// MODULES //
22+
23+
var resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var randu = require( '@stdlib/random/base/randu' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var format = require( '@stdlib/string/format' );
28+
var tryRequire = require( '@stdlib/utils/try-require' );
29+
var pkg = require( './../package.json' ).name;
30+
31+
32+
// VARIABLES //
33+
34+
var roundsdf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
35+
var opts = {
36+
'skip': ( roundsdf instanceof Error )
37+
};
38+
39+
40+
// MAIN //
41+
42+
bench( format( '%s::native:n=1', pkg ), opts, function benchmark( b ) {
43+
var x;
44+
var y;
45+
var i;
46+
47+
b.tic();
48+
for ( i = 0; i < b.iterations; i++ ) {
49+
x = ( randu() * 10000.0 ) - 5000.0;
50+
y = roundsdf( x, 1 );
51+
}
52+
b.toc();
53+
54+
if ( isnan( y ) ) {
55+
b.fail( 'should not return NaN' );
56+
}
57+
b.pass( 'benchmark finished' );
58+
b.end();
59+
});
60+
61+
bench( format( '%s::native:n=3', pkg ), opts, function benchmark( b ) {
62+
var x;
63+
var y;
64+
var i;
65+
66+
b.tic();
67+
for ( i = 0; i < b.iterations; i++ ) {
68+
x = ( randu() * 10000.0 ) - 5000.0;
69+
y = roundsdf( x, 3 );
70+
}
71+
b.toc();
72+
73+
if ( isnan( y ) ) {
74+
b.fail( 'should not return NaN' );
75+
}
76+
b.pass( 'benchmark finished' );
77+
b.end();
78+
});
79+
80+
bench( format( '%s::native:n=5', pkg ), opts, function benchmark( b ) {
81+
var x;
82+
var y;
83+
var i;
84+
85+
b.tic();
86+
for ( i = 0; i < b.iterations; i++ ) {
87+
x = ( randu() * 10000.0 ) - 5000.0;
88+
y = roundsdf( x, 5 );
89+
}
90+
b.toc();
91+
92+
if ( isnan( y ) ) {
93+
b.fail( 'should not return NaN' );
94+
}
95+
b.pass( 'benchmark finished' );
96+
b.end();
97+
});

0 commit comments

Comments
 (0)