Skip to content

Commit 33bc9f4

Browse files
committed
feat: add number/float16/base/exponent
1 parent 8635752 commit 33bc9f4

File tree

10 files changed

+649
-0
lines changed

10 files changed

+649
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
# exponent
22+
23+
> Return an integer corresponding to the unbiased exponent of a [half-precision floating-point number][ieee754].
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var exponent = require( '@stdlib/number/float16/base/exponent' );
31+
```
32+
33+
#### exponent( x )
34+
35+
Returns an integer corresponding to the unbiased exponent of a [half-precision floating-point number][ieee754].
36+
37+
```javascript
38+
var toFloat16 = require( '@stdlib/number/float64/base/to-float16' );
39+
40+
var exp = exponent( toFloat16( 3.14e4 ) ); // => 2^15 ≈ 3.27e4
41+
// returns 15
42+
43+
exp = exponent( toFloat16( 3.14e-4 ) ); // => 2^-12 ≈ 2.44e-4
44+
// returns -12
45+
46+
exp = exponent( toFloat16( -3.14 ) );
47+
// returns 1
48+
49+
exp = exponent( 0.0 );
50+
// returns -15
51+
52+
exp = exponent( NaN );
53+
// returns 16
54+
```
55+
56+
</section>
57+
58+
<!-- /.usage -->
59+
60+
<section class="examples">
61+
62+
## Examples
63+
64+
<!-- eslint no-undef: "error" -->
65+
66+
```javascript
67+
var randu = require( '@stdlib/random/base/randu' );
68+
var round = require( '@stdlib/math/base/special/round' );
69+
var pow = require( '@stdlib/math/base/special/pow' );
70+
var toFloat16 = require( '@stdlib/number/float64/base/to-float16' );
71+
var exponent = require( '@stdlib/number/float16/base/exponent' );
72+
73+
var frac;
74+
var exp;
75+
var x;
76+
var e;
77+
var i;
78+
79+
// Generate random numbers and extract their exponents...
80+
for ( i = 0; i < 100; i++ ) {
81+
frac = randu() * 10.0;
82+
exp = round( randu()*13.0 ) - 7;
83+
x = frac * pow( 10.0, exp );
84+
x = toFloat16( x );
85+
e = exponent( x );
86+
console.log( 'x: %d. unbiased exponent: %d.', x, e );
87+
}
88+
```
89+
90+
</section>
91+
92+
<!-- /.examples -->
93+
94+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
95+
96+
<section class="related">
97+
98+
</section>
99+
100+
<!-- /.related -->
101+
102+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
103+
104+
<section class="links">
105+
106+
[ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985
107+
108+
<!-- <related-links> -->
109+
110+
<!-- </related-links> -->
111+
112+
</section>
113+
114+
<!-- /.links -->
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var toFloat16 = require( '@stdlib/number/float64/base/to-float16' );
27+
var pkg = require( './../package.json' ).name;
28+
var exponent = 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()*1.2e5 ) - 7.0e4;
41+
y = exponent( toFloat16( x ) );
42+
if ( isnan( y ) ) {
43+
b.fail( 'should not return NaN' );
44+
}
45+
}
46+
b.toc();
47+
if ( isnan( y ) ) {
48+
b.fail( 'should not return NaN' );
49+
}
50+
b.pass( 'benchmark finished' );
51+
b.end();
52+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
{{alias}}( x )
3+
Returns an integer corresponding to the unbiased exponent of a half-
4+
precision floating-point number.
5+
6+
Parameters
7+
----------
8+
x: float
9+
Half-precision floating-point number.
10+
11+
Returns
12+
-------
13+
out: integer
14+
Unbiased exponent.
15+
16+
Examples
17+
--------
18+
> var exponent = {{alias}}( {{alias:@stdlib/number/float64/base/to-float16}}( 3.14e4 ) )
19+
15
20+
> exponent = {{alias}}( {{alias:@stdlib/number/float64/base/to-float16}}( 3.14e-4 ) )
21+
-12
22+
> exponent = {{alias}}( {{alias:@stdlib/number/float64/base/to-float16}}( -3.14 ) )
23+
1
24+
> exponent = {{alias}}( 0.0 )
25+
-15
26+
> exponent = {{alias}}( NaN )
27+
16
28+
29+
See Also
30+
--------
31+
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
// TypeScript Version: 4.1
20+
21+
/**
22+
* Returns an integer corresponding to the unbiased exponent of a half-precision floating-point number.
23+
*
24+
* @param x - half-precision floating-point number
25+
* @returns unbiased exponent
26+
*
27+
* @example
28+
* var toFloat16 = require( '@stdlib/number/float64/base/to-float16' );
29+
*
30+
* var exp = exponent( toFloat16( 3.14e4 ) ); // => 2**15 ~ 3.27e4
31+
* // returns 15
32+
*
33+
* @example
34+
* var toFloat16 = require( '@stdlib/number/float64/base/to-float16' );
35+
*
36+
* var exp = exponent( toFloat16( 3.14e-4 ) ); // => 2**-12 ~ 2.44e-4
37+
* // returns -12
38+
*
39+
* @example
40+
* var toFloat16 = require( '@stdlib/number/float64/base/to-float16' );
41+
*
42+
* var exp = exponent( toFloat16( -3.14 ) );
43+
* // returns 1
44+
*
45+
* @example
46+
* var exp = exponent( 0.0 );
47+
* // returns -15
48+
*
49+
* @example
50+
* var exp = exponent( NaN );
51+
* // returns 16
52+
*/
53+
declare function exponent( x: number ): number;
54+
55+
56+
// EXPORTS //
57+
58+
export = exponent;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
import exponent = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a number...
25+
{
26+
exponent( 0 ); // $ExpectType number
27+
}
28+
29+
// The compiler throws an error if the function is provided a value other than a number...
30+
{
31+
exponent( true ); // $ExpectError
32+
exponent( false ); // $ExpectError
33+
exponent( null ); // $ExpectError
34+
exponent( undefined ); // $ExpectError
35+
exponent( '5' ); // $ExpectError
36+
exponent( [] ); // $ExpectError
37+
exponent( {} ); // $ExpectError
38+
exponent( ( x: number ): number => x ); // $ExpectError
39+
}
40+
41+
// The compiler throws an error if the function is provided insufficient arguments...
42+
{
43+
exponent(); // $ExpectError
44+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
var randu = require( '@stdlib/random/base/randu' );
22+
var round = require( '@stdlib/math/base/special/round' );
23+
var pow = require( '@stdlib/math/base/special/pow' );
24+
var toFloat16 = require( '@stdlib/number/float64/base/to-float16' );
25+
var exponent = require( './../lib' );
26+
27+
var frac;
28+
var exp;
29+
var x;
30+
var e;
31+
var i;
32+
33+
// Generate random numbers and extract their exponents...
34+
for ( i = 0; i < 100; i++ ) {
35+
frac = randu() * 10.0;
36+
exp = round( randu()*13.0 ) - 7;
37+
x = frac * pow( 10.0, exp );
38+
x = toFloat16( x );
39+
e = exponent( x );
40+
console.log( 'x: %d. unbiased exponent: %d.', x, e );
41+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
/**
22+
* Return an integer corresponding to the unbiased exponent of a half-precision floating-point number.
23+
*
24+
* @module @stdlib/number/float16/base/exponent
25+
*
26+
* @example
27+
* var toFloat16 = require( '@stdlib/number/float64/base/to-float16' );
28+
* var exponent = require( '@stdlib/number/float16/base/exponent' );
29+
*
30+
* var exp = exponent( toFloat16( 3.14e4 ) ); // => 2**15 ≈ 3.27e4
31+
* // returns 15
32+
*
33+
* exp = exponent( toFloat16( 3.14e-4 ) ); // => 2**-12 ≈ 2.44e-4
34+
* // returns -12
35+
*
36+
* exp = exponent( toFloat16( -3.14 ) );
37+
* // returns 1
38+
*
39+
* exp = exponent( 0.0 );
40+
* // returns -15
41+
*
42+
* exp = exponent( NaN );
43+
* // returns 16
44+
*/
45+
46+
// MODULES //
47+
48+
var main = require( './main.js' );
49+
50+
51+
// EXPORTS //
52+
53+
module.exports = main;

0 commit comments

Comments
 (0)