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

@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.

-->

# toWord

> Return an unsigned 16-bit integer corresponding to the [IEEE 754][ieee754] binary representation of a [half-precision floating-point number][ieee754].

<section class="usage">

## Usage

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

#### toWord( x )

Returns an unsigned 16-bit `integer` corresponding to the [IEEE 754][ieee754] binary representation of a [half-precision floating-point number][ieee754].

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

var f16 = float64ToFloat16( 1.05 );
// returns 1.0498046875

var w = toWord( f16 ); // => 0 01111 0000110011
// returns 15411
```

</section>

<!-- /.usage -->

<section class="notes">

</section>

<!-- /.notes -->

<section class="examples">

## Examples

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

```javascript
var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' );
var randu = require( '@stdlib/random/base/randu' );
var toWord = require( '@stdlib/number/float16/base/to-word' );

var word;
var f64;
var f16;
var i;

// Convert half-precision floating-point numbers to integers representing the binary literal...
for ( i = 0; i < 1000; i++ ) {
f64 = (randu()*100.0) - 50.0;
f16 = float64ToFloat16( f64 );
word = toWord( f16 );
console.log( 'float64: %d => float16: %d => word: %d', f64, f16, word );
}
```

</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 toWord = 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.0e7 ) - 5.0e6;
y = toWord( 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();
});
25 changes: 25 additions & 0 deletions lib/node_modules/@stdlib/number/float16/base/to-word/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

{{alias}}( x )
Returns an unsigned 16-bit integer corresponding to the IEEE 754 binary
representation of a half-precision floating-point number.

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

Returns
-------
out: integer
Unsigned 16-bit integer.

Examples
--------
> var f16 = {{alias:@stdlib/number/float64/base/to-float16}}( 1.05 )
1.0498046875
> var w = {{alias}}( f16 )
15411

See Also
--------

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

// TypeScript Version: 4.1

/**
* Returns an unsigned 16-bit integer corresponding to the IEEE 754 binary representation of a half-precision floating-point number.
*
* @param x - half-precision floating-point number
* @returns unsigned 16-bit integer
*
* @example
* var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' );
*
* var f16 = float64ToFloat16( 1.05 );
* // returns 1.0498046875
*
* var w = toWord( f16 ); // => 0 01111 0000110011
* // returns 15411
*/
declare function toWord( x: number ): number;


// EXPORTS //

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


// TESTS //

// The function returns a number...
{
toWordf( 3.14 ); // $ExpectType number
toWordf( -3.14 ); // $ExpectType number
}

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

// The compiler throws an error if the function is provided insufficient arguments...
{
toWordf(); // $ExpectError
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* @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 float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' );
var randu = require( '@stdlib/random/base/randu' );
var toWord = require( './../lib' );

var word;
var f64;
var f16;
var i;

// Convert half-precision floating-point numbers to integers representing the binary literal...
for ( i = 0; i < 1000; i++ ) {
f64 = (randu()*100.0) - 50.0;
f16 = float64ToFloat16( f64 );
word = toWord( f16 );
console.log( 'float64: %d => float16: %d => word: %d', f64, f16, word );
}
Loading
Loading