Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.databases.postgresql > #885

Re: calculating pi [Re: php and big data and predictive modelling...]

Newsgroups comp.databases.postgresql
Date 2019-12-29 04:15 -0800
References (5 earlier) <5e073d4a$0$10273$e4fe514c@news.xs4all.nl> <6b2f2a6b-bddf-4398-8ba8-4aa5b4ea6e46@googlegroups.com> <5e077cca$0$10261$e4fe514c@news.xs4all.nl> <a29d8b40-5288-4a95-b243-75e0d756a072@googlegroups.com> <5e086e8c$0$10258$e4fe514c@news.xs4all.nl>
Message-ID <3d701fa2-f2fb-4103-9f1c-9f00b7e6a952@googlegroups.com> (permalink)
Subject Re: calculating pi [Re: php and big data and predictive modelling...]
From robamman2019@gmail.com

Show all headers | View raw


On Sunday, December 29, 2019 at 11:15:01 AM UTC+2, Luuk wrote:
> On 28-12-2019 18:40, robamman2019@gmail.com wrote:
> > The BigNumber library is available at : https://github.com/MikeMcl/bignumber.js/ .
> > I modified this code a little bit.
> > For example with width 5000 square The Pi came out 3.1415....
> > It took about 1 minute.
> > 
> 
> Ok, my script loads within 1 second, and does not need an input, and pi 
> came out with 15 decimals  as: 3.141592653589793
> 
> 
> <html>
> <head>
>    <meta charset="utf-8">
>    <title>Calculating pi</title>
>    <script type="text/javascript">
>      function findnumber() {
> 	var pivalue=Math.atan(1)*4;
> 	document.getElementById("pi").innerHTML=pivalue;
>      }
>    </script>
> </head>
> <body onload="findnumber();">
>    <h1>Pi</h1>
>    <p id="pi"></p>
> </body>
> </html>
> 
> 
> for now, i will keep it with this simple approach, and YES, i kno that 
> Math.PI also exists, but than what calculation am i doing anywayz?.
> 
> -- 
> Luuk

Yes, it's a little bit more difficult. You need super fast computer for it to calculate. But I think somewhere are these kind of computers already available. But we don't have access for them. Or you have to optimize code. 
I tested it on 2600px width, it took 48 seconds. So to get precision 16 decimals it will take about a year on my computer.

I fixed a little bit of it's code.
index.html:

<html>
<head>
<meta charset="utf-8">
<title>Calculating pi</title>
<script src='bignumber/bignumber.js'></script>
<style>
#pivalue {
    border: 1px solid green;
	margin: 5px;
	height: 200px;
	width: 500px;
	overflow-x: auto;
	overflow-wrap: break-word;
}
</style>
</head>
<body>

<form onsubmit="findnumber(); return false;">
<h1>
<p>Pi(The Circle circumference divided with it's diameter)</p>
<p>Pi can be calculated with a circle inside a square. The Pi estimated value is the following: (Circle area)*4/(Square area).
</p>
<p>Please enter square width <input style="width: 200px; height: 40px; font-size: 30px;" type="text" id="squarew"></input></p>
<p><button type="submit" style="width: 200px; height: 40px; font-size: 30px;">Calculate pi</button></p>
<p id="pivalue"></p>
<p>For estonians: Soovitan kasutada translate.google.com-i kui inglise keelt ei mõika.</p>

</h1>

</form>
<script>
var incircle=new BigNumber(0);
var area=new BigNumber(0);
function pointisincircle(xx0,yy0,xx,yy,radius) {
	  var aaa1=xx0.minus(xx);
	  var aaa2=(yy0).minus(yy);
	  var isin=(aaa1.multipliedBy(aaa1).plus(aaa2.multipliedBy(aaa2))).isLessThanOrEqualTo(radius.multipliedBy(radius));
      return isin;
}
function findnumber() {
    incircle=new BigNumber(0);
    area=new BigNumber(0);
    
	var squarew=new BigNumber(document.getElementById("squarew").value);
	//document.getElementById("pivalue").innerHTML
	//alert(squarew.valueOf());
	area=squarew.multipliedBy(squarew);
    var xmax=new BigNumber(squarew);
    var ymax=new BigNumber(squarew);
	//alert(xmax.valueOf());
	var radius=new BigNumber(squarew.dividedBy(2));
    var x0=new BigNumber(squarew).dividedBy(2);
    var y0=new BigNumber(squarew).dividedBy(2);
	var x=new BigNumber(0);
	var y=new BigNumber(0);
	var run2=true;
	while(true) {
	      if(x.isEqualTo(xmax)) break;
		  while(run2) {
		        if(y.isEqualTo(ymax)) run2=false;
				if(run2) {
					y=y.plus(new BigNumber(1));
				         	
				} else {
					y=new BigNumber(0);
				}
				if(pointisincircle(x0,y0,x,y,radius)) {
					incircle=incircle.plus(1);
				}
		  }
		  run2=true;
		  x=x.plus(1);
	}
	var pivalue=incircle.multipliedBy(4).dividedBy(area);
	//alert("In circle: "+incircle+" Area: "+area+" Pi value: "+pivalue);
	document.getElementById("pivalue").innerHTML=pivalue;
}
</script>
</body>
</html>


---------------
Kristjan Robam
E-mail: robamman2019x @ aol.com
(Please remove x, when writing to me)

Back to comp.databases.postgresql | Previous | NextPrevious in thread | Find similar


Thread

Re: calculating pi [Re: php and big data and predictive modelling...] robamman2019@gmail.com - 2019-12-28 02:22 -0800
  Re: calculating pi [Re: php and big data and predictive modelling...] robamman2019@gmail.com - 2019-12-28 04:55 -0800
    Re: calculating pi [Re: php and big data and predictive modelling...] robamman2019@gmail.com - 2019-12-28 08:30 -0800
    Re: calculating pi [Re: php and big data and predictive modelling...] robamman2019@gmail.com - 2019-12-28 09:40 -0800
      Re: calculating pi [Re: php and big data and predictive modelling...] robamman2019@gmail.com - 2019-12-29 04:15 -0800

csiph-web