By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,206 Members | 1,035 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,206 IT Pros & Developers. It's quick & easy.

how to calculate value of pi

P: n/a
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?

Jul 14 '07 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Thomas wrote:
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.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jul 14 '07 #2

P: n/a
"Thomas" wrote:
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.heto
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
Jul 14 '07 #3

P: n/a
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
27669029702758120146491942186874884129832195596687 593922941153328644\
61503706121889747295812490836130522939753216599929 386788554537297860\
72051766301814526769339210738412304501439841789615 580511966398183532\
32501575334232440224365258413261224549068647496733 893492858693838936\
01936689405612487962894280618800556342388396076741 350978743071524441\
46728820186095678197423949799582894481473506157406 815975220112285819\
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.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 14 '07 #4

P: n/a
Thomas <my********************@gmail.comwrites:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 14 '07 #5

P: n/a

"Thomas" <my********************@gmail.comwrote in message
news:11**********************@22g2000hsm.googlegro ups.com...
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.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
Jul 14 '07 #6

P: n/a
Thomas wrote:
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.)

--
Eric Sosman
es*****@ieee-dot-org.invalid
Jul 14 '07 #7

P: n/a
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.

--
pete
Jul 14 '07 #8

P: n/a
On Jul 14, 6:40 am, Thomas <mynameisthomasander...@gmail.comwrote:
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/Softwar...ulating_%CF%80

Some of these have source code:
http://myownlittleworld.com/miscella...argetable.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[b] : a / 5), h = d / --g, f[b] = d
- g * h;
return 0;
}

Jul 16 '07 #9

P: n/a
On Jul 16, 12:50 pm, user923005 <dcor...@connx.comwrote:
On Jul 14, 6:40 am, Thomas <mynameisthomasander...@gmail.comwrote:
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/Softwar...ulating_%CF%80

Some of these have source code:http://myownlittleworld.com/miscella...argetable.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[b] : a / 5), h = d / --g, f[b] = 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[b] : a / 5), h = d / --g, f[b] = d
- g * h;
return 0;
}

Jul 16 '07 #10

P: n/a
On Jul 17, 12:51 am, user923005 <dcor...@connx.comwrote:
On Jul 16, 12:50 pm, user923005 <dcor...@connx.comwrote:
On Jul 14, 6:40 am, Thomas <mynameisthomasander...@gmail.comwrote:
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/Softwar...ulating_%CF%80
Some of these have source code:http://myownlittleworld.com/miscella...argetable.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[b] : a / 5), h = d / --g, f[b] = 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[b] : a / 5), h = d / --g, f[b] = 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.

Jul 17 '07 #11

P: n/a
On Jul 17, 12:51 am, user923005 <dcor...@connx.comwrote:
On Jul 16, 12:50 pm, user923005 <dcor...@connx.comwrote:
On Jul 14, 6:40 am, Thomas <mynameisthomasander...@gmail.comwrote:
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/Softwar...ulating_%CF%80
Some of these have source code:http://myownlittleworld.com/miscella...argetable.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[b] : a / 5), h = d / --g, f[b] = 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[b] : a / 5), h = d / --g, f[b] = d
- g * h;
return 0;

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

Jul 17 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.