欢迎你的访问
 
返回天空素材库首页 http://www.Skysucai.com  
收藏本站
将本站设为首页
您当前的位置:天空素材首页 -> 计算圆周率
 
栏 目 导 航
. 常用代码 . 综合特效
. 状态特效 . 游戏特效
. 页面背景 . 页面特效
. 页面导航 . 文本操作
. 文本特效 . 图形特效
. 鼠标特效 . 时间日期
. 密码特效 . 浏览相关
. 警告对话 . 技巧特效
. 计数转换 . 测试搜索
. 代码生成 . 播放音乐
. 按钮特效 . 系统相关
. 链接特效 . 黑客性质
. 相关特效 . 窗口特效
. 广告特效 . 表单表格
. 模拟例子
相 关 文 章
本 类 热 门
· 面积计算器
· 100只鸡
· 不错的助手“默林
· 数制转换 可自定2、8...
· 科学计算器
· 坐标图
· 职位
· 三角函数计算
· 装机计算器
· NUMBER对象的增强函数...
· 阿拉伯数字转换为英文...
· 计算圆周率
更多  
 
 
更多  
最 新 推 荐
· 状态栏时间跳动特效
· 农历挂历特效
· 真正的万年历
· 网页特效 菜单-会动的...
· 网页特效 时间-任意位...
· 个人网页特效-有前后日...
· 时间每过一秒,Radio就...
· 时钟显示在任意指定位...
· 带开关的Form时钟
· 记录几天后的日期
· 显示登陆时间
· 退出时显示访问时间
更多  
     
计算圆周率
计算圆周率
作者:无  来源:本站整理  名称:计算圆周率 

减小字体 增大字体

       

[提示:你可先按照下面代码提示修改 上面部分代码,再按运行,此处看到的演示不完全正确]

此特效的详细完整代码如下:
]  源码如下
----------------------------------------------------------
<HTML>
<HEAD>
<META NAME="keywords" CONTENT="Pi, machin, javascipt, numerical methods, multiple precision, series">
<TITLE>PI in JavaScript-www.51windows.Net</TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
//
// arctangent series for Pi a la mode by Bohr
//
var vectorsize = 53; // number of elements in each of four arrays
var nCells = 52; // number of array elements displayed

// constant
var BASE = 10000.0; // The number base
var TENTHOUSANDTH = 0.0001; // avoids floating point division
var SQ_239E4 = 571210000.0; // = BASE x 239 squared
var SQ_5E4 = 250000.0; // = BASE x 5 squared
var REPAIR = 0.000005; // roundoff correction
var RECIPR_25 = 0.04; // the reciprocal of 25 is 0.04
var RECIPR_239= 1.0 / 57121; // reciprocal of 239 squared

// arrays
var term5 = null; // = (1/5)^(2n+1), integer array
var term239 = null; // = (1/239)^(2n+1), integer array
var sum = null; // = 16term5-4term239, integer array
var series = null; // = PI

// floating or integer
var TwoNplus1 = 1.0; // 2n+1 = 1,3,5,7,...
var Basex2n_1 = BASE; // = 10000*(2n+1)
var Currdigit = 1; // index of the series[] element being printed
var sgn = 1; // = (-1)^n; takes the values -1 and 1

// strings
var IntroString = " Pi = 3.";

function MakeArray(n) {
this.length = n;
for (var i = 1; i <= n; i++) {
this[i] = 0;
}
return this;
}
function PiSetup() {
term5 = new MakeArray(vectorsize);
term239 = new MakeArray(vectorsize);
sum = new MakeArray(vectorsize);
series = new MakeArray(vectorsize);

term5[1] = 5;
term239[1] = 239;
}
function DivideTerms() {
var total5 = term5[1];
var total239 = term239[1];

// Divide the terms by 25 or 57121
for (var i = Currdigit; i <= nCells + 2; i++) {
term5[i] = Math.floor( RECIPR_25 * total5 + REPAIR );
total5 = BASE * total5 - SQ_5E4 * term5[i] + term5[i+1];
term239[i] = Math.floor( total239 * RECIPR_239 + REPAIR );
total239 = BASE * total239 - SQ_239E4 * term239[i] + term239[i+1];
}
}
function SubtractTerms() {
var carry = 0;
var total = 0;

for (var i = nCells + 1; i > Currdigit; i--) {
total = 16.0 * term5[i] - 4.0 * term239[i] + carry + 60000.0;
carry = Math.floor( total * TENTHOUSANDTH + REPAIR ) - 6.0;
sum[i] = Math.floor( total - BASE * carry - 60000.0 );
}
}
function DivideSum() {
var total = sum[1];
var reciprocal = 1.0 / TwoNplus1;

for( var i = Currdigit; i <= nCells + 2; i++) {
sum[i] = Math.floor( total * reciprocal + REPAIR );
total = BASE * total - Basex2n_1 * sum[i] + sum[i+1];
}
}
function AddShowDigits(nchars,lwidth) {
var i = 0;
var total = 0;
var carry = 0;
var ccount = nchars / 4;
var strg = "x";
var old1 = series[Currdigit+1];
var old2 = series[Currdigit+2];

// Add the sum to the series
for (i = nCells + 1; i >= Currdigit; i--) {
total = series[i] + sgn * sum[i] + carry + 30000.0;
carry = Math.floor( total * TENTHOUSANDTH + REPAIR ) - 3.0;
series[i] = Math.floor( total - BASE * carry - 30000.0 );
}

// if stable, display digits
if((old1==series[Currdigit+1]) && (old2==series[Currdigit+2])) {
Currdigit++;
ccount++;
if (ccount > lwidth / 4) {
document.write("<BR>")
ccount = 1;
}
strg = (BASE + series[Currdigit]) + " ";
document.write(strg.substring(1,5))
}

return (4 * ccount);
}

function Crunch(fontname,fontsize) {
var linelength = IntroString.length;
var time0 = new Date();
var linewidth = 4 * Math.floor(50.0 / fontsize)

window.status = ".. calculating "+(4 * nCells)+" digits, please wait .."
document.write('<UL><FONT SIZE=' + fontsize + ' FACE="' + fontname + '">')
document.write(IntroString)

while (Currdigit < nCells) {
DivideTerms()
SubtractTerms()
DivideSum()
linelength = AddShowDigits(linelength, linewidth)

// Set up the next value of n
TwoNplus1 += 2.0;
Basex2n_1 = BASE * TwoNplus1;
sgn = 0 - sgn;
}

var time1 = new Date();
var elapsed = time1.getTime() - time0.getTime();
document.write("</FONT>")
document.write("<BR><BR><B>" + (4 * nCells) + " digits calculated in ")
document.write((Math.floor(elapsed)/1000.0) + " seconds.</B>")
document.write("</UL>")
}

PiSetup()

// -->
</SCRIPT>
</HEAD>

<BODY TEXT="#000000" LINK="#0000EE" VLINK="#551A8B" ALINK="#FF0000" BACKGROUND="litepie3.jpg">
<br>

<CENTER>
<B><FONT SIZE=+1>Machin's Arctangent Series for Pi in JavaScript</FONT></B>
&nbsp;<A HREF="../mathindex.html">
<IMG SRC=http://www.haosc.com/wytx/114/"p_btn3.jpg" BORDER="0" HSPACE=14 HEIGHT=40 WIDTH=110 ALIGN=ABSCENTER></A>
</CENTER>
<hr>

<SCRIPT LANGUAGE="JavaScript">
<!--
Crunch("Courier New", 4)
// -->
</SCRIPT>

<hr>
<CENTER>Click "View: Page Source" to see the Javascript source code</CENTER>
<P><B>In 1706 John Machin</B> discovered the trigonometric identity</P>

<PRE> PI = 16 arctan(1/5) - 4 arctan(1/239)</PRE>

and used it to calculate the first one hundred digits of Pi. &nbsp;&nbsp;He combined
his formula with the Taylor series expansion for the inverse tangent of
x. &nbsp;(Brook Taylor was Machin's contemporary in Cambridge.) &nbsp;&nbsp;The arctangent
series is

<PRE> 3 5 7 n 2n+1
arctan(x) = x - x /3 + x /5 - x /7 +...+ (-1) x /(2n+1) +..</PRE>

Machin's formula remained the primary tool of Pi-hunters for centuries. &nbsp;&nbsp;As
late as 1973, Guilloud and Bouyer used a variation of it to compute one
million digits of Pi on a CDC 7600.

<P>This program uses arrays of multiple precision digits for the two series
terms, for their sum, and for the series. &nbsp;&nbsp;Each 'digit' is a modulo 10000
integer. &nbsp;&nbsp;The number base is 10,000.
</P>

<P>In the end, John Machin was never cured.
</P>

<HR SIZE=3 WIDTH="100%">
<B><FONT SIZE=-1>AUTHOR:</FONT></B>&nbsp;<TT> John Bohr </TT>
<A HREF="mailto:jnbohr@netscape.net">email me</A>
<BR><TT>Last updated: 1 October 1998</TT>
<BR>
</BODY>
</HTML>
[返回上一页] [打印计算圆周率]
上一篇特效:真正的右键屏蔽
下一篇特效:100只鸡
     
| 关于本站 | 服务条款 | 友情连接 | 网站地图 | 联系方式 | 广告联系 |