@@ -263,6 +263,8 @@ if (!DataView.prototype.getFloat16) {
263
263
// byteOffset: The offset where the 16-bit float is stored
264
264
// littleEndian: Specifies the endianness of the data
265
265
266
+ // Used references: https://en.wikipedia.org/wiki/Half-precision_floating-point_format
267
+
266
268
// Read the 16-bit float as an unsigned integer
267
269
const uint16 = this . getUint16 ( byteOffset , littleEndian ) ;
268
270
@@ -289,7 +291,7 @@ if (!DataView.prototype.getFloat16) {
289
291
// If exponent is 31, it's infinity or NaN
290
292
if ( mantissa === 0 ) {
291
293
// Positive or negative infinity
292
- return ( sign === 1 ) ? Number . POSITIVE_INFINITY : Number . NEGATIVE_INFINITY ;
294
+ return ( sign === 1 ) ? Infinity : - Infinity ;
293
295
} else {
294
296
// NaN
295
297
return NaN ;
@@ -316,9 +318,6 @@ if (!DataView.prototype.setFloat16) {
316
318
/// Returns:
317
319
/// None.
318
320
///
319
- /// Throws:
320
- /// - RangeError if the provided float16 value is out of the representable range for float16.
321
- ///
322
321
/// Details:
323
322
/// The function encodes the provided float16 value as per IEEE 754-2008 standard for half-precision
324
323
/// floating-point numbers and writes it to the specified offset in the DataView. If the value is
@@ -359,12 +358,13 @@ if (!DataView.prototype.setFloat16) {
359
358
mantissa = Math . floor ( ( value / Math . pow ( 2 , exponent - 15 ) - 1 ) * 1024 ) ;
360
359
361
360
if ( mantissa === 1024 ) {
362
- // Handle rounding that causes exponent overflow.
361
+ // This needs to be done otherwise this will cause exponent overflow.
363
362
exponentAndMantissa += 1 ;
364
363
exponent = exponentAndMantissa ;
365
364
mantissa = 0 ;
366
365
}
367
366
367
+ // Check if we overflow into Infinity.
368
368
if ( exponentAndMantissa > 30 ) {
369
369
// Handle overflow to Infinity.
370
370
exponent = 0x1F ;
0 commit comments