//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;
}
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.
"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
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
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
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;
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;
// 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);
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();
}