lua 中的反转指数
Reversing exponents in lua
我有一个算法来计算达到一个级别所需的 exp。但是我不知道如何根据他的经验得到想要的等级。
local function NextLevelXP(level)
return math.floor(1000 * (level ^ 2.0))
end
print(NextLevelXP(7)) -- output: 49000
现在我想要基于他的 EXP 的等级,例如:
local function MagicFunctionThatWillLetMeKnowTheLevel(exp)
return --[[math magics here]]
end
print(MagicFunctionThatWillLetMeKnowTheLevel(49000)) --output: 7
print(MagicFunctionThatWillLetMeKnowTheLevel(48999)) --output: 6
我尝试了一些更糟糕、更奇怪的算法,但没有成功。
这可以简化:
local function NextLevelXP(level)
return math.floor(1000 * (level ^ 2.0))
end
如果 level
始终是整数,则 math.floor
不是必需的。
要反转公式,您可以执行以下操作:
local function CurrentLevelFromXP(exp)
return math.floor((exp / 1000) ^ (1/2))
end
这里有必要对值进行下限,因为您将获得级别之间的值(如 7.1、7.5、7.8)。要反转 1000 的乘法,我们除以 1000,要反转指数,我们使用指数的倒数,在这种情况下,2 变为 1/2 或 0.5
此外,作为一种特殊情况,对于 ^2
,您可以简单地 math.sqrt
值。
我有一个算法来计算达到一个级别所需的 exp。但是我不知道如何根据他的经验得到想要的等级。
local function NextLevelXP(level)
return math.floor(1000 * (level ^ 2.0))
end
print(NextLevelXP(7)) -- output: 49000
现在我想要基于他的 EXP 的等级,例如:
local function MagicFunctionThatWillLetMeKnowTheLevel(exp)
return --[[math magics here]]
end
print(MagicFunctionThatWillLetMeKnowTheLevel(49000)) --output: 7
print(MagicFunctionThatWillLetMeKnowTheLevel(48999)) --output: 6
我尝试了一些更糟糕、更奇怪的算法,但没有成功。
这可以简化:
local function NextLevelXP(level)
return math.floor(1000 * (level ^ 2.0))
end
如果 level
始终是整数,则 math.floor
不是必需的。
要反转公式,您可以执行以下操作:
local function CurrentLevelFromXP(exp)
return math.floor((exp / 1000) ^ (1/2))
end
这里有必要对值进行下限,因为您将获得级别之间的值(如 7.1、7.5、7.8)。要反转 1000 的乘法,我们除以 1000,要反转指数,我们使用指数的倒数,在这种情况下,2 变为 1/2 或 0.5
此外,作为一种特殊情况,对于 ^2
,您可以简单地 math.sqrt
值。