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
102 changes: 102 additions & 0 deletions lib/node_modules/@stdlib/number/float64/base/to-float16/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<!--

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

-->

# toFloat16

> Convert a [double-precision floating-point number][ieee754] to the nearest [half-precision floating-point number][half-precision-floating-point-format].

<section class="usage">

## Usage

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

#### float64ToFloat16( x )

Converts a [double-precision floating-point number][ieee754] to the nearest [half-precision floating-point number][ieee754].

```javascript
var y = float64ToFloat16( 1.337 );
// returns 1.3369140625
```

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- This function may be used as a polyfill for the ES2015 built-in [`Math.f16round`][math-f16round].

</section>

<!-- /.notes -->

<section class="examples">

## Examples

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

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

var f64;
var f16;
var i;

// Convert random double-precision floating-point numbers to the nearest half-precision floating-point number...
for ( i = 0; i < 1000; i++ ) {
f64 = randu() * 100.0;
f16 = float64ToFloat16( f64 );
console.log( 'float64: %d => float16: %d', f64, f16 );
}
```

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

[half-precision-floating-point-format]: https://en.wikipedia.org/wiki/Half-precision_floating-point_format

[math-f16round]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/f16round

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* @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 pkg = require( './../package.json' ).name;
var float64ToFloat16 = require( './../lib' );
var polyfill = require( './../lib/polyfill.js' );


// VARIABLES //

var opts = {
'skip': ( typeof Math.f16round === 'undefined' ) // eslint-disable-line stdlib/no-builtin-math
};


// MAIN //

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

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*1.0e5 ) - 5.0e4;
y = float64ToFloat16( 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();
});

bench( pkg+'::polyfill', function benchmark( b ) {
var x;
var y;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*1.0e5 ) - 5.0e4;
y = polyfill( 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();
});

bench( pkg+'::builtin', opts, function benchmark( b ) {
var x;
var y;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*1.0e5 ) - 5.0e4;
y = Math.f16round( x ); // eslint-disable-line stdlib/no-builtin-math
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,22 @@

{{alias}}( x )
Converts a double-precision floating-point number to the nearest half-
precision floating-point number.

Parameters
----------
x: number
Double-precision floating-point number.

Returns
-------
out: float
Nearest half-precision floating-point number.

Examples
--------
> var y = {{alias}}( 1.337 )
1.3369140625

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

// TypeScript Version: 4.1

/**
* Convert a double-precision floating-point number to the nearest half-precision floating-point number.
*
* @param x - double-precision floating-point number
* @returns nearest half-precision floating-point number
*
* @example
* var y = float64ToFloat16( 1.337 );
* // returns 1.3369140625
*/
declare function float64ToFloat16( x: number ): number;


// EXPORTS //

export = float64ToFloat16;
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 float64ToFloat16 = require( './index' );


// TESTS //

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

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

// The compiler throws an error if the function is provided insufficient arguments...
{
float64ToFloat16(); // $ExpectError
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @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 float64ToFloat16 = require( './../lib' );

var f64;
var f16;
var i;

// Convert random double-precision floating-point numbers to the nearest half-precision floating-point number...
for ( i = 0; i < 1000; i++ ) {
f64 = randu() * 100.0;
f16 = float64ToFloat16( f64 );
console.log( 'float64: %d => float16: %d', f64, f16 );
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* @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';

/**
* Convert a double-precision floating-point number to the nearest half-precision floating-point number.
*
* @module @stdlib/number/float64/base/to-float16
*
* @example
* var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' );
*
* var y = float64ToFloat16( 1.337 );
* // returns 1.3369140625
*/

// MODULES //

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


// MAIN //

var float64ToFloat16;
if ( typeof builtin === 'function' ) {
float64ToFloat16 = builtin;
} else {
float64ToFloat16 = polyfill;
}


// EXPORTS //

module.exports = float64ToFloat16;
Loading
Loading