Skip to content

Commit

Permalink
fix: float decimal (single-percision) detection
Browse files Browse the repository at this point in the history
  • Loading branch information
usernmae0101 committed Jan 9, 2024
1 parent 030e650 commit b8af4a4
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
5 changes: 2 additions & 3 deletions lib/BufferEncoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,10 @@ BufferEncoder.prototype.encode = function(key, val, index) {
* @param {Number} index
*/
BufferEncoder.prototype._asFloat = function(val, index) {
// TODO: bad detection
var valLen = val.toString().split('.')[1].length;
var isFloat32 = (Math.fround(val) === val);

// Float.
if (valLen <= 6) {
if (isFloat32) {
this._setMark(marks.DEFAULT_MARK_FLOAT, index);
this._toFloat32(val);
}
Expand Down
4 changes: 2 additions & 2 deletions test/BufferEncoder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ describe("BufferEncoder", () => {
expect(bufferEncoder._offset).toBe(16);
});

it("encodes 1.234567 as 5 bytes (float with mark)", () => {
bufferEncoder.encode(undefined, 1.234567);
it("encodes 123.45600128173828 as 5 bytes (float with mark)", () => {
bufferEncoder.encode(undefined, 123.45600128173828);
expect(bufferEncoder._offset).toBe(5);
});

Expand Down

0 comments on commit b8af4a4

Please sign in to comment.