Skip to content

Commit 7e6a88b

Browse files
gururaj1512kgrytestdlib-botanandkaranubc
committed
feat: add math/base/special/wrapf
PR-URL: stdlib-js#3055 Ref: stdlib-js#649 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Reviewed-by: Karan Anand <[email protected]> Reviewed-by: Gunj Joshi <[email protected]> Co-authored-by: stdlib-bot <[email protected]> Co-authored-by: Karan Anand <[email protected]>
1 parent c16125f commit 7e6a88b

File tree

26 files changed

+2477
-0
lines changed

26 files changed

+2477
-0
lines changed
Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
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+
# wrapf
22+
23+
> Wrap a single-precision floating-point value to the half-open interval `[min,max)`.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var wrapf = require( '@stdlib/math/base/special/wrapf' );
41+
```
42+
43+
#### wrapf( v, min, max )
44+
45+
Wraps a single-precision floating-point value to the half-open interval `[min,max)`.
46+
47+
```javascript
48+
var v = wrapf( 3.14, 0.0, 5.0 );
49+
// returns ~3.14
50+
51+
v = wrapf( -3.14, 0.0, 5.0 );
52+
// returns ~1.86
53+
54+
v = wrapf( 10.0, 0.0, 5.0 );
55+
// returns 0.0
56+
57+
v = wrapf( -0.0, 0.0, 5.0 );
58+
// returns 0.0
59+
60+
v = wrapf( 0.0, -3.14, -0.0 );
61+
// returns ~-3.14
62+
```
63+
64+
If provided `NaN` for any argument, the function returns `NaN`.
65+
66+
```javascript
67+
var v = wrapf( NaN, 0.0, 5.0 );
68+
// returns NaN
69+
70+
v = wrapf( 0.0, NaN, 5.0 );
71+
// returns NaN
72+
73+
v = wrapf( 3.14, 0.0, NaN );
74+
// returns NaN
75+
```
76+
77+
If provided `min == max`, the function returns `NaN`.
78+
79+
```javascript
80+
var v = wrapf( 3.14, 3.0, 3.0 );
81+
// returns NaN
82+
```
83+
84+
</section>
85+
86+
<!-- /.usage -->
87+
88+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
89+
90+
<section class="notes">
91+
92+
## Notes
93+
94+
- The function does **not** distinguish between positive and negative zero. Where appropriate, the function returns positive zero.
95+
96+
</section>
97+
98+
<!-- /.notes -->
99+
100+
<!-- Package usage examples. -->
101+
102+
<section class="examples">
103+
104+
## Examples
105+
106+
<!-- eslint no-undef: "error" -->
107+
108+
```javascript
109+
var uniform = require( '@stdlib/random/array/uniform' );
110+
var logEachMap = require( '@stdlib/console/log-each-map' );
111+
var wrapf = require( '@stdlib/math/base/special/wrapf' );
112+
113+
var opts = {
114+
'dtype': 'float32'
115+
};
116+
var min = uniform( 100, 0.0, 10.0, opts );
117+
var max = uniform( 100, 5.0, 15.0, opts );
118+
var v = uniform( 100, -20.0, 20.0, opts );
119+
120+
logEachMap( 'wrapf(%0.4f,%0.4f,%0.4f) => %0.4f', v, min, max, wrapf );
121+
```
122+
123+
</section>
124+
125+
<!-- /.examples -->
126+
127+
<!-- C interface documentation. -->
128+
129+
* * *
130+
131+
<section class="c">
132+
133+
## C APIs
134+
135+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
136+
137+
<section class="intro">
138+
139+
</section>
140+
141+
<!-- /.intro -->
142+
143+
<!-- C usage documentation. -->
144+
145+
<section class="usage">
146+
147+
### Usage
148+
149+
```c
150+
#include "stdlib/math/base/special/wrapf.h"
151+
```
152+
153+
#### stdlib_base_wrapf( v, min, max )
154+
155+
Wraps a single-precision floating-point value to the half-open interval `[min,max)`.
156+
157+
```c
158+
float v = stdlib_base_wrapf( 3.14f, 0.0f, 5.0f );
159+
// returns ~3.14f
160+
161+
v = stdlib_base_wrapf( -3.14f, 0.0f, 5.0f );
162+
// returns ~1.86f
163+
```
164+
165+
The function accepts the following arguments:
166+
167+
- **v**: `[in] float` input value to wrap.
168+
- **min**: `[in] float` minimum value.
169+
- **max**: `[in] float` maximum value.
170+
171+
```c
172+
float stdlib_base_wrapf( const float v, const float min, const float max )
173+
```
174+
175+
</section>
176+
177+
<!-- /.usage -->
178+
179+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
180+
181+
<section class="notes">
182+
183+
</section>
184+
185+
<!-- /.notes -->
186+
187+
<!-- C API usage examples. -->
188+
189+
<section class="examples">
190+
191+
### Examples
192+
193+
```c
194+
#include "stdlib/math/base/special/wrapf.h"
195+
#include <stdio.h>
196+
197+
int main( void ) {
198+
const float min[] = { 0.0f, 0.0f, 0.0f, 0.0f, -3.14f };
199+
const float max[] = { 5.0f, 5.0f, 5.0f, 5.0f, -0.0f };
200+
const float v[] = { 3.14f, -3.14f, 10.0f, -0.0f, 0.0f };
201+
202+
float out;
203+
int i;
204+
for ( i = 0; i < 5; i++ ) {
205+
out = stdlib_base_wrapf( v[i], min[i], max[i] );
206+
printf( "wrapf(%f,%f,%f) => %f\n", v[i], min[i], max[i], out );
207+
}
208+
}
209+
```
210+
211+
</section>
212+
213+
<!-- /.examples -->
214+
215+
</section>
216+
217+
<!-- /.c -->
218+
219+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
220+
221+
<section class="references">
222+
223+
</section>
224+
225+
<!-- /.references -->
226+
227+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
228+
229+
<section class="related">
230+
231+
</section>
232+
233+
<!-- /.related -->
234+
235+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
236+
237+
<section class="links">
238+
239+
<!-- <related-links> -->
240+
241+
<!-- </related-links> -->
242+
243+
</section>
244+
245+
<!-- /.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) 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 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 wrapf = 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, -10.0, 10.0, {
38+
'dtype': 'float32'
39+
});
40+
41+
b.tic();
42+
for ( i = 0; i < b.iterations; i++ ) {
43+
y = wrapf( x[ i%x.length ], -5.0, 5.0 );
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: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 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 pkg = require( './../package.json' ).name;
29+
30+
31+
// VARIABLES //
32+
33+
var wrapf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
34+
var opts = {
35+
'skip': ( wrapf 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+
x = uniform( 100, -10.0, 10.0, {
47+
'dtype': 'float32'
48+
});
49+
50+
b.tic();
51+
for ( i = 0; i < b.iterations; i++ ) {
52+
y = wrapf( x[ i%x.length ], -5.0, 5.0 );
53+
if ( isnanf( y ) ) {
54+
b.fail( 'should not return NaN' );
55+
}
56+
}
57+
b.toc();
58+
if ( isnanf( y ) ) {
59+
b.fail( 'should not return NaN' );
60+
}
61+
b.pass( 'benchmark finished' );
62+
b.end();
63+
});

0 commit comments

Comments
 (0)