473,417 Members | 1,566 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,417 software developers and data experts.

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 8852
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
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
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
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
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
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
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
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
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
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.