Javascript:PI(π)计算器 [英] Javascript: PI (π) Calculator

查看:137
本文介绍了Javascript:PI(π)计算器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以计算Java中的pi?我知道您可以在其中使用 Math.PI 查找这样的馅饼:

Is there a way to calculate pi in Javascript? I know there you can use Math.PI to find pie like this:

var pie = Math.PI;
alert(pie); // output "3.141592653589793"

但这是不正确的.我想要的是能够进行计算,并具有所需的位数,而不是 pie = 3.141592 ... 之类的东西.但是,我想要的不仅仅是拥有更多的数字,而是尽可能多的(例如拥有一千个数字,但我需要更多).

but this is not accurate. What I want is to be able to calculate it, to have as many digits as you want, not anything like pie = 3.141592.... But still, what I want is not have just have some more digits, but as much as you can (like having one thousand digits, but I need more).

推荐答案

我在本网站:

<html>
  <head>
    <title>Pi</title>
    <script type="text/javascript">
      mess = "";
      Base = Math.pow(10, 11);
      cellSize = Math.floor(Math.log(Base) / Math.LN10);
      a = Number.MAX_VALUE;
      MaxDiv = Math.floor(Math.sqrt(a));
      function makeArray(n, aX, Integer) {
        var i = 0;
        for (i = 1; i < n; i++) aX[i] = null;
        aX[0] = Integer
      }
      function isEmpty(aX) {
        var empty = true
        for (i = 0; i < aX.length; i++) if (aX[i]) {
          empty = false;
          break
        }
        return empty
      }
      function Add(n, aX, aY) {
        carry = 0
        for (i = n - 1; i >= 0; i--) {
          aX[i] += Number(aY[i]) + Number(carry);
          if (aX[i] < Base) carry = 0;
          else {
            carry = 1;
            aX[i] = Number(aX[i]) - Number(Base)
          }
        }
      }
      function Sub(n, aX, aY) {
        for (i = n - 1; i >= 0; i--) {
          aX[i] -= aY[i];
          if (aX[i] < 0) {
            if (i > 0) {
              aX[i] += Base;
              aX[i - 1]--
            }
          }
        }
      }
      function Mul(n, aX, iMult) {
        carry = 0;
        for (i = n - 1; i >= 0; i--) {
          prod = (aX[i]) * iMult;
          prod += carry;
          if (prod >= Base) {
            carry = Math.floor(prod / Base);
            prod -= (carry * Base)
          } else carry = 0;
          aX[i] = prod
        }
      }
      function Div(n, aX, iDiv, aY) {
        carry = 0;
        for (i = 0; i < n; i++) {
          currVal = Number(aX[i]) + Number(carry * Base);
          theDiv = Math.floor(currVal / iDiv);
          carry = currVal - theDiv * iDiv;
          aY[i] = theDiv
        }
      }
      function arctan(iAng, n, aX) {
        iAng_squared = iAng * iAng;
        k = 3;
        sign = 0;
        makeArray(n, aX, 0);
        makeArray(n, aAngle, 1);
        Div(n, aAngle, iAng, aAngle);
        Add(n, aX, aAngle);
        while (!isEmpty(aAngle)) {
          Div(n, aAngle, iAng_squared, aAngle);
          Div(n, aAngle, k, aDivK);
          if (sign) Add(n, aX, aDivK);
          else Sub(n, aX, aDivK);
          k += 2;
          sign = 1 - sign
        }
        mess += "aArctan=" + aArctan + "<br>"
      }
      function calcPI(numDec) {
        var ans = "";
        t1 = new Date();
        numDec = Number(numDec) + 5;
        iAng = new Array(10);
        coeff = new Array(10);
        arrayLength = Math.ceil(1 + numDec / cellSize);
        aPI = new Array(arrayLength);
        aArctan = new Array(arrayLength);
        aAngle = new Array(arrayLength);
        aDivK = new Array(arrayLength);
        coeff[0] = 4;
        coeff[1] = -1;
        coeff[2] = 0;
        iAng[0] = 5;
        iAng[1] = 239;
        iAng[2] = 0;
        makeArray(arrayLength, aPI, 0);
        makeArray(arrayLength, aAngle, 0);
        makeArray(arrayLength, aDivK, 0);
        for (var i = 0; coeff[i] != 0; i++) {
          arctan(iAng[i], arrayLength, aArctan);
          Mul(arrayLength, aArctan, Math.abs(coeff[i]));
          if (coeff[i] > 0) Add(arrayLength, aPI, aArctan);
          else Sub(arrayLength, aPI, aArctan)
        }
        Mul(arrayLength, aPI, 4);
        sPI = "";
        tempPI = "";
        for (i = 0; i < aPI.length; i++) {
          aPI[i] = String(aPI[i]);
          if (aPI[i].length < cellSize && i != 0) {
            while (aPI[i].length < cellSize) aPI[i] = "0" + aPI[i]
          }
          tempPI += aPI[i]
        }
        for (i = 0; i <= numDec; i++) {
          if (i == 0) sPI += tempPI.charAt(i) + ".<br>";
          else {
            if (document.getElementById("cbCount").checked) addcount = " (" + (i) + ")";
            else addcount = "";
            if (document.getElementById("cbSpace").checked) thespace = " ";
            else thespace = "";
            if ((i) % 50 == 0 && i != 0) sPI += tempPI.charAt(i) + addcount + "<br>";
            else if (i % 5 == 0) sPI += tempPI.charAt(i) + thespace;
            else sPI += tempPI.charAt(i)
          }
        }
        ans += ("PI (" + numDec + ")=" + sPI + "<br>");
        ans += ("Win PI=<br>3.1415926535897932384626433832795<br>");
        t2 = new Date();
        timeTaken = (t2.getTime() - t1.getTime()) / 1000;
        ans += "It took: " + timeTaken + " seconds";
        var myDiv = document.getElementById("d1");
        myDiv.innerHTML = ans
      }
    </script>
  </head>
  <body>
    <h1>
      Pi Machin
    </h1>
    <form name="" id="" method="post" action="" enctype="text/plain" onsubmit="calcPI(this.t1.value);return false;">
      Number of Digits:<br>
      <input type="text" name="t1" id="t1" value="100" size="25" maxlength="25">
      <br>Add Count:
      <input type="checkbox" name="cbCount" id="cbCount" value="" checked="checked">
      <br>Add Spaces:
      <input type="checkbox" name="cbSpace" id="cbSpace" value="" checked="checked">
      <br>
      <input type="button" value="Calculate Pi" onclick="calcPI(this.form.t1.value)">
    </form>
    <div id="d1">0</div>
  </body>
</html>

这篇关于Javascript:PI(π)计算器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆