Precision C++
Can somone compile this C++ code (you'll probably have to edit the libraries i included for no reason like tapestry. But when i run this i only get 16 decimal places...like an artificial limit, is this just the most the processor can handle for some reason or is it just limits of c++/borland compiler?
//Pi
//3/14/03
//---------------------------------------------------------------------------
#pragma hdrstop
#include <tapestry>
#include <iostream.h>
#include <math.h>
#include <conio.h>
#include <condefs.h>
//------------------------------------------------------------------
#pragma argsused
int main(int argc, char* argv[])
{
int x;
double pi, odd;
odd=3;
pi=1;
getch();
while(true)
{
if(x==0)
{
pi=pi+(1/odd);
}
else
{
pi=pi-(1/odd);
}
cout<<setprecision(100)<<pi*4<<endl;
if(x==0)
{
x=1;
}
else
{
x=0;
}
odd=odd+2;
}
getch();
return 0;
}
//---------------------------------------------------------------------------
//Pi
//3/14/03
//---------------------------------------------------------------------------
#pragma hdrstop
#include <tapestry>
#include <iostream.h>
#include <math.h>
#include <conio.h>
#include <condefs.h>
//------------------------------------------------------------------
#pragma argsused
int main(int argc, char* argv[])
{
int x;
double pi, odd;
odd=3;
pi=1;
getch();
while(true)
{
if(x==0)
{
pi=pi+(1/odd);
}
else
{
pi=pi-(1/odd);
}
cout<<setprecision(100)<<pi*4<<endl;
if(x==0)
{
x=1;
}
else
{
x=0;
}
odd=odd+2;
}
getch();
return 0;
}
//---------------------------------------------------------------------------
Comments
Anyhow, here's a quick reference guide to the precision of various variable types:
short
A 2 byte integer (whole number values from -128 to 127).
int
A 4 byte integer (whole number values from -2,147,483,648 to 2,147,483,647).
long
4 or 8 byte integer (compiler dependent).
float
Single precision floating point number (compiler dependent, 7 digits or more).
double
Double precision floating point number (compiler dependent, 15 digits or more).
If you need *lots* more precision, I would suggest creating a class that uses an array to store digits and write some functions or overload the operators to do the math with it. I did this in an AP class long ago to create "bigint" and "bigfloat" variables. I'm sure there's some sample code or explanation around the web somewhere if you want to try implementing that.
i used double as u can see but yet it still only shows to 16 decimal places
Originally posted by Brad
Um, did you leave something out? Your code looks incomplete.
Anyhow, here's a quick reference guide to the precision of various variable types:
short
A 2 byte integer (whole number values from -128 to 127).
int
A 4 byte integer (whole number values from -2,147,483,648 to 2,147,483,647).
long
4 or 8 byte integer (compiler dependent).
float
Single precision floating point number (compiler dependent, 7 digits or more).
double
Double precision floating point number (compiler dependent, 15 digits or more).
If you need *lots* more precision, I would suggest creating a class that uses an array to store digits and write some functions or overload the operators to do the math with it. I did this in an AP class long ago to create "bigint" and "bigfloat" variables. I'm sure there's some sample code or explanation around the web somewhere if you want to try implementing that.
thanks i'll check it out...i guess it really doesn't matter though, cuz the way i was figuring for pie would take a year probably to get to the 16 decimal places haha
i modified it a little and put it into a calculator and it didn't even get to the second decimal places haha