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 | 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 10010x 10010x 10010x 10010x 10010x 10010x 10010x 18x 18x 10010x 4996x 4996x 4996x 4996x 9992x 9992x 10010x 2000x 2000x 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 abs = require( '@stdlib/math/base/special/abs' ); 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/float64/eps' ); var PINF = require( '@stdlib/constants/float64/pinf' ); // VARIABLES // var $isFinite = isFinite; var inverseEpsilon = 1 / 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 (n + inverseEpsilon) - inverseEpsilon; } // MAIN // /** * Converts a double-precision floating-point number to the nearest half-precision floating-point number. * * @param {number} x - double-precision floating-point number * @returns {number} nearest half-precision floating-point number * * @example * var y = float64ToFloat16( 1.337 ); * // returns 1.3369140625 */ function float64ToFloat16( x ) { var result; var mod; var a; var s; if ( x === 0 || isNaN( x ) || !$isFinite( x ) ) { return x; } if ( x < 0 ) { s = -1; } else { s = 1; } mod = abs( x ); if ( mod < FLOAT16_MIN ) { return ( s * roundTiesToEven( mod / FLOAT16_MIN / FLOAT16_EPSILON ) * FLOAT16_MIN * FLOAT16_EPSILON // eslint-disable-line max-len ); } // Veltkamp's splitting a = ( 1 + ( FLOAT16_EPSILON / EPS ) ) * mod; result = a - ( a - mod ); if ( result > FLOAT16_MAX || isNaN( result ) ) { return s * PINF; } return s * result; } // EXPORTS // module.exports = float64ToFloat16; |