Skip to content

Commit 71c43cf

Browse files
feat: add math/base/special/ldexpf
PR-URL: #2805 Ref: #649 --------- Signed-off-by: Gunj Joshi <[email protected]> Signed-off-by: Philipp Burckhardt <[email protected]> Co-authored-by: Philipp Burckhardt <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]>
1 parent 6c8f019 commit 71c43cf

File tree

30 files changed

+2518
-0
lines changed

30 files changed

+2518
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 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+
# ldexpf
22+
23+
> Multiply a [single-precision floating-point number][ieee754] by an integer power of two.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var ldexpf = require( '@stdlib/math/base/special/ldexpf' );
31+
```
32+
33+
#### ldexpf( frac, exp )
34+
35+
Multiplies a [single-precision floating-point number][ieee754] by an `integer` power of two (i.e., `x = frac * 2^exp`).
36+
37+
```javascript
38+
var x = ldexpf( 0.5, 3 ); // => 0.5 * 2^3 = 0.5 * 8
39+
// returns 4.0
40+
41+
x = ldexpf( 4.0, -2 ); // => 4 * 2^(-2) = 4 * (1/4)
42+
// returns 1.0
43+
```
44+
45+
If `frac` equals positive or negative zero, `NaN`, or positive or negative `infinity`, the function returns a value equal to `frac`.
46+
47+
```javascript
48+
var x = ldexpf( 0.0, 20 );
49+
// returns 0.0
50+
51+
x = ldexpf( -0.0, 39 );
52+
// returns -0.0
53+
54+
x = ldexpf( NaN, -101 );
55+
// returns NaN
56+
57+
x = ldexpf( Infinity, 11 );
58+
// returns Infinity
59+
60+
x = ldexpf( -Infinity, -118 );
61+
// returns -Infinity
62+
```
63+
64+
<section class="usage">
65+
66+
<section class="notes">
67+
68+
## Notes
69+
70+
// TODO: update this once we have `frexpf`.
71+
72+
- This function is the inverse of [`frexp`][@stdlib/math/base/special/frexp].
73+
74+
</section>
75+
76+
<!-- /.notes -->
77+
78+
<section class="examples">
79+
80+
## Examples
81+
82+
<!-- eslint no-undef: "error" -->
83+
84+
```javascript
85+
var linspace = require( '@stdlib/array/base/linspace' );
86+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
87+
var ldexpf = require( '@stdlib/math/base/special/ldexpf' );
88+
89+
var frac = linspace( 0.0, 100.0, 10 );
90+
var exp = discreteUniform( 100, 0, 10 );
91+
92+
var i;
93+
for ( i = 0; i < frac.length; i++ ) {
94+
console.log( 'ldexpf(%d,%d) = %d', frac[ i ], exp[ i ], ldexpf( frac[ i ], exp[ i ] ) );
95+
}
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/ldexpf.h"
126+
```
127+
128+
#### stdlib_base_ldexpf( frac, exp )
129+
130+
Multiplies a [single-precision floating-point number][ieee754] by an integer power of two (i.e., `x = frac * 2^exp`).
131+
132+
```c
133+
float x = stdlib_base_ldexpf( 0.5f, 3 ); // => 0.5 * 2^3 = 0.5 * 8
134+
// returns 4.0f
135+
```
136+
137+
The function accepts the following arguments:
138+
139+
- **frac**: `[in] float` input value.
140+
- **exp**: `[in] int32_t` integer power of two.
141+
142+
```c
143+
float stdlib_base_ldexpf( const float frac, const int32_t exp );
144+
```
145+
146+
</section>
147+
148+
<!-- /.usage -->
149+
150+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
151+
152+
<section class="notes">
153+
154+
</section>
155+
156+
<!-- /.notes -->
157+
158+
<!-- C API usage examples. -->
159+
160+
<section class="examples">
161+
162+
### Examples
163+
164+
```c
165+
#include "stdlib/math/base/special/ldexpf.h"
166+
#include <stdint.h>
167+
#include <stdio.h>
168+
169+
int main( void ) {
170+
float y;
171+
int i;
172+
173+
const float frac[] = { 0.5f, 5.0f, 0.0f, 3.5f, 7.9f };
174+
const int32_t exp[] = { 3, -2, 20, 39, 14 };
175+
176+
for ( i = 0; i < 5; i++ ) {
177+
y = stdlib_base_ldexpf( frac[ i ], exp[ i ] );
178+
printf( "ldexpf(%f, %d) = %f\n", frac[ i ], exp[ i ], y );
179+
}
180+
}
181+
```
182+
183+
</section>
184+
185+
<!-- /.examples -->
186+
187+
</section>
188+
189+
<!-- /.c -->
190+
191+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
192+
193+
<section class="related">
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+
[ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985
204+
[@stdlib/math/base/special/frexp]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/frexp
205+
206+
<!-- <related-links> -->
207+
208+
<!-- </related-links> -->
209+
210+
</section>
211+
212+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 randu = require( '@stdlib/random/array/uniform' );
25+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
26+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
27+
var pkg = require( './../package.json' ).name;
28+
var ldexpf = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg, function benchmark( b ) {
34+
var x;
35+
var y;
36+
var z;
37+
var i;
38+
39+
x = randu( 100, -10.0, 10.0 );
40+
y = discreteUniform( 100, -1020.0, 1020.0 );
41+
42+
b.tic();
43+
for ( i = 0; i < b.iterations; i++ ) {
44+
z = ldexpf( x[ i % x.length ], y[ i % y.length ] );
45+
if ( isnanf( z ) ) {
46+
b.fail( 'should not return NaN' );
47+
}
48+
}
49+
b.toc();
50+
if ( isnanf( z ) ) {
51+
b.fail( 'should not return NaN' );
52+
}
53+
b.pass( 'benchmark finished' );
54+
b.end();
55+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 randu = require( '@stdlib/random/base/randu' );
26+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
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 ldexpf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
35+
var opts = {
36+
'skip': ( ldexpf instanceof Error )
37+
};
38+
39+
40+
// MAIN //
41+
42+
bench( pkg+'::native', opts, function benchmark( b ) {
43+
var x;
44+
var y;
45+
var z;
46+
var i;
47+
48+
x = randu( 100, -10.0, 10.0 );
49+
y = discreteUniform( 100, -1020.0, 1020.0 );
50+
51+
b.tic();
52+
for ( i = 0; i < b.iterations; i++ ) {
53+
z = ldexpf( x[ i % x.length ], y[ i % y.length ] );
54+
if ( isnanf( z ) ) {
55+
b.fail( 'should not return NaN' );
56+
}
57+
}
58+
b.toc();
59+
if ( isnanf( z ) ) {
60+
b.fail( 'should not return NaN' );
61+
}
62+
b.pass( 'benchmark finished' );
63+
b.end();
64+
});

0 commit comments

Comments
 (0)