Skip to content

Commit 8147ce6

Browse files
committed
feat: add c implementation for trunc10f
--- 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: passed - task: lint_c_examples status: passed - task: lint_c_benchmarks status: passed - 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 8452bac commit 8147ce6

24 files changed

Lines changed: 2126 additions & 0 deletions

File tree

Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 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+
# trunc10f
22+
23+
> Round a numeric value to the nearest power of 10 toward zero.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var trunc10f = require( '@stdlib/math/base/special/trunc10f' );
31+
```
32+
33+
#### trunc10f( x )
34+
35+
Rounds a `single-precision` floating point number to the nearest power of `10` toward zero.
36+
37+
```javascript
38+
var v = trunc10f( -4.2 );
39+
// returns -1.0
40+
41+
v = trunc10f( -4.5 );
42+
// returns -1.0
43+
44+
v = trunc10f( -4.6 );
45+
// returns -1.0
46+
47+
v = trunc10f( 9.99999 );
48+
// returns 1.0
49+
50+
v = trunc10f( 9.5 );
51+
// returns 1.0
52+
53+
v = trunc10f( 13.0 );
54+
// returns 10.0
55+
56+
v = trunc10f( -13.0 );
57+
// returns -10.0
58+
59+
v = trunc10f( 0.0 );
60+
// returns 0.0
61+
62+
v = trunc10f( -0.0 );
63+
// returns -0.0
64+
65+
v = trunc10f( Infinity );
66+
// returns Infinity
67+
68+
v = trunc10f( -Infinity );
69+
// returns -Infinity
70+
71+
v = trunc10f( NaN );
72+
// returns NaN
73+
```
74+
75+
</section>
76+
77+
<!-- /.usage -->
78+
79+
<section class="notes">
80+
81+
## Notes
82+
83+
- The function may not return accurate results for subnormals due to a general loss in precision.
84+
85+
```javascript
86+
var v = trunc10f( 1.0e-46 ); // should return 1.0e-46
87+
// returns 0.0
88+
```
89+
90+
</section>
91+
92+
<!-- /.notes -->
93+
94+
<section class="examples">
95+
96+
## Examples
97+
98+
<!-- eslint no-undef: "error" -->
99+
100+
```javascript
101+
var uniform = require( '@stdlib/random/array/uniform' );
102+
var logEachMap = require( '@stdlib/console/log-each-map' );
103+
var trunc10f = require( '@stdlib/math/base/special/trunc10f' );
104+
105+
var opts = {
106+
'dtype': 'float32'
107+
};
108+
var x = uniform( 100, -50.0, 50.0, opts );
109+
110+
logEachMap( 'x: %0.4f. Rounded: %0.4f.', x, trunc10f );
111+
```
112+
113+
</section>
114+
115+
<!-- /.examples -->
116+
117+
<!-- C interface documentation. -->
118+
119+
* * *
120+
121+
<section class="c">
122+
123+
## C APIs
124+
125+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
126+
127+
<section class="intro">
128+
129+
</section>
130+
131+
<!-- /.intro -->
132+
133+
<!-- C usage documentation. -->
134+
135+
<section class="usage">
136+
137+
### Usage
138+
139+
```c
140+
#include "stdlib/math/base/special/trunc10f.h"
141+
```
142+
143+
#### stdlib_base_trunc10f( x )
144+
145+
Rounds a `single-precision` floating point number to the nearest power of `10` toward zero.
146+
147+
```c
148+
float y = stdlib_base_trunc10f( -4.2 );
149+
// returns -1.0
150+
```
151+
152+
The function accepts the following arguments:
153+
154+
- **x**: `[in] float` input value.
155+
156+
```c
157+
float stdlib_base_trunc10f( const float x );
158+
```
159+
160+
</section>
161+
162+
<!-- /.usage -->
163+
164+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
165+
166+
<section class="notes">
167+
168+
</section>
169+
170+
<!-- /.notes -->
171+
172+
<!-- C API usage examples. -->
173+
174+
<section class="examples">
175+
176+
### Examples
177+
178+
```c
179+
#include "stdlib/math/base/special/trunc10f.h"
180+
#include <stdio.h>
181+
182+
int main( void ) {
183+
const float x[] = { 3.14f, -3.14f, 0.0f, 0.0f / 0.0f };
184+
185+
float y;
186+
int i;
187+
for ( i = 0; i < 4; i++ ) {
188+
y = stdlib_base_trunc10f( x[ i ] );
189+
printf( "trunc10f(%f) = %f\n", x[ i ], y );
190+
}
191+
}
192+
```
193+
194+
</section>
195+
196+
<!-- /.examples -->
197+
198+
</section>
199+
200+
<!-- /.c -->
201+
202+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
203+
204+
<section class="related">
205+
206+
* * *
207+
208+
## See Also
209+
210+
- <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>
211+
- <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>
212+
- <span class="package-name">[`@stdlib/math/base/special/round10`][@stdlib/math/base/special/round10]</span><span class="delimiter">: </span><span class="description">round a numeric value to the nearest power of 10 on a linear scale.</span>
213+
- <span class="package-name">[`@stdlib/math/base/special/trunc`][@stdlib/math/base/special/trunc]</span><span class="delimiter">: </span><span class="description">round a double-precision floating-point number toward zero.</span>
214+
- <span class="package-name">[`@stdlib/math/base/special/trunc2`][@stdlib/math/base/special/trunc2]</span><span class="delimiter">: </span><span class="description">round a numeric value to the nearest power of two toward zero.</span>
215+
216+
</section>
217+
218+
<!-- /.related -->
219+
220+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
221+
222+
<section class="links">
223+
224+
<!-- <related-links> -->
225+
226+
[@stdlib/math/base/special/ceil10]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/ceil10
227+
228+
[@stdlib/math/base/special/floor10]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/floor10
229+
230+
[@stdlib/math/base/special/round10]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/round10
231+
232+
[@stdlib/math/base/special/trunc]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/trunc
233+
234+
[@stdlib/math/base/special/trunc2]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/trunc2
235+
236+
<!-- </related-links> -->
237+
238+
</section>
239+
240+
<!-- /.links -->
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var format = require( '@stdlib/string/format' );
27+
var pkg = require( './../package.json' ).name;
28+
var trunc10f = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( format( '%s', pkg ), function benchmark( b ) {
34+
var opts = {
35+
'dtype': 'float32'
36+
};
37+
var x;
38+
var y;
39+
var i;
40+
41+
x = uniform( 100, -5.0e6, 5.0e6, opts );
42+
43+
b.tic();
44+
for ( i = 0; i < b.iterations; i++ ) {
45+
y = trunc10f( x[ i % x.length ] );
46+
if ( isnanf( y ) ) {
47+
b.fail( 'should not return NaN' );
48+
}
49+
}
50+
b.toc();
51+
if ( isnanf( y ) ) {
52+
b.fail( 'should not return NaN' );
53+
}
54+
b.pass( 'benchmark finished' );
55+
b.end();
56+
});
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 format = require( '@stdlib/string/format' );
27+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
28+
var tryRequire = require( '@stdlib/utils/try-require' );
29+
var pkg = require( './../package.json' ).name;
30+
31+
32+
// VARIABLES //
33+
34+
var trunc10f = tryRequire( resolve( __dirname, './../lib/native.js' ) );
35+
var opts = {
36+
'skip': ( trunc10f instanceof Error )
37+
};
38+
39+
40+
// MAIN //
41+
42+
bench( format( '%s::native', pkg ), opts, function benchmark( b ) {
43+
var opts = {
44+
'dtype': 'float32'
45+
};
46+
var x;
47+
var y;
48+
var i;
49+
50+
x = uniform( 100, -5.0e6, 5.0e6, opts );
51+
52+
b.tic();
53+
for ( i = 0; i < b.iterations; i++ ) {
54+
y = trunc10f( x[ i % x.length ] );
55+
if ( isnanf( y ) ) {
56+
b.fail( 'should not return NaN' );
57+
}
58+
}
59+
b.toc();
60+
if ( isnanf( y ) ) {
61+
b.fail( 'should not return NaN' );
62+
}
63+
b.pass( 'benchmark finished' );
64+
b.end();
65+
});

0 commit comments

Comments
 (0)