【问题标题】:Decrease the increase of value in database减少数据库中值的增加
【发布时间】:2017-12-06 22:33:47
【问题描述】:

我的起始值是 18,该数字每增加 1,值的增加应该减少。基本上,如果你拥有更多的价值,那么增加这个价值就会变得更加困难。增加量应比之前的增加量少 2%,如下所示。

现在我每次跑步只能稳定增加 2 个。

$Strengthvalue = 2 + $row['strength'];
  • 18 = +2(增加)
  • 19 = +1.96(增加)
  • 20 = +1.92(增加)

【问题讨论】:

  • 你使用数据库吗?如果这样做,您可以保存起始值或之前的增加值。从那里,您可以计算下一个值需要增加多少

标签: php math


【解决方案1】:

我认为您需要将符号值更改为 - 这样:

$Strengthvalue = 2 + $row['strength'];
$stengthvalue = -0.04 + $Strengthvalue // to decrease current value

【讨论】:

    【解决方案2】:

    您的起始强度是静态的,因此您可以根据当前强度与起始点的差异来计算增加量。

    $currentStrength = $row["strength"];
    $increment = (1 - ($currentStrength - 18) * 0.02) * 2;
    $StrengthValue = $increment + $currentStrength;
    

    【讨论】:

      【解决方案3】:

      如果您想精确而不出现舍入问题,我建议使用递归方法:

      <?php
      
      function calculate_increase($current_strength) {
        if($current_strength == 18) {
            return 2;
        }
      
        return 0.98 * calculate_increase($current_strength-1);
      }
      //calculate_increase(20) == 1.9208
      

      【讨论】:

        猜你喜欢
        • 2021-12-31
        • 2013-04-08
        • 2012-05-30
        • 2021-11-21
        • 2020-07-31
        • 1970-01-01
        • 1970-01-01
        • 2021-12-02
        • 1970-01-01
        相关资源
        最近更新 更多