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

Expression for "0 through X" with a +- if compare?

Asked by
RoboFrog 400 Moderation Voter
8 years ago

(This is a portion of the same script that my last 3 unanswered questions used, but it isn't a duplicate question in case similarities are made in the code)

As of now, I have an if statement which checks if an input number to a function is within +- 8 of a table value. Everything works, except it isn't comparing right with my current equation (or any combination therein).

Current code snippet for those who like visuals --

mdist = 16; -- actually references an int value, but let's just call it 16.
local Coordsx = { -- table is filled whenever something is okay-ed to spawn after this check.

}

function xcheck(xrand) -- xrand is a random number input within set parameters.
    local ixgood = 0;
    ixgood = 0;
    for index, value in pairs(Coordsx) do
        if (value+mdist < xrand and value-mdist < xrand)  then -- This is the problem comparison. Obviously this doesn't logically work out, the signs are just placeholders.
            ixgood = ixgood + 1;
            wait();
        else
            ixgood = ixgood - 1;
            wait();
        end
    end
    if ixgood == #Coordsx then
        wait();
        return "true"; -- Yes, it is a string on purpose.
    else
        return "false"; -- Yet again, on purpose.
    end
end

I've tried having an (and) or (and) setup, and have mixed and matched so many sign combos (after logic eluded me) to no avail. At this point, if I could just have something to check if it's within +1, +2, +3, et cetera, (without hard typing it), it'd make things so much easier.

Thanks for reading, and I look forward to any offered help!

0
What is the random input for? Goulstem 8144 — 8y
0
It's for finding a suitable x coordinate to compare with the current x coordinates in Coordsx. Much of the script is omitted to make it seem less intimidating. RoboFrog 400 — 8y

1 answer

Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
8 years ago

Use absolute values!

VALUE = 23
EXPECTED = 17
VARIANCE = 8

if math.abs(VALUE - EXPECTED) <= VARIANCE then
    print(string.format("%d is within %d of %d", VALUE, VARIANCE, EXPECTED))
end
0
Wow, this worked extremely well. I implemented it for both the x and z functions and now it works great. (I did however change <= to >= since it did the opposite at first) Thank you for the help! RoboFrog 400 — 8y
Ad

Answer this question