0

Here is my code. two num plus > 0 but add a variable < 0 !!!

function addExp(actorExp, val)
    local actorExp = actorExp  -- actorExp  = 800000000
    local val = val -- val = 3000000000
    if actorExp + val  -- Here actorExp > 0 then
        -- actorExp + val > 0 but 
       actorExp = actorExp + val  -- Here actorExp  = -2147483648 ???
    else return end
    updateInfo(actor)
end

how can i fix it

  • A 32-bit signed integer. It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive). Maybe this will give you some insight: https://stackoverflow.com/a/5131140/5037430 – u-ways Jun 29 '22 at 21:28
  • 8
    What version of Lua is this, in what environment? – Erwin Jun 29 '22 at 21:31
  • It is inappropriate to change the commenting in a code block because you believe it to be "wrong", many Lua environments do allow C-like comments as an example garry's mod. As the author did not specify their environment, the commenting at least lends some information that they may not be in a "standard" environment. – Nifim Jun 30 '22 at 14:26

1 Answers1

0

This is called an overflow error.

The way computers store data is using binary (1s and 0s), and depending on the size (number of digits) of the binary used to store each number (often 32 'bits') will determine the maximum value. When the maximum value is reached, it will "overflow" back to the start, which in this case is -2147483648 as it is a "signed" (can be negative) number.

HPringles
  • 1,042
  • 6
  • 15