Multiplier and Adder calculation order

If I wanted to add a system where a value has a set of multipliers and adders associated with them, which of the two categories of modifiers should I take into account first?

Example Multipliers: (1.5, 0.8, 1.4) [Are multiplied to value]
Example Adders: (1, 10, -30) [Are added onto value]

If I’m understanding correctly, you’d set those values in a table, and iterate over them for each modifier, your sequence would go something like this:

  1. Start with a base value
  2. Multiply the base value by each of the multipliers
  3. Add up the products from the multiplication to get the multiplied value
  4. Add each of the “adders” to the multiplied value to get the final value

Example

-- Set the modifiers
local multipliers = {1.5, 0.8, 1.4}
local adders = {1, 10, -30}

-- Set the base value
local baseValue = 100

-- Apply the modifiers
local multipliedValue = baseValue
for i, multiplier in ipairs(multipliers) do
    multipliedValue = multipliedValue * multiplier
end

local finalValue = multipliedValue
for i, adder in ipairs(adders) do
    finalValue = finalValue + adder
end

-- The final value is now calculated
print(finalValue) -- Outputs 75

Outputs 75 hence the base value is 100, which is multiplied by EACH of the multipliers to get 210. Then, each of the “adders” are added to the multiplied value to get 75, thus outputting 75.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.