lua-users home
lua-l archive

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



On 03/06/2014 15:16, Jorge wrote:
I don't know what it does, but the third line is

for x=i,math.huge do

I can not read the code beyond that line. I will be stuck looking at that and thinking for a while.
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> for x=1,3 do print(x) end
1
2
3

If you replace the 3 with math.huge it keeps going forever, and because 1+1 = 2 (and 2+1=3 etc), we can combine a for loop, the input (i) and math.huge to increase x until we exit the loop.
So:
    for x=i,math.huge do
        if x > i then
            return x
        end
    end

Can also be written as:
    local x = i
    while x <= math.huge do
        if x > i then
            return x
        end
        x = x + 1
    end

Adding with for loops is better because it supports floats. For positive b:
    local c
    for x=a,math.huge,b do
        if c then return x end
        c = true
    end

And for negative b:
    local c
    for x=a,-math.huge,b do
        if c then return x end
        c = true
    end

If b is 0 (or -0) the loop doesn't run, so we need special handling for that, which's basically a "return a" right after the loop:
    local c
    for x=a,(b >= 0 and math.huge or -math.huge),b do
        if c then return x end
        c = true
    end
    return a

And so on...

J.

On 06/03/2014 03:09 AM, David Demelier wrote:

Le 03/06/2014 03:56, Thiago L. a écrit :
I've been working on some stupid/troll code (mostly to bypass some
stupid/troll restrictions), and this is what it looks like:
https://github.com/SoniEx2/Stuff

And ofc, because this is a stupid thing, it should be as stupid as
possible, so here's a bug: https://github.com/SoniEx2/Stuff/issues/1

Feel free to send pull requests! (or hate me :c)

(This is more of an experiment than an actual thing, so let's see how
it goes!)

What's the use case of this ?