仅使用算术运算符复制变量

时间:2018-03-13 15:50:09

标签: variables math integer-arithmetic paradox-scripting

在没有直接赋值函数的情况下,将一个变量的值复制到另一个变量的最佳方法是什么?

变量是2个字节的定点数,最大值为2147483.647。可用的运算符(为清晰起见重新格式化):

  • add(<variable>, <constant>)为给定变量
  • 添加常量值
  • subtract(<variable>, <constant>)减去
  • multiply(<variable>, <constant>)乘以
  • divide(<variable>, <constant>)除以
  • check(<variable1>, <op>, <variable2/constant>)使用运算符variable1<variable2/constant>的值与op的值进行比较,并返回一个布尔值。 op可以是=<<=>=>
  • set(<variable>, <constant>)设置变量的值。
  • 脚本语言具有基本控制流,它有whileif(但不是else)以及基本布尔运算符ANDORNOTNOR

我们的第一次尝试就是:

while = {
    count = var1 //loop var1 times
    change_variable = { which = var2 value = 1 } //increment by one
}

我的下一次尝试是:

set_variable = { which = new value = 1 }
while = {
    limit = { NOT = { check_variable = { which = old value = new} } }
    if = {
        limit = { 
            check_variable = { which = old value > new } 
            check_variable = { which = stage value = 0 }
        }
        multiply_variable = { which = new value = 10 }
    }
    if = {
        limit = { 
            check_variable = { which = stage value = 0 } 
            check_variable = { which = old value < new } 
        }
        set_variable = { which = stage value = 1 }
    }
    if = {
        limit = {
            check_variable = { which = stage value = 1 }
            check_variable = { which = old value < new }
        }
        subtract_variable = { which = new value = 1 }
    }
}

例如,从1开始,乘以10直到大于目标,然后减去1直到达到目标。

但我确信有更好的方法可以做到这一点。

上下文 最近在游戏中使用的脚本语言补丁&#34; Stellaris&#34;破坏了变量赋值函数。上面的set应为set(<variable>, <variable/constant>)。这已经破坏了许多现有的代码,并且不知道它会在多长时间内被破坏。

1 个答案:

答案 0 :(得分:3)

因为2147483.647是(2 - 1的幂)/ 1000我可以使用2的连续幂来一次接近1位解决方案:(使用等效操作用C语言编写)

result = 0;
result +=  1073741.824;
if( var < result )
    result -=  1073741.824;

result += 536870.912;
if( var < result)
    result -= 536870.912;

result += 268435.456;
if( var < result)
    result -= 268435.456;

result += 134217.728;
if( var < result)
    result -= 134217.728;

result += 67108.864;
if( var < result)
    result -= 67108.864;

result += 33554.432;
if( var < result)
    result -= 33554.432;

result += 16777.216;
if( var < result)
    result -= 16777.216;

result += 8388.608;
if( var < result)
    result -= 8388.608;

result += 4194.304;
if( var < result)
    result -= 4194.304;

result += 2097.152;
if( var < result)
    result -= 2097.152;

result += 1048.576;
if( var < result)
    result -= 1048.576;

result += 524.288;
if( var < result)
    result -= 524.288;

result += 262.144;
if( var < result)
    result -= 262.144;

result += 131.072;
if( var < result)
    result -= 131.072;

result += 65.536;
if( var < result)
    result -= 65.536;

result += 32.768;
if( var < result)
    result -= 32.768;

result += 16.384;
if( var < result)
    result -= 16.384;

result +=  8.192;
if( var < result)
    result -=  8.192;

result +=  4.096;
if( var < result)
    result -=  4.096;

result += 2.048;
if( var < result)
    result -= 2.048;

result += 1.024;
if( var < result)
    result -= 1.024;

result += 0.512;
if( var < result)
    result -= 0.512;

result += 0.256;
if( var < result)
    result -= 0.256;

result += 0.128;
if( var < result)
    result -= 0.128

result += 0.064;
if( var < result)
    result -= 0.064;

result += 0.032;
if( var < result)
    result -= 0.032;

result += 0.016;
if( var < result)
    result -= 0.016;

result += 0.008;
if( var < result)
    result -= 0.008;

result += 0.004;
if( var < result)
    result -= 0.004;

result += 0.002;
if( var < result)
    result -= 0.002;

result += 0.001;
if( var < result)
    result -= 0.001;

只需要进行32次比较(如果精度较低或知道上限,则可以减少比较)。