Skip to content

Commit 3a7c1fb

Browse files
committed
Add support for reading 64-bit floats split across multiple buffers. Fixes #8
1 parent a26391c commit 3a7c1fb

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

lib/internal/buf.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,14 @@ class CombinedBuffer extends BaseBuffer {
473473
}
474474
};
475475
};
476+
477+
getFloat64 ( position ) {
478+
// At some point, a more efficient impl. For now, we copy the 8 bytes
479+
// we want to read and depend on the platform impl of IEEE 754.
480+
let b = alloc(8);
481+
for (var i = 0; i < 8; i++) { b.putUInt8(i, this.getUInt8( position + i )); };
482+
return b.getFloat64(0);
483+
}
476484
}
477485

478486
/**

test/internal/buf.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,20 @@ describe('CombinedBuffer', function() {
100100
expect(first).toBe(1);
101101
expect(second).toBe(2);
102102
});
103+
104+
it('should read divided float64', function() {
105+
// Given
106+
var inner = alloc(8);
107+
inner.putFloat64(0, 0.1);
108+
109+
var b = new CombinedBuffer([inner.readSlice(4),inner.readSlice(4)]);
110+
111+
// When
112+
var read = b.readFloat64();
113+
114+
// Then
115+
expect(read).toBe(0.1);
116+
});
103117
});
104118

105119
function writeString(b, str) {

0 commit comments

Comments
 (0)