


Detailed explanation of the implementation steps for calculating pi to 100 decimal places using JS
This time I will bring you a detailed explanation of the implementation steps for calculating pi to 100 digits after the decimal point with JS. What are the precautions for calculating pi to 100 digits after the decimal point with JS. Here is a practical case. Let’s take a look. .
The significant digits of floating point numbers are 16. I made a large number class myself, which can store 100 significant digits and implement the basic operations of the large number class. I used it to calculate pi (circle cut method, that is, polygonal approximation), and got one hundred significant figures after the decimal point. I compared the calculation results with Machin's formula, and there was no error. It takes about 2 seconds.
The complete example is as follows:
nbsp;html> <meta> <title>js计算圆周率</title> <script> <!-- function BigNum(str, n, b) { /* BigNum -- 大数类 私有成员: data -- 119 位数字,放在长度为 17 的数组里,每个数组元素存放 7 位数字。 decimal_place -- 小数点的位置,从最左位开始算。 positive -- 是否是正数。 recalc() -- 为了尽可能存放最多的有效数位,去除前缀的 0,并重新计算小数点位置。 init() -- 部分初始化工作。 公有成员: BigNum( String str, INT n, BOOL b) -- 构造函数。参数:str -- 字符串,各个有效数位;n -- 整数,小数点位置,从最左位开始算,比如 BigNum("123", 2) = 12.3; BigNum("123", 0) = 0.123; BigNum("123", -2) = 0.00123;b -- 布尔值,是否是正数。 Add( BigNum num ) -- 加法。 Subtract( BigNum num ) -- 减法。 Multiply( BigNum num ) -- 乘法。 pide( BigNum num ) -- 除法。 SquareRoot() -- 平方根。 toString() -- 转换为字符串(包括小数点),以便以文本形式输出计算结果。 Clone() -- 复制。 */ this.recalc = function() /* 去除前缀的 0,并重新计算小数点位置 */ { for(var i = 0; i < 17; i ++) { if(this.data[0] != 0) break; this.data.shift(); this.data.push(0); this.decimal_place --; } } this.init = function() /* 部分初始化工作 */ { this.decimal_place = Math.ceil( n / 7 ); //小数点位置 this.data = new Array(17); //保存有效数位的数组 if(n % 7 > 0) { var arr = new Array( 8 - n % 7 ); } else { var arr = new Array( 1 - n % 7 ); } str = arr.join("0") + str; if(str.length > 119) { str = str.substr(0, 119); } else if(str.length < 119) { var arr = new Array(120 - str.length); str += arr.join("0"); } for( var i = 0; i < 17; i ++ ) { this.data[i] = parseInt( str.substr(i * 7, 7) , 10 ); } } /* 初始化开始 */ this.positive = b; if( ! /^0*$/.test(str) ) { this.init(); this.recalc(); } else { this.data = new Array(17); for( var i = 0; i < 17; i ++ ) { this.data[i] = 0; } this.decimal_place = 0; } /* 初始化结束 */ this.Add = function(num) /* 加法 */ { if(this.positive && !num.positive) { num.positive = true; var result = this.Subtract(num); num.positive = false; return result; } else if(num.positive && !this.positive) { this.positive = true; var result = num.Subtract(this); this.positive = false; return result; } var result = new BigNum("", 0, this.positive); var num1,num2; if(this.decimal_place >= num.decimal_place) { num1 = this; num2 = num; } else { num1 = num; num2 = this; } result.decimal_place = num1.decimal_place; if(num1.decimal_place - num2.decimal_place >= 17) { for(var i = 0; i < 17; i ++) { result.data[i] = num1.data[i]; } return result; } var nOffDec = num1.decimal_place - num2.decimal_place; var nTmp = 0; for( var i = 16; i >= 0; i -- ) { var nTmp1 = i - nOffDec; var nTmp2 = 0; if(nTmp1 >= 0) { nTmp2 = num1.data[i] + num2.data[nTmp1]; } else { nTmp2 = num1.data[i]; } nTmp2 += nTmp; nTmp = Math.floor(nTmp2 / 10000000); result.data[i] = nTmp2 % 10000000; } if(nTmp > 0) { result.data.unshift(nTmp); result.decimal_place ++; } return result; } this.Subtract = function(num) /* 减法 */ { if(this.positive && !num.positive) { num.positive = true; var result = this.Add(num); num.positive = false; return result; } else if(!this.positive && num.positive) { this.positive = true; var result = this.Add(num); result.positive = false; this.positive = false; return result; } else { var num1 = num2 = null; var bPositive; if(this.decimal_place > num.decimal_place) { num1 = this; num2 = num; bPositive = this.positive; } else if(this.decimal_place < num.decimal_place) { num1 = num; num2 = this; bPositive = !this.positive; } else { for( var i = 0; i < 17; i ++ ) { if(this.data[i] > num.data[i]) { num1 = this; num2 = num; bPositive = this.positive; break; } else if(this.data[i] < num.data[i]) { num1 = num; num2 = this; bPositive = !this.positive; break; } } } if( num1 == null) { return new BigNum("", 0, true); } else { if(num1.decimal_place - num2.decimal_place >= 17) { var result = new BigNum("", 0, bPositive); for(var i = 0; i < 17; i ++) { result.data[i] = num1.data[i]; } result.decimal_place = num1.decimal_place; return result; } var result = new BigNum("", 0, bPositive); result.decimal_place = num1.decimal_place; var nOffDec = num1.decimal_place - num2.decimal_place; var nTmp = 0; for( var i = 16; i >= 0; i -- ) { var nTmp1 = i - nOffDec; var nTmp2 = 0; if(nTmp1 >= 0) { nTmp2 = 10000000 + nTmp + num1.data[i] - num2.data[nTmp1]; } else { nTmp2 = 10000000 + nTmp + num1.data[i]; } if(nTmp2 >= 10000000) { result.data[i] = nTmp2 - 10000000; nTmp = 0; } else { result.data[i] = nTmp2; nTmp = -1; } } result.recalc(); return result; } } } this.Multiply = function(num) /* 乘法 */ { var bPositive; var nDecimalPlace = this.decimal_place + num.decimal_place - 1; if(this.positive == num.positive) { bPositive = true; } else { bPositive = false; } var result = new BigNum("", 0, bPositive); var nTmpData = 0; for( var i = 16; i >= 0; i -- ) { for( var j = 16; j >= 0; j -- ) { if(isNaN(result.data[j + i])) result.data[j + i] = 0; result.data[j + i] += this.data[j] * num.data[i]; if(result.data[j + i] >= 10000000) { if( j + i -1 >= 0 ) { result.data[j + i -1] += Math.floor(result.data[j + i] / 10000000); } else { nTmpData += Math.floor(result.data[j + i] / 10000000); } result.data[j + i] = result.data[j + i] % 10000000; } } } if(nTmpData > 0) { result.data.unshift(nTmpData); result.data.pop(); nDecimalPlace ++; } result.decimal_place += nDecimalPlace; return result; } this.pide = function(num) /* 除法 */ { var bPositive; var nDecimalPlace = this.decimal_place - num.decimal_place + 1; if(this.positive == num.positive) { bPositive = true; } else { bPositive = false; } var result = new BigNum("", 0, bPositive); var arrTemp = new Array(17); for( var i = 0; i < 17; i ++ ) { arrTemp[i] = this.data[i]; } var bTest = true; var nTest = 0; for( var i = 0; i < 17; i ++ ) { if(bTest) { nTest = Math.floor( ( arrTemp[0] * 10000000 + arrTemp[1] ) / ( num.data[0] * 10000000 + num.data[1] ) ); } else { bTest = true; } if(nTest == 0) { result.data[i] = 0; arrTemp[1] += arrTemp[0] * 10000000; arrTemp.shift(); arrTemp.push(0); continue; } var arrTemp1 = new Array(17); for( var j = 0; j < 17; j ++ ) { arrTemp1[j] = 0; } for( var j = 16; j >= 0; j -- ) { arrTemp1[j] += nTest * num.data[j]; if(arrTemp1[j] >= 10000000) { if(j != 0) { arrTemp1[j - 1] += Math.floor( arrTemp1[j] / 10000000); arrTemp1[j] = arrTemp1[j] % 10000000; } } } for( var j = 0; j < 17; j ++ ) { if(arrTemp[j] < arrTemp1[j]) { bTest = false; break; } else if(arrTemp[j] > arrTemp1[j]) { break; } } if(bTest) { result.data[i] = nTest; for( var j = 16; j >= 0; j -- ) { if(arrTemp[j] >= arrTemp1[j]) { arrTemp[j] -= arrTemp1[j]; } else { arrTemp[j] = 10000000 + arrTemp[j] - arrTemp1[j]; arrTemp[j - 1] --; } } } else { nTest --; i --; continue; } arrTemp[1] += arrTemp[0] * 10000000; arrTemp.shift(); arrTemp.push(0); } result.decimal_place = nDecimalPlace; result.recalc(); return result; } this.SquareRoot = function() /* 平方根 */ { var result = new BigNum("", 0, true); nDecimalPlace = Math.ceil(this.decimal_place / 2); var arrTemp = new Array(17); for(var i = 0; i < 17; i ++) { arrTemp[i] = this.data[i]; } var bTest = true; for(var i = 0; i < 17; i ++) { if( i == 0 ) { if(this.decimal_place % 2 == 0) { var nTemp = arrTemp[0] * 10000000 + arrTemp[1]; var nTemp1 = Math.floor( Math.sqrt( nTemp ) ); var nTemp2 = nTemp - nTemp1 * nTemp1; arrTemp[0] = 0; arrTemp[1] = nTemp2; arrTemp.shift(); arrTemp.push(0); } else { var nTemp1 = Math.floor( Math.sqrt( arrTemp[0] ) ); var nTemp2 = arrTemp[0] - nTemp1 * nTemp1; arrTemp[0] = nTemp2; } } else { if(bTest) { if( i == 1 ) { nTemp1 = Math.sqrt( (arrTemp[0] * 10000000 + arrTemp[1]) + 100000000000000 * Math.pow(result.data[0], 2) ) - 10000000 * result.data[0]; nTemp1 = Math.floor(nTemp1); } else { nTemp = result.data[0] * 10000000 + result.data[1]; nTemp1 = Math.floor( ( arrTemp[0] * 10000000 + arrTemp[1] ) / ( 2 * nTemp ) ); } } else { bTest = true; } var arrTemp1 = new Array(17); var nTemp3 = 0 for( var j = i - 1; j >= 0; j -- ) { arrTemp1[j] = result.data[j] * 2 + nTemp3; if( arrTemp1[j] >= 10000000 && j > 0 ) { nTemp3 = 1; arrTemp1[j] = arrTemp1[j] % 10000000; } else { nTemp3 = 0; } } arrTemp1[i] = nTemp1; nTemp3 = 0; for( var j = i; j >= 0; j -- ) { arrTemp1[j] = arrTemp1[j] * nTemp1 + nTemp3; if( arrTemp1[j] >= 10000000 && j > 0 ) { nTemp3 = Math.floor( arrTemp1[j] / 10000000 ); arrTemp1[j] = arrTemp1[j] % 10000000; } else { nTemp3 = 0; } } var arrTemp2 = new Array(17); for( var j = 0; j < 17; j ++ ) { arrTemp2[j] = arrTemp[j]; } for( var j = i; j >= 0; j -- ) { if( arrTemp2[j] >= arrTemp1[j] ) { arrTemp2[j] -= arrTemp1[j]; } else { if(j > 0) { arrTemp2[j] = arrTemp2[j] + 10000000 - arrTemp1[j]; arrTemp2[j - 1] --; } else { bTest = false; break; } } } if(bTest) { arrTemp = arrTemp2; } else { nTemp1 --; i --; continue; } } result.data[i] = nTemp1; arrTemp[1] += arrTemp[0] * 10000000; arrTemp.shift(); arrTemp.push(0); } result.decimal_place = nDecimalPlace; result.recalc(); return result; } this.toString = function() /* 转换为字符串(包括小数点),以便以文本形式输出计算结果 */ { var szData = ""; var szOutPut = ""; this.recalc(); for( var i = 0; i < 17; i ++ ) { var szTmpData = this.data[i].toString() var arr = new Array(8 - szTmpData.length); szData += arr.join("0") + szTmpData; } if( /^0*$/.test(szData) ) { return "0"; } var n = this.decimal_place * 7; for(var i = 0; i < 7; i++) { if(szData.substr(i, 1) != 0) break; n --; } szData = szData.replace(/^0+/g,""); szData = szData.substr(0, 101); szData = szData.replace(/0+$/g,""); if( n < 1 ) { szOutPut = szData.substr(0, 1) + ( ( szData.length > 1 ) ? "." : "" ) + szData.substr(1) + "e" + ( n - 1 ).toString(); } else if(n == szData.length) { szOutPut = szData; } else if(n > szData.length) { szOutPut = szData.substr(0, 1) + "." + szData.substr(1) + "e+" + (n - 1).toString(); } else { szOutPut = szData.substr(0, n) + "." + szData.substr(n); } return ( this.positive ? "" : "-" ) + szOutPut; } this.Clone = function() /* 复制 */ { var result = new BigNum("", 0, true); for( var i = 0; i < 17; i ++) { result.data[i] = this.data[i]; } result.decimal_place = this.decimal_place; result.positive = this.positive; return result; } } var a = new BigNum("1", 1, true) var count = 168; var nTwo = new BigNum("2", 1, true); function loop(intTmpvar,intCount) { if(intCount == 0) return intTmpvar; var v1 = intTmpvar.pide( nTwo ); var v11 = v1.Clone(); var nTemp = v1.Multiply( v11 ); var a1 = a.Clone(); a1 = a.Multiply(a1); var nTemp1 = a1.Subtract( nTemp ) v2 = nTemp1.SquareRoot(); v3 = a.Subtract( v2 ); var v31 = v3.Clone(); var nTemp2 = v3.Multiply( v31 ); var nTemp3 = nTemp2.Add(nTemp); v4 = nTemp3.SquareRoot(); return loop( v4 , -- intCount ) } var a1 = a.Clone(); var nTemp = a.Multiply(a1); var nTemp1 = nTemp.Clone(); nTemp = nTemp.Add(nTemp1); nTemp = loop(nTemp.SquareRoot(), count); var nFour = new BigNum("4", 1, true); nTemp = nTemp.Multiply( nFour ); nTemp1 = new BigNum("2", 1, true); var nTemp2 = new BigNum("2", 1, true); for(var i = 0; i < count - 1; i ++) { nTemp1 = nTemp1.Multiply( nTemp2 ); } nTemp = nTemp.Multiply( nTemp1 ); nTemp = nTemp.pide( nTwo ); nTemp = nTemp.pide( a ); document.write( nTemp ) //--> </script>
Running result:
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421 170679
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to php Other related articles on the Chinese website!
Recommended reading:
Detailed explanation of the steps for JS to call the local camera function
JS accumulation, iteration, exhaustion, recursion, etc. Summary of commonly used algorithms
The above is the detailed content of Detailed explanation of the implementation steps for calculating pi to 100 decimal places using JS. For more information, please follow other related articles on the PHP Chinese website!

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools