当语言指示默认情况下舍入舍入时,乘以%s

时间:2018-03-06 02:18:46

标签: math floating-point erlang solidity fixed-point

在erlang:

cost(I, Miners) ->

  BasePrice = lists:nth(I, prices()),  

  Owned = lists:nth(I, Miners),

  Rate = increaseRate(I) / 100,

  Multiplier = math:pow((1 + Rate), Owned),

  floor(BasePrice * Multiplier).

例如,基本价格为8000,增长率为7,我拥有0

我期望的第一个价格:8000

购买我的第二个,增加率为7,我拥有1

我期望的第二个价格:

Multiplier = 1.07

8000 * 1.07 =

8560

一切正常。现在我必须在Solidity中实现它,它不能很好地完成十进制数学运算。它自动向下舍入,使得3/2 == 1处于Solidity状态。

我想在Solidity中重新创建我的成本函数。

function cost(uint _minerIndex, uint _owned) public view returns (uint) {
    uint basePrice = 8000;
    uint increaseRate = 7;
    return basePrice * ((1 + increaseRate / 100) ** _owned);
  }
如果increaseRate为&lt; <{increaseRate / 100将始终返回0 100。

我如何达到同样的效果?

来自文档:

"Fixed point numbers are not fully supported by Solidity yet. They can be declared, but cannot be assigned to or from."

1 个答案:

答案 0 :(得分:0)

一个简单的解决方案是

<div class="counterPartyId">
  <ul class="select2-choices">  
    <li class="select2-search-choice">    
        <div>ABN nv,</div>    
    </li>
    <li class="select2-search-field" >    
      <label for="s2id_autogen5" class="select2-offscreen"></label>       <input type="text" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" class="select2-input" id="s2id_autogen5" placeholder="" style="width: 103px;" aria-activedescendant="select2-result-label-7">  
    </li>
  </ul>
</div>

但也可能由于算术溢出而失败,具体取决于你的数字和坚固性支持的MaxInt。