Skip to content

Commit 43b047b

Browse files
Added some more comments and added reference to the getFloat16
1 parent f78dd9b commit 43b047b

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

scripts/yyBuffer.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ if (!DataView.prototype.getFloat16) {
263263
// byteOffset: The offset where the 16-bit float is stored
264264
// littleEndian: Specifies the endianness of the data
265265

266+
// Used references: https://en.wikipedia.org/wiki/Half-precision_floating-point_format
267+
266268
// Read the 16-bit float as an unsigned integer
267269
const uint16 = this.getUint16(byteOffset, littleEndian);
268270

@@ -289,7 +291,7 @@ if (!DataView.prototype.getFloat16) {
289291
// If exponent is 31, it's infinity or NaN
290292
if (mantissa === 0) {
291293
// Positive or negative infinity
292-
return (sign === 1) ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;
294+
return (sign === 1) ? Infinity : -Infinity;
293295
} else {
294296
// NaN
295297
return NaN;
@@ -316,9 +318,6 @@ if (!DataView.prototype.setFloat16) {
316318
/// Returns:
317319
/// None.
318320
///
319-
/// Throws:
320-
/// - RangeError if the provided float16 value is out of the representable range for float16.
321-
///
322321
/// Details:
323322
/// The function encodes the provided float16 value as per IEEE 754-2008 standard for half-precision
324323
/// 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) {
359358
mantissa = Math.floor((value / Math.pow(2, exponent - 15) - 1) * 1024);
360359

361360
if (mantissa === 1024) {
362-
// Handle rounding that causes exponent overflow.
361+
// This needs to be done otherwise this will cause exponent overflow.
363362
exponentAndMantissa += 1;
364363
exponent = exponentAndMantissa;
365364
mantissa = 0;
366365
}
367366

367+
// Check if we overflow into Infinity.
368368
if (exponentAndMantissa > 30) {
369369
// Handle overflow to Infinity.
370370
exponent = 0x1F;

0 commit comments

Comments
 (0)