方角根角

时间:2015-10-15 14:40:25

标签: javascript angularjs math

我对编码很新,刚刚开始使用角度。现在我正在尝试制作一个简单的计算器,计算指数增长期间的增长因子。我当前的代码如下所示:

angular.module('eksponenskalkulator', [])
.controller('kalkController', ['$scope', function($scope) {
  $scope.p = {};
}]);
<!DOCTYPE html>
<html lang="en">
    
<head>
  <meta charset="UTF-8">
  <title>Example - example-example35-production</title>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0-beta.1/angular.min.js"></script>
  <script src="app.js"></script>
</head>

<body ng-app="eksponenskalkulator">
  <div ng-controller="kalkController">
  <form>
    Opprinnelig:
    <input type="text" ng-model="p.opprinnelig" /><br />
    Eksponent:
    <input type="text" ng-model="p.eksponent" /><br />
    Endelig:
    <input type="text" ng-model="p.endelig" /><br />
  </form>
  <pre>Svar = {{100*(p.endelig/p.opprinnelig-1)}}</pre>
</div>
</body>
</html>

这很有效,但我需要做的是取(p.endelig/p.opprinnelig)的第七个根。对于如何做到这一点我会非常感激。

2 个答案:

答案 0 :(得分:2)

对于常见的数学运算,有一个本地javascript Math library

对于您的示例,在获取第7个根时,您可以使用Math.pow函数,如下所示:

Math.pow(p.endelig/p.opprinnelig,1/7)

答案 1 :(得分:1)

这是一个工作片段,向您展示zacran的答案:

angular.module('eksponenskalkulator', [])
  .controller('kalkController', ['$scope',
    function($scope) {
      $scope.p = {
        opprinnelig: 0,
        eksponent: 0,
        endelig: 0
      };
      $scope.findSeventhRoot = function(x) {
        return Math.pow(x, 1 / 7);
      };
    }
  ]);
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Example - example-example35-production</title>
</head>

<body ng-app="eksponenskalkulator">
  <div ng-controller="kalkController">
    <form>
      Opprinnelig:
      <input type="number" ng-model="p.opprinnelig" />
      <br />Eksponent:
      <input type="number" ng-model="p.eksponent" />
      <br />Endelig:
      <input type="number" ng-model="p.endelig" />
      <br />
    </form>
    <pre>Svar = {{100*(findSeventhRoot(p.endelig/p.opprinnelig)-1)}}</pre>
  </div>

  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0-beta.1/angular.min.js"></script>
  <script src="app.js"></script>
</body>

</html>