c++ help?

No, you can't have a sip of my Java.

c++ help?

Postby armesty » Sun Jul 06, 2008 1:48 am

well i know this is mainly concerned for java, but i was wondering if anyone knew how to design a simple program to calculate pi to 100 places using c++ with just loops. i dont know any computer science so im clueless lol =/
armesty
Random Spammer
 
Posts: 51
Joined: Sun Feb 03, 2008 7:05 pm

Re: c++ help?

Postby Quelloquialism » Sat Jul 19, 2008 12:20 am

Unless I'm overlooking something obvious, that's actually a pretty hard thing to do.

See, in many modern programming languages (Java and C++ included), most numerical data is stored in one of the "primitive data types"--the most common of these are probably "int" (for 32-bit, or 4-byte, integer data) and "double" (for 64-bit, or 8-byte, real number data). The problem with these are that they have their limits; in usual circumstances you'd want to store a non-integer number like pi in a "double" type, but that won't give you 100 digits. Internally, the 64 bits allocated to each double are used in a fashion very similar to what you'd recognize as scientific notation, with a coefficient, a base (usually 10), and an exponent. The primary difference is that the base used in the scientific notation is 2 instead of the usual 10 (computers like binary, you know). Typically, the 64 bits will be used as follows: one bit tells whether the number is negative or positive, 52 bits describe the coefficient, and 11 bits describe the exponent. The part that will give us trouble in calculating something like pi is that 52-bit limit on the coefficient--you can think of this as giving a precision of roughly 2^52 in calculations (it's actually 2^53, but whatever, you get the idea). But you're asking for 100 digits of pi--that's similar to asking for a precision of 10^100.

Long story short, you can't store 100 digits of pi in a primitive data type, so you'd have to either attempt to implement an arbitrary-precision arithmetic library (that is, a program which uses these smaller data types to represent larger data types artificially--if you are familiar with Java's BigInteger or BigDecimal object classes, you know what I'm talking about), or you'd have to find a formula for calculating pi digit-by-digit (I'm not sure if one exists, to be honest). If explicit calculation of pi is not necessary, you could also store the digits of pi as a string in your program and simply print it instead (there are plenty of places # to download insane numbers of digits if you search on Google for it).

What is this program for? A class or something? Or just personal interest?
Quelloquialism
Celebrated Spammer
 
Posts: 534
Joined: Tue Oct 24, 2006 8:02 pm
Location: TopCoder Chat Room 1

Re: c++ help?

Postby armesty » Sun Jul 20, 2008 12:10 pm

umm, no it was for some type of charity thing i was doing. but dont worry, i was able to get it :to like 20k i think
armesty
Random Spammer
 
Posts: 51
Joined: Sun Feb 03, 2008 7:05 pm


Return to UIL Computer Science

Who is online

Users browsing this forum: No registered users and 1 guest

cron