Skip to content

Commit c51df70

Browse files
feat: add implementation of math/base/special/floor2f
PR-URL: #10323 Ref: #649 Co-authored-by: Philipp Burckhardt <pburckhardt@outlook.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com> Signed-off-by: Philipp Burckhardt <pburckhardt@outlook.com>
1 parent 8b01d81 commit c51df70

File tree

24 files changed

+2164
-0
lines changed

24 files changed

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

0 commit comments

Comments
 (0)