From eb0a55af583593dfa0abf57a91cd270a0d859323 Mon Sep 17 00:00:00 2001 From: Lars Mueller Date: Sat, 2 Jul 2022 17:41:11 +0200 Subject: [PATCH] Bluon: Clean up number writing --- bluon.lua | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/bluon.lua b/bluon.lua index e8465a0..d9206d8 100644 --- a/bluon.lua +++ b/bluon.lua @@ -9,7 +9,8 @@ setfenv(1, _ENV) --! experimental local no_op = modlib.func.no_op -local write_float = modlib.binary.write_float +local fround = modlib.math.fround +local write_single, write_double = modlib.binary.write_single, modlib.binary.write_double local metatable = {__index = _ENV} @@ -122,11 +123,8 @@ function len(self, value) if value % 1 == 0 then return 1 + uint_len(value > 0 and value or -value) end - -- HACK use write_float to get the length local bytes = 4 - write_float(no_op, value, function(double) - if double then bytes = 8 end - end) + if fround(value) ~= value then bytes = 8 end return 1 + bytes end local id = object_ids[value] @@ -184,11 +182,14 @@ function write(self, value, stream) byte(base + type_offset) uint(type_offset, _uint) end - local function float_on_write(double) - byte(double and type_ranges.number or type_ranges.number_f32) - end local function float(number) - write_float(byte, number, float_on_write) + if fround(number) == number then + byte(type_ranges.number_f32) + write_single(byte, number) + else + byte(type_ranges.number) + write_double(byte, number) + end end local aux_write = self.aux_write local function _write(value)