lua-users home
lua-l archive

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


On Mon, May 16, 2016 at 7:04 AM, Philipp Janda <siffiejoe@gmx.net> wrote:
> Another syntax hack:
>
>     local function num( a, ... )
>       local r = 1.0 * a
>       for i = 1, select( '#', ... ) do
>         local v = select( i, ... )
>         assert( type( v ) == "number" and v >= 0 and v < 1000 )
>         r = r * 1000 + v
>       end
>       return r
>     end
>
>     print( num( 1,000,000,000,000 ) )    --> 1000000000000.0
>     print( num( 2,012,014,144,567 ) )    --> 2012014144567.0
>     print( num( 1,000,000,000,000,000,000,000 ) )    --> 1e+21
>
> Converting to floats only when an integer overflow would occur, or
> restricting fractions to the last argument is left as an exercise for the
> reader.

It should be possible to write a token filter that identifies the
num() function and transforms the input into a float in scientific
notation at compile time, to avoid the O(n) runtime overhead.

/s/ Adam