The Daily Click ::. Forums ::. Non-Klik Coding Help ::. PI Calculator Counts up to 10 digits! (C++)
 

Post Reply  Post Oekaki 
 

Posted By Message

Cazra

Crazy?

Registered
  24/07/2002
Points
  4472

Game of the Week WinnerVIP Member
19th April, 2004 at 17:12:50 -


#include<iostream.h>
#include<limits.h>
#include<iomanip.h>

void toggleflag(bool &x);

//This program is based on the following algorithm to find PI:
//Pi = 4A
//A= 1 - 1/3 + 1/5 - 1/7 + 1/9....

int main()
{
cout << "Snerlin's Pi calculator" << endl << "t\t\by Stephen Lindberg(Snerlin)";
cout << "Press enter to start." <<endl;
cin.get();
double pi=1.000000000000000;
double add=3;
bool flag=false;
int counter=0;
while(add<INT_MAX-3)
{
if(counter%1000000==0)
{
cout << setprecision(15);
cout << 4*pi << endl;
}
if(!flag)
{
pi-=1/add;
}
if(flag)
{
pi+=1/add;
}
toggleflag(flag);
add+=2;
++counter;
}
cout << "\n\nerror: You know how computers can only\n store a number up to a certain maximum number?\nThe adder in the algorithm reached the limit and went \n.all the way down to the lowest possible negative number.\n";
cout << "goodbye and have a nice day! ;P";
cin.get();
return 0;
}

void toggleflag(bool &x)
{
int a=0;
if(!x)
{
x=true;
a=1;
}
if((x)&&(a==0))
{
x=false;
}
}




Anyone know how to tirck the computer into handling numbers beyond the highest limit?

Image Edited by the Author.

 
n/a

Kris

Possibly Insane

Registered
  17/05/2002
Points
  2017
19th April, 2004 at 17:34:03 -

you can't trick computers. you could try using a string for your output, that could have any level of accuracy, but you couldn't use it with maths operations so it'd be pointless.

Before:

void toggleflag(bool &x)
{
int a=0;
if(!x)
{
x=true;
a=1;
}
if((x)&&(a==0))
{
x=false;
}
}



After:

flag = 1 - flag;

Image Edited by the Author.

 
"Say you're hanging from a huge cliff at the top of mt. everest and a guy comes along and says he'll save you, and proceeds to throw religious pamphlets at you while simultaniously giving a sermon." - Dustin G

Cazra

Crazy?

Registered
  24/07/2002
Points
  4472

Game of the Week WinnerVIP Member
19th April, 2004 at 18:12:37 -

nice toggling trick there

 
n/a

ShadowCaster

Possibly Insane

Registered
  02/01/2002
Points
  2203
19th April, 2004 at 18:37:18 -

SON: You dont include the .h when including from the standard library in C++, it's depricated, and can only be used for backward compatibility.

You should be able to trick the computer to using more digits if you create a pointer and define the amount of space using malloc (or realloc if you wish to redefine the amount of space at runtime). Just make sure whenever you malloc to cast it to the correct type otherwise you might get a few problems ^__^

Mike

 
"Now I guess we're... 'Path-E-Tech Management'" -Dilbert

Cazra

Crazy?

Registered
  24/07/2002
Points
  4472

Game of the Week WinnerVIP Member
19th April, 2004 at 20:52:20 -

umm... I read about malloc, but I don't really get what it does.

 
n/a

Batchman



Registered
  08/08/2003
Points
  231
20th April, 2004 at 10:36:17 -

python :


precision=input("Select Precision : ")
max,i,pi,result=pow(10,(precision)),1,0,1
while result>0:
result=max/i
if ((i-1)/2)&1:
pi-=result
else:
pi+=result
i=i+2
if (i==10000003):
print "press ^C if it last too long"
print "pi value : ",(4*pi)




python support a type called "long" which is defined to be a integer with no limit,but i don't understand why the last digit are always false

yes, it is slower than C/C++ or any compiled language



Image Edited more than twice by the Author.

Image Edited by the Drunk Author.

Image Edited by the Author.

 
n/a

Tigerworks

Klik Legend

Registered
  15/01/2002
Points
  3882
20th April, 2004 at 12:31:11 -

You might be able to find a C++ class somewhere which has very fine precision decimals, in the same way you can find 128 bit integer classes and stuff (__int64 is also supported usually, its an integer which can hold loads more than an int, but is emulated and comparitively slow, doubles are also 64 bit).
Another flag toggler: flag = !flag;

 
- Tigerworks

ChrisB

Crazy?

Registered
  16/08/2002
Points
  5457
20th April, 2004 at 14:21:29 -

A better pi calculator: Calc.exe

 
n/a

Kramy



Registered
  08/06/2002
Points
  1888
22nd April, 2004 at 17:35:25 -

Unoptimized Jamascript conversion of PI Calculator. Requries SLINT. Rather slow due to using Slint(strings).

The code has a bunch of crap in it too, since I had it return a percentage originally, but didn't change all the comments or lines of code.

Generates window, edit box, etc. for viewing PI. Updates PI every 1 second to save on window refresh time.

myPiCalculator = New PiCalc(digits); // too many digits(like 128+) will REALLY slow it down. Try it on 16 or 32.


// --------------------------------------------------- //
// --- PI Calculator by Kramy --- //
// --------------------------------------------------- //
// --- #Pi Calculator Requires Slint v1.3 or newer --- //
// --------------------------------------------------- //
// PiCalc uses the following Formula to find PI: //
// // 1 -1/3 +1/5 -1/7 +1/9... //
// --------------------------------------------------- //
// When making a new Pi Calculator, specify the number //
// of digits to store. //
// --------------------------------------------------- //
// To get any level of accuracy you must call //
// myPiCalculate.CalculatePI atleast Digits(cubed) //
// --------------------------------------------------- //
// The n is used to update the percentage of the //
// calculations completed. //
// --------------------------------------------------- //

// Make window for Result
myWindow = New Window(320,240,0,0,0xFFFFFF,8,"Kramy's Pi Calculator");
myWindow.SetAutoRefresh(FALSE);
myWindow.CENTER();
// Edit for showing result
myEdit = New Edit(myWindow,"",4,4,212,232,Edit.VSCROLL,Edit.MULTILINE);

// Make Pi Calculator (Digits)
myPiCalculator = New PiCalc(16);
Var Steps = 0;
Var PrevSteps = 0;

While(1)
{
// Raise Steps
Steps++;
// Calculate
myPiCalculator.CalculatePi();
If(Steps >= PrevSteps + 100)
{
// Update Steps
PrevSteps += 100;

// Get Result
Var myResult = myPiCalculator.Result(Steps);
// Add Decimal
myResult = myResult.SubString(0,1)+"."+myResult.SubString(1,myResult.Length()-1);
// Set Editbox to result for easier reading.
myEdit.SetText(myResult);

// Refresh Window
myWindow.Refresh();
}
}


Function PiCalc(pDigits)
{
// Precision
This.Digits = pDigits;
This.Divisor = 1;
This.isNeg = FALSE;

// Slint for End Result*4
This.myPI = New Slint("1");

// Slints for End Result
This.myCalc2 = New Slint("1");
For(PiCalcLoop = This.Digits;PiCalcLoop > 0;PiCalcLoop--) This.myCalc2.Mul("10");

// Slint for Calculations
This.myCalc = New Slint(myCalc2.Get());

// String for resetting
This.myPrecisionStr = myCalc2.Get();

// Calculate
This.CalculatePi = PiCalc_Calculate;
This.Result = PiCalc_Result;
}

Function PiCalc_Calculate()
{
// Increment Divisor
Divisor += 2;
// Switch from Positive to Negative
If(IsNeg) IsNeg = FALSE;
Else IsNeg = TRUE;

// -1/3 +1/5 -1/7 +1/9...
myCalc.Set(myPrecisionStr);
myCalc.Div(Divisor);

If(IsNeg) myCalc2.Sub(myCalc.Get());
Else myCalc2.ADD(myCalc.Get());
}

Function PiCalc_Result(pSteps)
{
myWindow.SetText("Digits: "+Digits+" Calculations: "+pSteps);
// Make Final Calculation
myPI.Set(myCalc2.Get());
myPi.Mul("4");
// Return to wherever
Return myPi.Get();
}



 
Kramy

Cazra

Crazy?

Registered
  24/07/2002
Points
  4472

Game of the Week WinnerVIP Member
22nd April, 2004 at 18:58:24 -

pretty jumbled up there. You could use tabs to make it more readable.

 
n/a

renneF



Registered
  02/08/2003
Points
  672
22nd April, 2004 at 20:31:54 -

What is all this crazy mumbo-jumbo? :S

 
Image

Cazra

Crazy?

Registered
  24/07/2002
Points
  4472

Game of the Week WinnerVIP Member
22nd April, 2004 at 20:56:21 -

C++ and Jamascript

 
n/a

Cazra

Crazy?

Registered
  24/07/2002
Points
  4472

Game of the Week WinnerVIP Member
23rd April, 2004 at 06:38:23 -

ya, but it's not really that simple:
for each fraction it calculates into the formula, it gets closer to finding the next digit of Pi.

4(1 - 1/3 + 1/5 - 1/7 + 1/9...)

 
n/a
   

Post Reply



 



Advertisement

Worth A Click