此计算器的Angular.js平方根

时间:2018-07-22 00:43:52

标签: angularjs

我到处寻找简化的平方根短代码版本,并在JavaScript,Java,jQuery中找到了很多版本,但是在Angular.js中却找不到任何版本。

下面是我的代码:

<body align"center"><form><h1 align="center">Quick 2 number Angular.js Calculator </h1></center>
<center><h6>- Teaching aide only -</h6></center>
    <div ng-app="">

    <input type="number" ng-model="numb1" placeholder="number1" size="5px"><br />

    <input type="number" ng-model="numb2" placeholder="number2" size="5px"><br />

<b style="color:#0000FF"><button disabled>+</button>
<button disabled>-</button> 
<button disabled>X</button> 
<button disabled>/</button></b>
<button style="background-color:lime;color:green" disabled>ENT</button>
<button style="background-color:orange;color:red">CLR</button>
<center><h2>Answers Below</h2></center>

 <p>My first expression: {{ numb1 }} ; {{ numb2 }}</p>

 <p>Addition: {{ numb1 + numb2 }}</p>
 <p>Subtraction: {{ numb1 - numb2 }}</p>
 <p>Multiplication: {{ numb1 * numb2 }}</p>
 <p>Division: {{ numb1 / numb2 }}</p>

 <p>Square of {{ numb1 }}<small><sup>2</sup></small> is {{ numb1 * numb1 }}<br>Square of {{ numb2 }}<small><sup>2</sup></small> is {{ numb2 * numb2 }}</p>
 <p>Cube of {{ numb1 }}<small><sup>3</sup></small> is {{ numb1 * numb1 * numb1 }}<br>Cube of {{ numb2 }}<small> 
 <sup>3</sup></small> is {{ numb2 * numb2 * numb2 }}</p>
</form>

3 个答案:

答案 0 :(得分:0)

在这种情况下,javascript具有您想要的功能。为此,您需要将参数传递给Math.sqrt函数。这是来自MDN的示例。您可以创建服务或指令来包装此函数,但是angularjs本身没有此函数。

function calcHypotenuse(a, b) {
  return(Math.sqrt((a * a) + (b * b)));
}

console.log(calcHypotenuse(3, 4));
// expected output: 5

console.log(calcHypotenuse(5, 12));
// expected output: 13

console.log(calcHypotenuse(0, 0));
// expected output: 0

如果必须创建自定义解决方案,建议您查看该函数本身的javascript代码,以了解其实现方式。例如,您可以转到Firefox下载其实现。

我可以找到算法,但是下载后找不到那里的代码。

  

平方根

     

使用简单的迭代算法来计算最大整数   小于或等于平方根。本质上,这是牛顿的   线性近似,通过找到   等式:

      x[k]^2 - V x[k+1]    =  x[k] - ------------
           2 x[k]
     

...其中V是要求平方根的值。在   本质上,这里发生的是我们猜测   平方根,然后通过平方我们的猜测来找出我们有多远   然后减去目标使用此值,我们计算线性   误差的近似值,并调整“猜测”。我们一直在做   直到精度变得足够低以至于上面的等式   产生商为零。至此,我们最后的猜测是   大于我们要寻找的平方根。

     

通过将V除以4来计算初始猜测,这是一种启发式方法   我发现平均而言还不错。这也有   易于高效计算的优势,即使对于大型   值。

     

因此,所得算法的工作原理如下:

x = V / 4   /* compute initial guess */

loop  t = (x * x) - V   /* Compute absolute error  */     u = 2 * x         /* Adjust by tangent slope */     t = t / u
     

/ *如果错误为零,则执行循环* / if(t == 0)         打破

     

/ *通过错误项调整猜测值* / x = x-t       结束

x = x - 1
     

计算结果是x的值。

答案 1 :(得分:0)

在HTML中:
调用角度控制器中定义的立方体和正方形函数

<input type="number" ng-model="numb1" placeholder="number1" ng-change="cube(numb1)" size="5px"><br />

<p>Square of {{ numb1 }}<small><sup>2</sup></small> is {{ cubeResult }}</p>

在Controller(*。js)中:

定义一个返回特定值的平方和立方的函数

  $scope.cubeResult =0;
  $scope.square= function(value){
       $scope.cubeResult = value * value;
  };

  $scope.cube = function(value){
       $scope.cubeResult = value * value * value;
  };

答案 2 :(得分:0)

最好的方法是为此使用过滤器:

angular.module('app', []).filter('sqrt', function() {
   return function(number) {
      return Math.sqrt(number);
   };
});
<div ng-app="app">
  <input type="number" ng-model="numb1" placeholder="number1" size="5px"><br />
  <div>{{ numb1 | sqrt }}</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.2/angular.min.js"></script>