Skip to content

Commit 0aa9703

Browse files
committed
feat: add math/base/special/round10f
--- 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 583bc84 commit 0aa9703

18 files changed

Lines changed: 1261 additions & 0 deletions

File tree

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2018 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+
# round10f
22+
23+
> Round a numeric value to the nearest multiple of \\(10^n\\) using single-precision floating-point arithmetic.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var round10f = require( '@stdlib/math/base/special/round10f' );
31+
```
32+
33+
#### round10f( v, n )
34+
35+
Rounds a numeric value to the nearest multiple of 10^n usingsingle-precision floating-point arithmetic.
36+
37+
```javascript
38+
var y;
39+
40+
y = round10f( 3.1415926, -2 );
41+
// returns 1
42+
43+
y = round10f( 3.1415926, 0 );
44+
// returns 1
45+
46+
y = round10f( 123.456, 1 );
47+
// returns 100
48+
49+
y = round10f( -2.5, 0 );
50+
// returns -1
51+
52+
y = round10f( -0.0, 0 );
53+
// returns -0
54+
```
55+
56+
</section>
57+
58+
<!-- /.usage -->
59+
60+
<section class="examples">
61+
62+
## Examples
63+
64+
```javascript
65+
var uniform = require( '@stdlib/random/array/uniform' );
66+
var logEachMap = require( '@stdlib/console/log-each-map' );
67+
var toFloat32 = require( '@stdlib/number/float64/base/to-float32' );
68+
var round10f = require( '@stdlib/math/base/special/round10f' );
69+
70+
var opts = {
71+
'dtype': 'float32'
72+
};
73+
74+
var x = uniform( 100, -50.0, 50.0, opts );
75+
76+
// Ensure float32 precision:
77+
var i;
78+
for ( i = 0; i < x.length; i++ ) {
79+
x[ i ] = toFloat32( x[ i ] );
80+
}
81+
82+
var n = -1;
83+
84+
function fcn( v ) {
85+
return round10f( v, n );
86+
}
87+
88+
logEachMap('x: %0.4f. Rounded: %0.4f.', x, fcn);
89+
```
90+
91+
</section>
92+
93+
<!-- /.examples -->
94+
95+
96+
* * *
97+
98+
<section class="c">
99+
100+
## C APIs
101+
102+
<section class="intro">
103+
104+
This package provides a C API for rounding single-precision floating-point numbers to the nearest multiple of \\(10^n\\).
105+
106+
</section>
107+
108+
<!-- /.intro -->
109+
110+
<section class="usage">
111+
112+
### Usage
113+
114+
```c
115+
#include "stdlib/math/base/special/round10f.h"
116+
```
117+
118+
#### stdlib_base_round10f( x, n )
119+
120+
Rounds a single-precision floating-point number to the nearest multiple of \\(10^n\\).
121+
122+
```c
123+
float out = stdlib_base_round10f( 3.14f, 0 );
124+
// returns 3.0f
125+
126+
out = stdlib_base_round10f( 3.14f, -1 );
127+
// returns 3.1f
128+
```
129+
130+
**Arguments**
131+
132+
- **x**: `[in] float` input value.
133+
- **n**: `[in] int32_t` power of ten exponent.
134+
135+
**Returns**
136+
137+
- `float`: rounded value.
138+
139+
```c
140+
float stdlib_base_round10f( const float x, const int32_t n );
141+
```
142+
143+
</section>
144+
145+
<!-- /.usage -->
146+
147+
<section class="examples">
148+
149+
### Examples
150+
151+
```c
152+
#include "stdlib/math/base/special/round10f.h"
153+
#include <stdio.h>
154+
155+
int main( void ) {
156+
const float x[] = {
157+
-5.0f, -3.89f, -2.78f, -1.67f, -0.56f,
158+
0.56f, 1.67f, 2.78f, 3.89f, 5.0f
159+
};
160+
const int n = -1;
161+
162+
float v;
163+
int i;
164+
for ( i = 0; i < 10; i++ ) {
165+
v = stdlib_base_round10f( x[ i ], n );
166+
printf( "round10f(%f,%d) = %f\n", x[ i ], n, v );
167+
}
168+
}
169+
```
170+
171+
</section>
172+
173+
<!-- /.examples -->
174+
175+
</section>
176+
177+
<!-- /.c -->
178+
179+
<section class="related">
180+
181+
* * *
182+
183+
## See Also
184+
185+
- <span class="package-name">[`@stdlib/math/base/special/ceil10`][@stdlib/math/base/special/ceil10]</span><span class="delimiter">: </span><span class="description">round a numeric value to the nearest power of 10 toward positive infinity.</span>
186+
- <span class="package-name">[`@stdlib/math/base/special/floor10`][@stdlib/math/base/special/floor10]</span><span class="delimiter">: </span><span class="description">round a numeric value to the nearest power of 10 toward negative infinity.</span>
187+
- <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>
188+
- <span class="package-name">[`@stdlib/math/base/special/round2`][@stdlib/math/base/special/round2]</span><span class="delimiter">: </span><span class="description">round a numeric value to the nearest power of two on a linear scale.</span>
189+
190+
</section>
191+
192+
<!-- /.related -->
193+
194+
<section class="links">
195+
196+
[@stdlib/math/base/special/ceil10]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/ceil10
197+
[@stdlib/math/base/special/floor10]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/floor10
198+
[@stdlib/math/base/special/round]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/round
199+
[@stdlib/math/base/special/round2]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/round2
200+
201+
</section>
202+
203+
<!-- /.links -->
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 uniform = require( '@stdlib/random/array/uniform' );
25+
var toFloat32 = require( '@stdlib/number/float64/base/to-float32' );
26+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
27+
var round10f = require( './../lib/main.js' );
28+
var pkg = require( './../package.json' ).name;
29+
30+
31+
// MAIN //
32+
33+
bench( pkg, function benchmark( b ) {
34+
var exps;
35+
var x;
36+
var y;
37+
var i;
38+
39+
// Generate float32 input values:
40+
x = uniform( 100, -5.0e3, 5.0e3 );
41+
for ( i = 0; i < x.length; i++ ) {
42+
x[ i ] = toFloat32( x[ i ] );
43+
}
44+
45+
exps = [ -3, -1, 0, 1, 3 ];
46+
47+
b.tic();
48+
for ( i = 0; i < b.iterations; i++ ) {
49+
y = round10f( x[ i % x.length ], exps[ i % exps.length ] );
50+
if ( isnanf( y ) ) {
51+
b.fail( 'should not return NaN' );
52+
}
53+
}
54+
b.toc();
55+
56+
if ( isnanf( y ) ) {
57+
b.fail( 'should not return NaN' );
58+
}
59+
b.pass( 'benchmark finished' );
60+
b.end();
61+
});
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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 uniform = require( '@stdlib/random/array/uniform' );
26+
var toFloat32 = require( '@stdlib/number/float64/base/to-float32' );
27+
var isnan = require( '@stdlib/math/base/assert/is-nanf' );
28+
var tryRequire = require( '@stdlib/utils/try-require' );
29+
var format = require( '@stdlib/string/format' );
30+
var pkg = require( './../package.json' ).name;
31+
32+
33+
// VARIABLES //
34+
35+
var round10f = tryRequire( resolve( __dirname, './../lib/native.js' ) );
36+
var opts = {
37+
'skip': ( round10f instanceof Error )
38+
};
39+
40+
41+
// MAIN //
42+
43+
bench( format( '%s::native', pkg ), opts, function benchmark( b ) {
44+
var exps;
45+
var x;
46+
var y;
47+
var i;
48+
49+
// Generate float32 input data:
50+
x = uniform( 100, -5.0e3, 5.0e3 );
51+
for ( i = 0; i < x.length; i++ ) {
52+
x[ i ] = toFloat32( x[ i ] );
53+
}
54+
55+
exps = [ -3, -1, 0, 1, 3 ];
56+
57+
b.tic();
58+
for ( i = 0; i < b.iterations; i++ ) {
59+
y = round10f( x[ i % x.length ], exps[ i % exps.length ] );
60+
if ( isnan( y ) ) {
61+
b.fail( 'should not return NaN' );
62+
}
63+
}
64+
b.toc();
65+
66+
if ( isnan( y ) ) {
67+
b.fail( 'should not return NaN' );
68+
}
69+
b.pass( 'benchmark finished' );
70+
b.end();
71+
});

0 commit comments

Comments
 (0)