how to calculate value of pi

T

Thomas

Hi, I'm pretty new to the programming world. I got stuck in the
following problem. Please guide me about it.

Well I'm trying to get the value of
(128^100)^2
I've used long double type but to no avail.
I even can't get the value of
2^128.
How this problem can be solved?
Similarly, I was trying to develoop a program in hich we can calculate
the value of PI to our desired decimal point, but i got stuck in it.
How these sort of problems can be solved in C?
What tools C provides us to calculate such values?
 
J

Joe Wright

Thomas said:
Hi, I'm pretty new to the programming world. I got stuck in the
following problem. Please guide me about it.

Well I'm trying to get the value of
(128^100)^2
I've used long double type but to no avail.
I even can't get the value of
2^128.
How this problem can be solved?
Similarly, I was trying to develoop a program in hich we can calculate
the value of PI to our desired decimal point, but i got stuck in it.
How these sort of problems can be solved in C?
What tools C provides us to calculate such values?
The C language doesn't provide that stuff directly. But do check out
www.snippets.org for a whole bunch of interesting stuff.
 
O

osmium

Thomas said:
Hi, I'm pretty new to the programming world. I got stuck in the
following problem. Please guide me about it.

Well I'm trying to get the value of
(128^100)^2
I've used long double type but to no avail.

That's a pretty big number, isn't it? Use a scientific calculator and logs
to estimate the value. Then look at the range of numbers that long double
handles. You can write a little program and the values in <limits.he> to
find out what the range is in your particular machine.


I even can't get the value of
2^128.
How this problem can be solved?

Post the code you tried.
Similarly, I was trying to develoop a program in hich we can calculate
the value of PI to our desired decimal point, but i got stuck in it.
How these sort of problems can be solved in C?
What tools C provides us to calculate such values?

C provides the normal arithmetic operators add, subtract, multiply and
divide enhanced by the functions in <math.h> Plus a few dribs and drabs of
other things.

There are many ways to compute pi, some of them spectacularly poor. I am
sure you can find enough clues on the Web to keep you occupied for days and
days. Essentially you do a numeric estimate of the value of a series of
some sort..

The site in the link offers some great help to a beginning programmer.

http://en.wikipedia.org/wiki/Main_Page
 
R

Richard Heathfield

Thomas said:
Hi, I'm pretty new to the programming world. I got stuck in the
following problem. Please guide me about it.

Well I'm trying to get the value of
(128^100)^2
27669029702758120146491942186874884129832195596687593922941153328644\
61503706121889747295812490836130522939753216599929386788554537297860\
72051766301814526769339210738412304501439841789615580511966398183532\
32501575334232440224365258413261224549068647496733893492858693838936\
01936689405612487962894280618800556342388396076741350978743071524441\
46728820186095678197423949799582894481473506157406815975220112285819\
60985268453376

I've used long double type but to no avail.
I even can't get the value of
2^128.
340282366920938463463374607431768211456

How this problem can be solved?

With programming.
Similarly, I was trying to develoop a program in hich we can calculate
the value of PI to our desired decimal point, but i got stuck in it.

#define PI 3.1415926535897932384626433

If you ever need more than that, it ain't for engineering, that's for
sure.
How these sort of problems can be solved in C?

Using programming and, if you're feeling lazy[1], using other people's
programming. Look up "bignums", or "Miracl", or "GMP", on the Web.
What tools C provides us to calculate such values?

+ - * / % =


[1] Laziness in a programmer isn't necessarily a bad thing.
 
K

Keith Thompson

Thomas said:
Well I'm trying to get the value of
(128^100)^2
I've used long double type but to no avail.
I even can't get the value of
2^128.
[...]

Presumably "^" means exponentiation (in C it means bitwise xor; C has
no exponentation operator).

2^128 should be well within the range of values representable by long
double (or even double) on most systems.

Show us the (exact!) code you used to attempt to calculate 2^128.

See also the pow() function, declared in <math.h>. It works on type
double; your implementation might also provide a powl() function that
works on type long double.

(128^100)^2, or 128^200, or 2^1400, is a much larger number, but even
that should be representable in long double on many systems; a binary
floating-point representation with an exponent of 12 or more bits
would suffice. See also the values of FLT_MAX, DBL_MAX, and LDBL_MAX
in <float.h>.

For even larger values, or if your implementation's type long double
supports a smaller range than mine does, you'll need to resort to
something like an extended-precision library. GNU GMP is one such
library.
 
M

Malcolm McLean

Thomas said:
Hi, I'm pretty new to the programming world. I got stuck in the
following problem. Please guide me about it.

Well I'm trying to get the value of
(128^100)^2
I've used long double type but to no avail.
I even can't get the value of
2^128.
How this problem can be solved?
Similarly, I was trying to develoop a program in hich we can calculate
the value of PI to our desired decimal point, but i got stuck in it.
How these sort of problems can be solved in C?
What tools C provides us to calculate such values?
I've got a program that calculates energetically favourable configurations
of molecules. The petide bond in a protein is planar due to delocalisation
of the the electrons round the neighbouring oxygen and various quantum
effects. I pointed out that our code actually provides an algorithm for
deriving the value of PI.
 
E

Eric Sosman

Thomas said:
Hi, I'm pretty new to the programming world. I got stuck in the
following problem. Please guide me about it.

Well I'm trying to get the value of
(128^100)^2

If by ^ you mean the same thing C does, the answer is 230.

If by ^ you mean exponentiation, the answer is

0x100000000000000000000000000000000000\
00000000000000000000000000000000000\
00000000000000000000000000000000000\
00000000000000000000000000000000000\
00000000000000000000000000000000000\
00000000000000000000000000000000000\
00000000000000000000000000000000000\
00000000000000000000000000000000000\
00000000000000000000000000000000000\
00000000000000000000000000000000000

(Both answers derived without benefit of a computer;
better double-check my mental arithmetic.)
 
P

pete

Richard Heathfield wrote:
#define PI 3.1415926535897932384626433

If you ever need more than that, it ain't for engineering, that's for
sure.

I think that you should have rounded that last digit up.
 
U

user923005

Hi, I'm pretty new to the programming world. I got stuck in the
following problem. Please guide me about it.

Well I'm trying to get the value of
(128^100)^2
I've used long double type but to no avail.
I even can't get the value of
2^128.
How this problem can be solved?
Similarly, I was trying to develoop a program in hich we can calculate
the value of PI to our desired decimal point, but i got stuck in it.
How these sort of problems can be solved in C?
What tools C provides us to calculate such values?

http://en.wikipedia.org/wiki/Software_for_calculating_π

Some of these have source code:
http://myownlittleworld.com/miscellaneous/computers/pilargetable.html

Finally, here's a cute quickie:
/* Calculation of pi to 32372 decimal digits */
/* Size of program: 152 characters */
/* After Dik T. Winter, CWI Amsterdam */
unsigned a = 1e4,
b,
c = 113316,
d,
e,
f[113316],
g,
h,
i;
int main(void)
{
for (; b = c, c -= 14; i = printf("%04d", e + d / a), e = d % a)
while (g = --b * 2)
d = h * b + a * (i ? f : a / 5), h = d / --g, f = d
- g * h;
return 0;
}
 
U

user923005

Hi, I'm pretty new to the programming world. I got stuck in the
following problem. Please guide me about it.
Well I'm trying to get the value of
(128^100)^2
I've used long double type but to no avail.
I even can't get the value of
2^128.
How this problem can be solved?
Similarly, I was trying to develoop a program in hich we can calculate
the value of PI to our desired decimal point, but i got stuck in it.
How these sort of problems can be solved in C?
What tools C provides us to calculate such values?

http://en.wikipedia.org/wiki/Software_for_calculating_π

Some of these have source code:http://myownlittleworld.com/miscellaneous/computers/pilargetable.html

Finally, here's a cute quickie:
/* Calculation of pi to 32372 decimal digits */
/* Size of program: 152 characters */
/* After Dik T. Winter, CWI Amsterdam */
unsigned a = 1e4,
b,
c = 113316,
d,
e,
f[113316],
g,
h,
i;
int main(void)
{
for (; b = c, c -= 14; i = printf("%04d", e + d / a), e = d % a)
while (g = --b * 2)
d = h * b + a * (i ? f : a / 5), h = d / --g, f = d
- g * h;
return 0;



}- Hide quoted text -

- Show quoted text -


Forgot the include file so that printf() behavior is defined:

/* Calculation of pi to 32372 decimal digits */
/* Size of program: 152 characters */
/* After Dik T. Winter, CWI Amsterdam */
#include <stdio.h>
unsigned a = 1e4,
b,
c = 113316,
d,
e,
f[113316],
g,
h,
i;
int main(void)
{
for (; b = c, c -= 14; i = printf("%04d", e + d / a), e = d % a)
while (g = --b * 2)
d = h * b + a * (i ? f : a / 5), h = d / --g, f = d
- g * h;
return 0;
}
 
T

Thomas

Finally, here's a cute quickie:
/* Calculation of pi to 32372 decimal digits */
/* Size of program: 152 characters */
/* After Dik T. Winter, CWI Amsterdam */
unsigned a = 1e4,
b,
c = 113316,
d,
e,
f[113316],
g,
h,
i;
int main(void)
{
for (; b = c, c -= 14; i = printf("%04d", e + d / a), e = d % a)
while (g = --b * 2)
d = h * b + a * (i ? f : a / 5), h = d / --g, f = d
- g * h;
return 0;

}- Hide quoted text -
- Show quoted text -

Forgot the include file so that printf() behavior is defined:

/* Calculation of pi to 32372 decimal digits */
/* Size of program: 152 characters */
/* After Dik T. Winter, CWI Amsterdam */
#include <stdio.h>
unsigned a = 1e4,
b,
c = 113316,
d,
e,
f[113316],
g,
h,
i;
int main(void)
{
for (; b = c, c -= 14; i = printf("%04d", e + d / a), e = d % a)
while (g = --b * 2)
d = h * b + a * (i ? f : a / 5), h = d / --g, f = d
- g * h;
return 0;

}


Thanks a lot. Now I have got the idea.
The resources i got from above links are enough to keep me occupied.
Thanks again.
 
T

Thomas

Finally, here's a cute quickie:
/* Calculation of pi to 32372 decimal digits */
/* Size of program: 152 characters */
/* After Dik T. Winter, CWI Amsterdam */
unsigned a = 1e4,
b,
c = 113316,
d,
e,
f[113316],
g,
h,
i;
int main(void)
{
for (; b = c, c -= 14; i = printf("%04d", e + d / a), e = d % a)
while (g = --b * 2)
d = h * b + a * (i ? f : a / 5), h = d / --g, f = d
- g * h;
return 0;

}- Hide quoted text -
- Show quoted text -

Forgot the include file so that printf() behavior is defined:

/* Calculation of pi to 32372 decimal digits */
/* Size of program: 152 characters */
/* After Dik T. Winter, CWI Amsterdam */
#include <stdio.h>
unsigned a = 1e4,
b,
c = 113316,
d,
e,
f[113316],
g,
h,
i;
int main(void)
{
for (; b = c, c -= 14; i = printf("%04d", e + d / a), e = d % a)
while (g = --b * 2)
d = h * b + a * (i ? f : a / 5), h = d / --g, f = d
- g * h;
return 0;

}


Thanks. now i've got enough resources.
Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top