Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions lib/node_modules/@stdlib/number/float16/base/exponent/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<!--

@license Apache-2.0

Copyright (c) 2025 The Stdlib Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

# exponent

> Return an integer corresponding to the unbiased exponent of a [half-precision floating-point number][ieee754].

<section class="usage">

## Usage

```javascript
var exponent = require( '@stdlib/number/float16/base/exponent' );
```

#### exponent( x )

Returns an integer corresponding to the unbiased exponent of a [half-precision floating-point number][ieee754].

```javascript
var toFloat16 = require( '@stdlib/number/float64/base/to-float16' );

var exp = exponent( toFloat16( 3.14e4 ) ); // => 2^15 ≈ 3.27e4
// returns 15

exp = exponent( toFloat16( 3.14e-4 ) ); // => 2^-12 ≈ 2.44e-4
// returns -12

exp = exponent( toFloat16( -3.14 ) );
// returns 1

exp = exponent( 0.0 );
// returns -15

exp = exponent( NaN );
// returns 16
```

</section>

<!-- /.usage -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var randu = require( '@stdlib/random/base/randu' );
var round = require( '@stdlib/math/base/special/round' );
var pow = require( '@stdlib/math/base/special/pow' );
var toFloat16 = require( '@stdlib/number/float64/base/to-float16' );
var exponent = require( '@stdlib/number/float16/base/exponent' );

var frac;
var exp;
var x;
var e;
var i;

// Generate random numbers and extract their exponents...
for ( i = 0; i < 100; i++ ) {
frac = randu() * 10.0;
exp = round( randu()*13.0 ) - 7;
x = frac * pow( 10.0, exp );
x = toFloat16( x );
e = exponent( x );
console.log( 'x: %d. unbiased exponent: %d.', x, e );
}
```

</section>

<!-- /.examples -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985

<!-- <related-links> -->

<!-- </related-links> -->

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var toFloat16 = require( '@stdlib/number/float64/base/to-float16' );
var pkg = require( './../package.json' ).name;
var exponent = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var x;
var y;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*1.2e5 ) - 7.0e4;
y = exponent( toFloat16( x ) );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

{{alias}}( x )
Returns an integer corresponding to the unbiased exponent of a half-
precision floating-point number.

Parameters
----------
x: float
Half-precision floating-point number.

Returns
-------
out: integer
Unbiased exponent.

Examples
--------
> var exponent = {{alias}}( {{alias:@stdlib/number/float64/base/to-float16}}( 3.14e4 ) )
15
> exponent = {{alias}}( {{alias:@stdlib/number/float64/base/to-float16}}( 3.14e-4 ) )
-12
> exponent = {{alias}}( {{alias:@stdlib/number/float64/base/to-float16}}( -3.14 ) )
1
> exponent = {{alias}}( 0.0 )
-15
> exponent = {{alias}}( NaN )
16

See Also
--------

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// TypeScript Version: 4.1

/**
* Returns an integer corresponding to the unbiased exponent of a half-precision floating-point number.
*
* @param x - half-precision floating-point number
* @returns unbiased exponent
*
* @example
* var toFloat16 = require( '@stdlib/number/float64/base/to-float16' );
*
* var exp = exponent( toFloat16( 3.14e4 ) ); // => 2**15 ~ 3.27e4
* // returns 15
*
* @example
* var toFloat16 = require( '@stdlib/number/float64/base/to-float16' );
*
* var exp = exponent( toFloat16( 3.14e-4 ) ); // => 2**-12 ~ 2.44e-4
* // returns -12
*
* @example
* var toFloat16 = require( '@stdlib/number/float64/base/to-float16' );
*
* var exp = exponent( toFloat16( -3.14 ) );
* // returns 1
*
* @example
* var exp = exponent( 0.0 );
* // returns -15
*
* @example
* var exp = exponent( NaN );
* // returns 16
*/
declare function exponent( x: number ): number;


// EXPORTS //

export = exponent;
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import exponent = require( './index' );


// TESTS //

// The function returns a number...
{
exponent( 0 ); // $ExpectType number
}

// The compiler throws an error if the function is provided a value other than a number...
{
exponent( true ); // $ExpectError
exponent( false ); // $ExpectError
exponent( null ); // $ExpectError
exponent( undefined ); // $ExpectError
exponent( '5' ); // $ExpectError
exponent( [] ); // $ExpectError
exponent( {} ); // $ExpectError
exponent( ( x: number ): number => x ); // $ExpectError
}

// The compiler throws an error if the function is provided insufficient arguments...
{
exponent(); // $ExpectError
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

var randu = require( '@stdlib/random/base/randu' );
var round = require( '@stdlib/math/base/special/round' );
var pow = require( '@stdlib/math/base/special/pow' );
var toFloat16 = require( '@stdlib/number/float64/base/to-float16' );
var exponent = require( './../lib' );

var frac;
var exp;
var x;
var e;
var i;

// Generate random numbers and extract their exponents...
for ( i = 0; i < 100; i++ ) {
frac = randu() * 10.0;
exp = round( randu()*13.0 ) - 7;
x = frac * pow( 10.0, exp );
x = toFloat16( x );
e = exponent( x );
console.log( 'x: %d. unbiased exponent: %d.', x, e );
}
53 changes: 53 additions & 0 deletions lib/node_modules/@stdlib/number/float16/base/exponent/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

/**
* Return an integer corresponding to the unbiased exponent of a half-precision floating-point number.
*
* @module @stdlib/number/float16/base/exponent
*
* @example
* var toFloat16 = require( '@stdlib/number/float64/base/to-float16' );
* var exponent = require( '@stdlib/number/float16/base/exponent' );
*
* var exp = exponent( toFloat16( 3.14e4 ) ); // => 2**15 ≈ 3.27e4
* // returns 15
*
* exp = exponent( toFloat16( 3.14e-4 ) ); // => 2**-12 ≈ 2.44e-4
* // returns -12
*
* exp = exponent( toFloat16( -3.14 ) );
* // returns 1
*
* exp = exponent( 0.0 );
* // returns -15
*
* exp = exponent( NaN );
* // returns 16
*/

// MODULES //

var main = require( './main.js' );


// EXPORTS //

module.exports = main;
Loading
Loading