@stdlib/number-float64-base-to-float16
v0.1.2
Published
Convert a double-precision floating-point number to the nearest half-precision floating-point number.
Readme
toFloat16
Convert a double-precision floating-point number to the nearest half-precision floating-point number.
Installation
npm install @stdlib/number-float64-base-to-float16Usage
var float64ToFloat16 = require( '@stdlib/number-float64-base-to-float16' );float64ToFloat16( x )
Converts a double-precision floating-point number to the nearest half-precision floating-point number.
var y = float64ToFloat16( 1.337 );
// returns 1.3369140625Notes
- This function may be used as a polyfill for the ES2025 built-in
Math.f16round.
Examples
var uniform = require( '@stdlib/random-array-uniform' );
var logEachMap = require( '@stdlib/console-log-each-map' );
var float64ToFloat16 = require( '@stdlib/number-float64-base-to-float16' );
// Generate an array of random numbers:
var x = uniform( 100, 0.0, 100.0 );
// Convert each double-precision floating-point number to the nearest half-precision floating-point number:
logEachMap( 'float64: %f => float16: %f', x, float64ToFloat16 );C APIs
Usage
#include "stdlib/number/float64/base/to_float16.h"stdlib_base_float64_to_float16( x )
Converts a double-precision floating-point number to the nearest half-precision floating-point number.
#include "stdlib/number/float16/ctor.h"
stdlib_float16_t x = stdlib_base_float64_to_float16( 3.14 );The function accepts the following arguments:
- x:
[in] doubleinput value.
stdlib_float16_t stdlib_base_float64_to_float16( const double x );Examples
#include "stdlib/number/float64/base/to_float16.h"
#include "stdlib/number/float16/ctor.h"
#include <stdint.h>
#include <stdio.h>
int main( void ) {
const double x[] = { 3.14, -3.14, 0.0, 0.0/0.0 };
stdlib_float16_t v;
int i;
for ( i = 0; i < 4; i++ ) {
v = stdlib_base_float64_to_float16( x[ i ] );
printf( "%lf => uint16: %d\n", x[ i ], stdlib_float16_to_bits( v ) );
}
}Notice
This package is part of stdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.
For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.
Community
License
See LICENSE.
Copyright
Copyright © 2016-2026. The Stdlib Authors.
