Skip to content

Commit 539f58a

Browse files
committed
feat: add math/base/special/truncbf
Implement single-precision floating-point truncation function that rounds toward zero to n digits in base b. Features: - C implementation with native addon support - Single-precision operations using powf and truncf - Overflow handling when scale factor becomes infinite - Comprehensive tests (23 test cases) - Benchmarks for C, native, Julia, and JavaScript - TypeScript declarations and documentation - Support for arbitrary base b - Proper handling of edge cases (NaN, ±0, ±Infinity, base=0) Addresses maintainer feedback: - Uses single-precision packages (powf, truncf) - Uses NAPI macro STDLIB_MATH_BASE_NAPI_MODULE_FII_F - Proper manifest.json structure (only main.c in src) Closes #649
1 parent 3138d95 commit 539f58a

File tree

27 files changed

+2804
-0
lines changed

27 files changed

+2804
-0
lines changed
Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 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+
# truncbf
22+
23+
> Round a [single-precision floating-point number][ieee754] toward zero to `n` digits in base `b`.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var truncbf = require( '@stdlib/math/base/special/truncbf' );
31+
```
32+
33+
#### truncbf( x, n, b )
34+
35+
Rounds a [single-precision floating-point number][ieee754] toward zero to `n` digits in base `b`.
36+
37+
```javascript
38+
var v = truncbf( 3.14159, 2, 10 );
39+
// returns 3.140000104904175
40+
41+
v = truncbf( 3.14159, 3, 10 );
42+
// returns 3.1410000324249268
43+
44+
v = truncbf( 15.0, -1, 10 );
45+
// returns 10.0
46+
47+
v = truncbf( -3.14159, 2, 10 );
48+
// returns -3.140000104904175
49+
```
50+
51+
If provided `NaN` or a `base` of `0`, the function returns `NaN`.
52+
53+
```javascript
54+
var v = truncbf( NaN, 2, 10 );
55+
// returns NaN
56+
57+
v = truncbf( 3.14, 2, 0 );
58+
// returns NaN
59+
```
60+
61+
If provided `x = ±0` or `x = ±infinity`, the function returns `x`.
62+
63+
```javascript
64+
var PINF = require( '@stdlib/constants/float32/pinf' );
65+
var NINF = require( '@stdlib/constants/float32/ninf' );
66+
67+
var v = truncbf( 0.0, 2, 10 );
68+
// returns 0.0
69+
70+
v = truncbf( -0.0, 2, 10 );
71+
// returns -0.0
72+
73+
v = truncbf( PINF, 2, 10 );
74+
// returns Infinity
75+
76+
v = truncbf( NINF, 2, 10 );
77+
// returns -Infinity
78+
```
79+
80+
</section>
81+
82+
<!-- /.usage -->
83+
84+
<section class="notes">
85+
86+
## Notes
87+
88+
- This function is the [single-precision floating-point][ieee754] equivalent of [`truncb`][@stdlib/math/base/special/truncb].
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 randu = require( '@stdlib/random/base/randu' );
102+
var truncbf = require( '@stdlib/math/base/special/truncbf' );
103+
104+
var x;
105+
var i;
106+
107+
for ( i = 0; i < 100; i++ ) {
108+
x = (randu() * 100.0) - 50.0;
109+
console.log( 'truncbf(%d, 2, 10) = %d', x, truncbf( x, 2, 10 ) );
110+
}
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/truncbf.h"
141+
```
142+
143+
#### stdlib_base_truncbf( x, n, b )
144+
145+
Rounds a [single-precision floating-point number][ieee754] toward zero to `n` digits in base `b`.
146+
147+
```c
148+
float y = stdlib_base_truncbf( 3.14159f, 2, 10 );
149+
// returns 3.14f
150+
```
151+
152+
The function accepts the following arguments:
153+
154+
- **x**: `[in] float` input value.
155+
- **n**: `[in] int32_t` number of digits.
156+
- **b**: `[in] int32_t` base.
157+
158+
```c
159+
float stdlib_base_truncbf( const float x, const int32_t n, const int32_t b );
160+
```
161+
162+
</section>
163+
164+
<!-- /.usage -->
165+
166+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
167+
168+
<section class="notes">
169+
170+
</section>
171+
172+
<!-- /.notes -->
173+
174+
<!-- C API usage examples. -->
175+
176+
<section class="examples">
177+
178+
### Examples
179+
180+
```c
181+
#include "stdlib/math/base/special/truncbf.h"
182+
#include <stdio.h>
183+
184+
int main( void ) {
185+
const float x[] = { 3.14159f, -3.14159f, 15.0f, -15.0f };
186+
const int32_t n[] = { 2, 3, -1, 0 };
187+
const int32_t b = 10;
188+
189+
float y;
190+
int i;
191+
for ( i = 0; i < 4; i++ ) {
192+
y = stdlib_base_truncbf( x[i], n[i], b );
193+
printf( "truncbf(%f, %d, %d) = %f\n", x[i], n[i], b, y );
194+
}
195+
}
196+
```
197+
198+
</section>
199+
200+
<!-- /.examples -->
201+
202+
</section>
203+
204+
<!-- /.c -->
205+
206+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
207+
208+
<section class="related">
209+
210+
</section>
211+
212+
<!-- /.related -->
213+
214+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
215+
216+
<section class="links">
217+
218+
[ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985
219+
220+
[@stdlib/math/base/special/truncb]: https://github.com/stdlib-js/stdlib
221+
222+
</section>
223+
224+
<!-- /.links -->
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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 bench = require( '@stdlib/bench' );
24+
var randu = require( '@stdlib/random/base/randu' );
25+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var trunc = require( '@stdlib/math/base/special/trunc' );
27+
var pkg = require( './../package.json' ).name;
28+
var truncbf = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( 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() * 100.0) - 50.0;
41+
y = truncbf( x, 2, 10 );
42+
if ( isnanf( y ) ) {
43+
b.fail( 'should not return NaN' );
44+
}
45+
}
46+
b.toc();
47+
if ( isnanf( y ) ) {
48+
b.fail( 'should not return NaN' );
49+
}
50+
b.pass( 'benchmark finished' );
51+
b.end();
52+
});
53+
54+
bench( pkg+'::built-in', function benchmark( b ) {
55+
var x;
56+
var y;
57+
var i;
58+
59+
b.tic();
60+
for ( i = 0; i < b.iterations; i++ ) {
61+
x = (randu() * 100.0) - 50.0;
62+
63+
// Use stdlib trunc instead of Math.trunc:
64+
y = trunc( x * 100.0 ) / 100.0;
65+
if ( isnanf( y ) ) {
66+
b.fail( 'should not return NaN' );
67+
}
68+
}
69+
b.toc();
70+
if ( isnanf( y ) ) {
71+
b.fail( 'should not return NaN' );
72+
}
73+
b.pass( 'benchmark finished' );
74+
b.end();
75+
});
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var randu = require( '@stdlib/random/base/randu' );
26+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
27+
var tryRequire = require( '@stdlib/utils/try-require' );
28+
var pkg = require( './../package.json' ).name;
29+
30+
31+
// VARIABLES //
32+
33+
var truncbf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
34+
var opts = {
35+
'skip': ( truncbf instanceof Error )
36+
};
37+
38+
39+
// MAIN //
40+
41+
bench( pkg+'::native', opts, function benchmark( b ) {
42+
var x;
43+
var y;
44+
var i;
45+
46+
b.tic();
47+
for ( i = 0; i < b.iterations; i++ ) {
48+
x = (randu() * 100.0) - 50.0;
49+
y = truncbf( x, 2, 10 );
50+
if ( isnanf( y ) ) {
51+
b.fail( 'should not return NaN' );
52+
}
53+
}
54+
b.toc();
55+
if ( isnanf( y ) ) {
56+
b.fail( 'should not return NaN' );
57+
}
58+
b.pass( 'benchmark finished' );
59+
b.end();
60+
});

0 commit comments

Comments
 (0)