473,503 Members | 1,609 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to calculate value of pi

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
11 8857
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
"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
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
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

"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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
9971
by: Building Blocks | last post by:
Hi, All I need is a simle calculate form script which contains this: A script that can handle text input, radio buttons, checkboxes, and dropdowns. Each one of these variables will contain a...
6
4116
by: Herrcho | last post by:
in K&R Chapter 6.3 it mentions two methods to calculate NKEYS. and points out the first one which is to terminate the list of initializers with a null pointer, then loop along keytab until the...
6
2065
by: luanhoxung | last post by:
dear all!! i met the headache problem in setting value for some controls in my form. i have 2 combo box in form F1. i want to set value for some textbox in F1 when i choose value from 2 combo...
2
1530
by: reidarT | last post by:
I have 3 fields in an aspx page. The 3. field should be the sum of field A and field B I use OnTextChanged to calculate the sum in field3. At the same time I want to insert the content of theese 3...
5
3994
chunk1978
by: chunk1978 | last post by:
hey... can anyone show me how to calculate sums to the 2nd decimal only? like: 5.50 + 4.00 = 9.50 i seriously have zero idea and could defo use some help...thanks! <script...
5
2615
by: Michael | last post by:
Hi. I need dinamically calculate input text field based on parent static TD before showing content of input. Please, advice. Michael
3
2924
by: coolguyraj | last post by:
I have a javascript code to take value from two text boxes and calculate on triggering the "OnBlur" function and display in the third box. The code works fine with one line item,If i have more...
6
4081
by: rrstudio2 | last post by:
I am using the following vba code to calculate the median of a table in MS Access: Public Function MedianOfRst(RstName As String, fldName As String) As Double 'This function will calculate the...
4
5662
by: tvnaidu | last post by:
How to calculate Prescalar and Modulus values for PIT (Programmable Interrupt Timer). I have microcontroller running at 60MHz. I am thinking of writing 1 milli sec ISR for PIT. I need to calculate...
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7086
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7280
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7332
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
382
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.