Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2000x 2000x 2000x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 10010x 10010x 10010x 10010x 10010x 10010x 10010x 10010x 18x 18x 10010x 4996x 4996x 4996x 4996x 9992x 10010x 2000x 2000x 7992x 7992x 7992x 7992x 10010x 7992x 10010x 2x 2x 2x 2x 2x | /**
* @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 isnanf = require( '@stdlib/math/base/assert/is-nanf' );
var f32 = require( '@stdlib/number/float64/base/to-float32' );
var isFiniteNumber = require( '@stdlib/math/base/assert/is-finitef' );
var FLOAT16_EPSILON = require( '@stdlib/constants/float16/eps' );
var FLOAT16_MAX = require( '@stdlib/constants/float16/max' );
var FLOAT16_MIN = require( '@stdlib/constants/float16/smallest-normal' );
var EPS = require( '@stdlib/constants/float32/eps' );
var PINF = require( '@stdlib/constants/float32/pinf' );
var absf = require( '@stdlib/math/base/special/absf' );
// VARIABLES //
var INVERSE_EPSILON = f32( 1.0 / EPS );
// FUNCTIONS //
/**
* Performs banker's rounding (round-half-to-even) via floating-point arithmetic.
*
* @private
* @param {number} n - input value
* @returns {number} rounded value
*/
function roundTiesToEven( n ) {
return f32( f32( n + INVERSE_EPSILON ) - INVERSE_EPSILON );
}
// MAIN //
/**
* Converts a single-precision floating-point number to the nearest half-precision floating-point number.
*
* @param {number} x - single-precision floating-point number
* @returns {number} nearest half-precision floating-point number
*
* @example
* var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' );
*
* var y = float32ToFloat16( float64ToFloat32( 1.337 ) );
* // returns 1.3369140625
*/
function float32ToFloat16( x ) {
var res;
var mod;
var a;
var s;
x = f32( x );
if ( x === 0.0 || isnanf( x ) || !isFiniteNumber( x ) ) {
return x;
}
if ( x < 0.0 ) {
s = f32( -1.0 );
} else {
s = f32( 1.0 );
}
mod = absf( x );
if ( mod < FLOAT16_MIN ) {
return f32( f32( f32( s * roundTiesToEven( f32( f32( mod / FLOAT16_MIN ) / FLOAT16_EPSILON ) ) ) * FLOAT16_MIN ) * FLOAT16_EPSILON ); // eslint-disable-line max-len
}
// Leverage Veltkamp's algorithm for splitting a number into two numbers to generate an approximation to `x` which fits in a smaller number of bits:
a = f32( f32( 1.0 + f32( FLOAT16_EPSILON / EPS ) ) * mod );
res = f32( a - f32( a - mod ) );
if ( res > FLOAT16_MAX || isnanf( res ) ) {
return f32( s * PINF );
}
return f32( s * res );
}
// EXPORTS //
module.exports = float32ToFloat16;
|