Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why won't this print anything?

Asked by
funyun 958 Moderation Voter
9 years ago

Somehow this does not print anything at all. It doesn't give me an error message. Pls halp

local x = 2 --x is 2
local msg = "A" --msg is A
local msgbyte = string.byte(msg)--msgbyte is 65

for i = 0, 7 do --All exponents of 2 in the basic binary notation.
    x = x^i --Get the i'th power of x
    if msgbyte/x == math.floor(1) then --if 65 divided by 64 is about 1, then
        print(i) --print the exponent, or the iteration
        end
    x = 2 --Set x back to 2 for the next iteration
end

1 answer

Log in to vote
0
Answered by 9 years ago

You put the math.floor in the wrong place. You said math.floor(1) which is unnecessary because it will always return 1. You should have said math.floor(msgbyte/x) == 1, which rounds msgbyte/x down towards negative infinity. I cleaned up the code for you:

local x = 2 --x is 2
local msg = "A" --msg is A
local msgbyte = string.byte(msg)--msgbyte is 65

for Exp = 0, 7 do --All exponents of 2 in the basic binary notation.
    local newx = x ^ Exp --Get the Exp'th power of x
    if math.floor(msgbyte / newx) == 1 then
        print(i) --print the exponent, or the iteration
    end
end

Hope this helped!

Ad

Answer this question