Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
104 changes: 104 additions & 0 deletions lib/node_modules/@stdlib/number/float16/base/from-word/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<!--
@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.
-->

# fromWord

> Create a [half-precision floating-point number][ieee754] from an unsigned integer corresponding to an [IEEE 754][ieee754] binary representation.
<section class="usage">

## Usage

```javascript
var fromWord = require( '@stdlib/number/float16/base/from-word' );
```

#### fromWord( word )

Creates a [half-precision floating-point number][ieee754] from an unsigned `integer` corresponding to an [IEEE 754][ieee754] binary representation.

```javascript
var word = 15411; // => 0 01111 0000110011

var f16 = fromWord( word ); // when printed, implicitly promoted to float64
// returns 1.0498046875
```

</section>

<!-- /.usage -->

<section class="notes">

</section>

<!-- /.notes -->

<section class="examples">

## Examples

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

```javascript
var uniform = require( '@stdlib/random/array/uniform' );
var round = require( '@stdlib/math/base/special/round' );
var map = require( '@stdlib/array/base/map' );
var naryFunction = require( '@stdlib/utils/nary-function' );
var pickArguments = require( '@stdlib/utils/pick-arguments' );
var logEachMap = require( '@stdlib/console/log-each-map' );
var MAX_UINT16 = require( '@stdlib/constants/uint16/max' );
var fromWord = require( '@stdlib/number/float16/base/from-word' );

// Generate an array of random numbers:
var arr = uniform( 1000, 0.0, MAX_UINT16 );

// Round each number:
var word = map( arr, naryFunction( round, 1 ) );

// Create half-precision floating-point numbers from unsigned integers...
logEachMap( 'word: %d => float16: %f', word, pickArguments( fromWord, [ 0 ] ) );
```

</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,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.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var uniform = require( '@stdlib/random/array/uniform' );
var round = require( '@stdlib/math/base/special/round' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var map = require( '@stdlib/array/base/map' );
var naryFunction = require( '@stdlib/utils/nary-function' );
var MAX_UINT16 = require( '@stdlib/constants/uint16/max' );
var pkg = require( './../package.json' ).name;
var fromWord = require( './../lib' );


// MAIN //

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

x = uniform( 100, 0.0, MAX_UINT16 );
word = map( x, naryFunction( round, 1 ) );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = fromWord( word );
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,24 @@

{{alias}}( word )
Creates a half-precision floating-point number from an unsigned integer
corresponding to an IEEE 754 binary representation.

Parameters
----------
word: integer
Unsigned integer.

Returns
-------
out: float
Half-precision floating-point number.

Examples
--------
> var word = 15411; // => 0 01111 0000110011
> var f16 = {{alias}}( word ) // when printed, promoted to float64
1.0498046875

See Also
--------

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* @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

/**
* Creates a half-precision floating-point number from an unsigned integer corresponding to an IEEE 754 binary representation.
*
* @param word - unsigned integer
* @returns half-precision floating-point number
*
* @example
* var word = 15411; // => 0 01111 0000110011
*
* var f16 = fromWord( word ); // when printed, implicitly promoted to float64
* // returns 1.0498046875
*/
declare function fromWord( word: number ): number;


// EXPORTS //

export = fromWord;
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* @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 fromWord = require( './index' );


// TESTS //

// The function returns a number...
{
fromWord( 15411 ); // $ExpectType number
}

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

// The compiler throws an error if the function is provided insufficient arguments...
{
fromWord(); // $ExpectError
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @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 uniform = require( '@stdlib/random/array/uniform' );
var round = require( '@stdlib/math/base/special/round' );
var map = require( '@stdlib/array/base/map' );
var naryFunction = require( '@stdlib/utils/nary-function' );
var pickArguments = require( '@stdlib/utils/pick-arguments' );
var logEachMap = require( '@stdlib/console/log-each-map' );
var MAX_UINT16 = require( '@stdlib/constants/uint16/max' );
var fromWord = require( './../lib' );

// Generate an array of random numbers:
var arr = uniform( 1000, 0.0, MAX_UINT16 );

// Round each number:
var word = map( arr, naryFunction( round, 1 ) );

// Create half-precision floating-point numbers from unsigned integers...
logEachMap( 'word: %d => float16: %f', word, pickArguments( fromWord, [ 0 ] ) );
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* @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';

/**
* Create a half-precision floating-point number from an unsigned integer corresponding to an IEEE 754 binary representation.
*
* @module @stdlib/number/float16/base/from-word
*
* @example
* var fromWord = require( '@stdlib/number/float16/base/from-word' );
*
* var word = 15411; // => 0 01111 0000110011
*
* var f16 = fromWord( word ); // when printed, implicitly promoted to float64
* // returns 1.0498046875
*/

// MODULES //

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


// EXPORTS //

module.exports = main;
Loading
Loading