How to use JS to calculate pi to 100 decimal places
This time I will show you how to use JS to calculate pi to 100 digits after the decimal point, and how to use JS to calculate pi to 100 digits after the decimal point. What are the precautions? What are the practical cases below? 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:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> js计算圆周率</title> </head> <body> <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> </body> </html>
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:
##JS Detailed explanation of the steps to use the callback function
The above is the detailed content of How to use JS to calculate pi to 100 decimal places. For more information, please follow other related articles on the PHP Chinese website!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics

General Matrix Multiplication (GEMM) is a vital part of many applications and algorithms, and is also one of the important indicators for evaluating computer hardware performance. In-depth research and optimization of the implementation of GEMM can help us better understand high-performance computing and the relationship between software and hardware systems. In computer science, effective optimization of GEMM can increase computing speed and save resources, which is crucial to improving the overall performance of a computer system. An in-depth understanding of the working principle and optimization method of GEMM will help us better utilize the potential of modern computing hardware and provide more efficient solutions for various complex computing tasks. By optimizing the performance of GEMM

WORD is a powerful word processor. We can use word to edit various texts. In Excel tables, we have mastered the calculation methods of addition, subtraction and multipliers. So if we need to calculate the addition of numerical values in Word tables, How to subtract the multiplier? Can I only use a calculator to calculate it? The answer is of course no, WORD can also do it. Today I will teach you how to use formulas to calculate basic operations such as addition, subtraction, multiplication and division in tables in Word documents. Let's learn together. So, today let me demonstrate in detail how to calculate addition, subtraction, multiplication and division in a WORD document? Step 1: Open a WORD, click [Table] under [Insert] on the toolbar, and insert a table in the drop-down menu.

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest
