Skip to content

Commit b8af4a4

Browse files
committed
fix: float decimal (single-percision) detection
1 parent 030e650 commit b8af4a4

2 files changed

Lines changed: 4 additions & 5 deletions

File tree

lib/BufferEncoder.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,10 @@ BufferEncoder.prototype.encode = function(key, val, index) {
9292
* @param {Number} index
9393
*/
9494
BufferEncoder.prototype._asFloat = function(val, index) {
95-
// TODO: bad detection
96-
var valLen = val.toString().split('.')[1].length;
95+
var isFloat32 = (Math.fround(val) === val);
9796

9897
// Float.
99-
if (valLen <= 6) {
98+
if (isFloat32) {
10099
this._setMark(marks.DEFAULT_MARK_FLOAT, index);
101100
this._toFloat32(val);
102101
}

test/BufferEncoder.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ describe("BufferEncoder", () => {
114114
expect(bufferEncoder._offset).toBe(16);
115115
});
116116

117-
it("encodes 1.234567 as 5 bytes (float with mark)", () => {
118-
bufferEncoder.encode(undefined, 1.234567);
117+
it("encodes 123.45600128173828 as 5 bytes (float with mark)", () => {
118+
bufferEncoder.encode(undefined, 123.45600128173828);
119119
expect(bufferEncoder._offset).toBe(5);
120120
});
121121

0 commit comments

Comments
 (0)