[PD-dev] pdlua: lua-5.3 problem with float/int representation

Martin Peach chakekatzil at gmail.com
Wed Aug 3 17:59:17 CEST 2016


On Mon, Aug 1, 2016 at 11:59 AM, Patric Schmitz <flavi0 at openmailbox.org>
wrote:

> Hi pd devs,
>
> I'm using pd 0.47.1 on archlinux which ships the latest lua-5.3
> by default. It seems that in 5.3 the internal representation of
> 'number' types differentiates between floating and integer types
> now. Quoting the manual:
> > The type number uses two internal representations, or two subtypes, one
> called integer and the other called float. Lua has explicit rules about
> when each representation is used, but it also converts between them
> automatically as needed (see §3.4.3).
>
> and also from §3.4.3:
"The conversion from numbers to strings uses a non-specified human-readable
format. For complete control over how numbers are converted to strings, use
the format function from the string library (see string.format
<http://www.lua.org/manual/5.3/manual.html#pdf-string.format>). "

In pd.lua,
function pd.Class:dispatch(inlet, sel, atoms)
  local m = self["in_" .. inlet .. "_" .. sel]
and
  m = self["in_" .. inlet]
...here we are looking for a function in the .pdlua script by building a
string
using the inlet number, which is a lua_Number and will be printed however
lua decides to print it.


This led to the problem that the 'inlet' variable to string
> conversion resulted in the value 1.0 instead of 1, so the
> dispatching of the method failed since no method named e.g.
> in_1.0_float exists.
>
> I was able to fix it by making the inlet value integral with
> > inlet = math.floor(inlet)
> in pd.lua pd.Class:dispatch.
> \
>
inlet = string.format("%d", inlet)
should also work.


> An integer-cast could be used as well, but that would not be
> compatible with older language versions. As found by Claude on
> IRC the floor version should be safe since "Rounding functions
> (math.ceil, math.floor, and math.modf) return an integer when the
> result fits in the range of an integer, or a float otherwise.".
> So as long as < 2^31 inlets are used, which one can reasonably
> assume, this should be fine.
>
> There are probably more places where this fix would need to be
> applied, such as the outlet dispatching, and the multi-receive
> example. It should also be commented to make obvious what is
> happening, like that no int-cast is used due to older language
> version compatibility.
>
> I think only two lines in pd.lua are affected by this. They could be
changed to
  local m = self["in_" .. string.format("%d", inlet) .. "_" .. sel]
and
  m = self["in_" .. string.format("%d", inlet)]
or
 local m = self["in_" .. math.floor(inlet) .. "_" .. sel]
and
  m = self["in_" .. math.floor(inlet)]

All the pd_lua scripts will usually pass through Class:dispatch so if the
correct function names are generated therein, there will be no other
effects.
pd_lua scripts may use integers internally of course but Pd itself uses
only floats. (And lua numbers are either doubles or floats,  pdlua.c will
convert them to float.)

Thanks for pointing this out,

Martin



> Thanks,
> Patric
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puredata.info/pipermail/pd-dev/attachments/20160803/73eebbf8/attachment.html>


More information about the Pd-dev mailing list