lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Wed, May 30, 2012 at 5:31 PM, Coda Highland <chighland@gmail.com> wrote:
> On Wed, May 30, 2012 at 3:34 AM, Alexander Gladysh <agladysh@gmail.com> wrote:
>> On Tue, May 29, 2012 at 9:03 PM, Paul K <paulclinger@yahoo.com> wrote:

>> -- convert numbers into loadable string, including inf, -inf and nan
>> local serialize_number
>> do
>>  local t =
>>  {
>>    [tostring(1/0)] = "1/0";
>>    [tostring(-1/0)] = "-1/0";
>>    [tostring(0/0)] = "0/0";
>>  }
>>  serialize_number = function(number)
>>    -- no argument checking - called very often
>>    local text = ("%.17g"):format(number)
>>    -- on the same platform tostring() and string.format()
>>    -- return the same results for 1/0, -1/0, 0/0
>>    -- so we don't need separate substitution table
>>    return t[text] or text
>>  end
>> end

> Hey, the substitution table keyed by tostring() is a clever idea. I
> wouldn't have thought of that! Great job. :)

Not my idea (done by one of our team members, see blame), but thanks
none the less. :-)

Alexander.