lua-users home
lua-l archive

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


In my opinion the "length" of a number can only be the length of the string generated when converting this number to a string with the default string conversion.
Note that keys in tables are not necessarily integers, they can be any number. It is an integer only to restrict tables to their sequence, but not all tables have a unique sequence, if there are "holes" such that there exists a integer key k in table t, where k >= 2, t[k] is not null, and t[k-1] is null).
Codes that expect sequences to give a meaning to "#t" cannot be portable if t is not a sequence with no holes. But there's no easy builtin properties of tables that can assert it forms a unique predictable sequence (note that the presence of non-integer keys in the table does not invalidate a sequence: a table is a valid sequence depending only on the subset of its keys that are integers, and keys below 1 are ignored: you can safely add a key 0 or -1 to any table that is a sequence without invalidating the sequence and what #t will return).
This behavior of tables in Lua and the very weak definition of "sequences" is very tricky.


Le mar. 7 mai 2019 à 17:49, Coda Highland <chighland@gmail.com> a écrit :
On Tue, May 7, 2019 at 10:01 AM Andrew Starks <andrew@starksfam.org> wrote:

On 5/7/19, 4:55 AM, "lua-l-bounces@lists.lua.org on behalf of Dirk Laurie" <lua-l-bounces@lists.lua.org on behalf of dirk.laurie@gmail.com> wrote:

    Lua 5.3.5  Copyright (C) 1994-2018 Lua.org, PUC-Rio
    > #-5
    stdin:1: attempt to get length of a number value
    stack traceback:
        stdin:1: in main chunk
        [C]: in ?
    > debug.setmetatable(0,{__len=math.abs})
    0
    > #-5
    5

 ----------

I like to give your crazy ideas at least a few brain cycles before I check out, but I'm wondering where you're going with this. I don't think I know enough math, but do math people have opinions about how long a number is, before they take mushrooms?

There are multiple, contradictory interpretations of "how long a number is". They all boil down to "how many digits?" but there are conflicting ways of answering that question.

How many digits are in 1? How many digits are in 1.00? How many digits are in 1e20? How many digits are in 0.001?

Are we counting decimal digits? Binary digits? Bytes? Is there a difference between the length of a float and the length of an int?

In the end the only answer that would be consistent would be to always return 8.

/s/ Adam