ToxicFrog / vstruct

A Lua library for packing and unpacking binary data, supporting arbitrary (byte-aligned) widths, named fields, and repetition.
https://github.com/ToxicFrog/vstruct
Other
110 stars 14 forks source link

Floating point number parsing/writing broken for latest Lua #31

Closed wyozi closed 4 years ago

wyozi commented 7 years ago

Can be fixed with

diff --git a/io/f.lua b/io/f.lua
index 2682260..88d6000 100644
--- a/io/f.lua
+++ b/io/f.lua
@@ -45,7 +45,7 @@ local function reader(data, size_exp, size_fraction)
   -- Decrease the size of the exponent rather than make the fraction (0.5, 1]
   exponent = exponent - size_fraction

-  return sign * math.ldexp(fraction, exponent)
+  return sign * (fraction * 2.0^exponent)
 end

Did not PR because not sure how this will affect Lua versions without ^

wyozi commented 7 years ago

Similarly math.frexp no longer exists

ToxicFrog commented 7 years ago

I haven't really kept up on lua releases because I'm using luajit for all my projects now, which is fairly stable at a blend of 5.1 and 5.2 features. I'll see if I can take a look at this sometime in the next few weeks.

ToxicFrog commented 4 years ago

This is fixed-ish as of 064b602385fdbadb469b79d81160e2987b4b8225 (2.1.0). It does require math.frexp() still (and thus LUA_COMPAT_MATHLIB, which is the default on at least some distros).