How do you write an infinite precision number class?

P

Protoman

Hi!!! Protoman here, I need to write an infinite precision number class
b/c I want to compute pi. Give me some sample code. Also, when I run my
program and it computes pi, will my computer freeze b/c it's infinite
precision? Thanks for the help!
 
M

Markus Moll

Hi
Hi!!! Protoman here, I need to write an infinite precision number class
b/c I want to compute pi. Give me some sample code.

Give me all your money.
Also, when I run my
program and it computes pi, will my computer freeze b/c it's infinite
precision? Thanks for the help!

Seriously...
1) you certainly want arbitrary precision, not infinite precision
2) you should definitely do your homework on your own
3) if you get stuck, feel free to ask a more specific question

Good luck
Markus
 
K

Kai-Uwe Bux

Protoman said:
Hi!!! Protoman here,

Hi, pleased to meet you.

I need to write an infinite precision number class
b/c I want to compute pi.

Non sequitur. If you want to compute pi, all you need is to use an arbitrary
precision library. There are some available, and Google is your friend.

Give me some sample code.

Request denied: be more polite.

Also, when I run my program and it computes pi, will my computer freeze
b/c it's infinite precision?

Yes, an infinite precission calculation will freeze your computer and the
whole universe along with it.

Seriously, if you use a high/arbitrary precision library, it will allow you
to specify the precision you want. Then the program will run and either
print out a highly accurate approximation to pi or it will die on you.
Whether your computer freezes in the process depends on your operating
system and how it handles programs that have an ever growing hunger for
memory.

Thanks for the help!

You are very welcome.


Best

Kai-Uwe Bux
 
P

Protoman

OK, let me rephrase this:
I'd like to write an arbitary precision number class[I'd like to be
able to specify the precision in digits or bits] because my co. won't
buy any commercial classes[the procurement dept. are cheapskates].
We're writing a program for a NASA sys. that requires us to calc pi to
1,000,000,000+ digits. Don't worry about size; it'll be running on a
Cray. We're also going to use it to replace or supplant the built in
number types. If you can give me some sample code, that'd be great.
Thanks for the help.
 
J

Jon Slaughter

Kai-Uwe Bux said:
Hi, pleased to meet you.



Non sequitur. If you want to compute pi, all you need is to use an
arbitrary
precision library. There are some available, and Google is your friend.



Request denied: be more polite.



Yes, an infinite precission calculation will freeze your computer and the
whole universe along with it.

lol

Seriously, if you use a high/arbitrary precision library, it will allow
you
to specify the precision you want. Then the program will run and either
print out a highly accurate approximation to pi or it will die on you.
Whether your computer freezes in the process depends on your operating
system and how it handles programs that have an ever growing hunger for
memory.



You are very welcome.


Best

Kai-Uwe Bux
 
J

Jon Slaughter

Protoman said:
OK, let me rephrase this:
I'd like to write an arbitary precision number class[I'd like to be
able to specify the precision in digits or bits] because my co. won't
buy any commercial classes[the procurement dept. are cheapskates].
We're writing a program for a NASA sys. that requires us to calc pi to
1,000,000,000+ digits. Don't worry about size; it'll be running on a
Cray. We're also going to use it to replace or supplant the built in
number types. If you can give me some sample code, that'd be great.
Thanks for the help.

OMG, you writing code for Nasa?
 
K

Kai-Uwe Bux

Protoman said:
OK, let me rephrase this:
I'd like to write an arbitary precision number class[I'd like to be
able to specify the precision in digits or bits] because my co. won't
buy any commercial classes[the procurement dept. are cheapskates].

Use a free library. There are those as well. At least you can use them to do
a quick and dirty implementation of the class to be replaced later. That
way, your colleagues can move on with the rest of the program.

We're writing a program for a NASA sys. that requires us to calc pi to
1,000,000,000+ digits.

a ) Why would NASA need that? The known universe has a diameter on the order
of 10^28m. Thus, an error in an angle in the 50s digit after the decimal
point will project to a distance way less than the diameter of an electron
at the other end of the universe. No real world application has a need for
more than 50 digits of pi.

b) If NASA does not like open source, have them pay for the library.

Don't worry about size; it'll be running on a Cray.

No need for that. There is a digit extraction algorithm for pi. You can
produce 1,000,000,000 digits and more on any modern day PC.

We're also going to use it to replace or supplant the built in
number types. If you can give me some sample code, that'd be great.
Thanks for the help.

Well, if you really want to code that beast on your own, the best way to
start is probably still to read the relevant chapters in

D.E. Knuth: The art of computer programming Vol 2
(seminumerical algorithms)

Then, start with coding an arbitrary size unsigned int class. A cheap way to
go would be to do something like:

class Cardinal {
std::vector< unsigned long > digit;
public:
...
}; // class Cardinal

later you can use that and build a float class like so:

template < unsigned long Digits = 100 > {
class Real {

bool positive;
Cardinal mantissa;
signed long exponent;
public:
...
}; // Real<>

It does not make sense to discuss the "..." parts unless you settled on
certain algorithms.


Best

Kai-Uwe Bux
 
P

Protoman

Yes really; I'm the asst. head of systems programming in the R&D Dept.
We're calculating thruster angles for a prototype ion drive engine.
 
J

Jim Langston

Protoman said:
OK, but how do I actually *code* it? The precision alogorithm, I mean.

Please do me a favor. Make sure this rocket that uses this ion drive is not
launched from California. Or let us know when it's launched so I can find
some underground bunker somewhere.
 
P

Protoman

It's not a missile; it's a space probe. And how do I code the
prescision algorithm and conversion functs; I can figure out how to do
just about everything else but constructor, destructor, and copy
constructor. Thanks.
 
P

Protoman

Hey, it happens :) It's one of our top resources. Anyway, back to the
*original* topic. Please.
 
K

Kai-Uwe Bux

Protoman said:
OK, but how do I actually *code* it? The precision alogorithm, I mean.

Before you do the algorithms, settle on a representation. I suggested to use
a std::vector< unsigned long >. For the sake of this discussion, let us go
with that for a while. The idea is that the vector represents a large
number in base 2^32 (I assume for the sake of this discussion that unsigned
long has 32 bits, which may or may not be true for your platform).

Now, you need to implement basic arithmetic for that:

addition,
subtraction,
multiplication,
division


Addition is kind of easy, just start with the least significant digits and
add them. The result is the least significant digit of the sum. If this is
smaller than one of the input digits, a carry occurred. Now move one to the
next digit. Iterate.

Subtraction is essentially the same.

For multiplication, there are choices. You can probably do a straight
forward implementation of the multiplication algorithm that you learned in
elementary school. But that will be slow. Faster methods involve smart
tricks and serious mathematics (including modular arithmetic and discrete
Fourier transform).

Division can be reduced to multiplication. This is tricky but explained in
TAOCP.


Something not to be forgotten: radix conversion in case you want to output
your results. Here is a piece of code that tries to minimize the use of
divisions. It performs reasonably well on something like 1000 digits, but
is very slow compared to libraries available:



namespace DO_NOT_USE {

template < typename T, typename S >
void radix ( T const & num,
std::vector< S > & stack,
std::vector< T > const & power,
typename std::vector< T >::size_type const & power_index,
bool do_fill )
{
if ( power_index == 0 ) {
// one digit only:
stack.push_back( S( num ) );
} else {
typename std::vector< S >::size_type start = stack.size();
typename std::vector< T >::size_type index = power_index - 1;
T q = num / power[ index ];
T r = num - ( q * power[ index ] );
if ( q == T(0) ) {
radix( r, stack, power, index, false );
} else {
radix( r, stack, power, index, true );
radix( q, stack, power, index, false );
}
if ( do_fill ) {
while ( static_cast<typename std::vector< S >::size_type>
( stack.size() - start )
<
static_cast<typename std::vector< S >::size_type>
( 1 << power_index ) ) {
stack.push_back( S(0) );
}
}
}
}

} // namespace DO_NOT_USE

template < typename T, typename S >
std::vector< S > radix ( T const & num, S const & base ) {
std::vector< T > power;
std::vector< S > result;

T current_power = T(base);
T q = num / current_power;
power.push_back( current_power );
while ( current_power < q ) {
q /= current_power;
current_power *= current_power;
power.push_back( current_power );
}
T r = num - ( q*current_power );
if ( q == T(0) ) {
DO_NOT_USE::radix( r, result, power, power.size()-1, false );
} else {
DO_NOT_USE::radix( r, result, power, power.size()-1, true );
DO_NOT_USE::radix( q, result, power, power.size()-1, false );
}
return( result );
}

Here T is supposed to be a high-precision cardinal type and S is some other
arithmetic type (like unsigned short). We assume that T can be constructed
from S and converted to S for values in the range of S.


Finally, floating point arithmetic can be reduced to arithmetic of
cardinals.


Also, let me re-iterate that you should use a library. This kind of code is
very time consuming to write, hard to get right, difficult to test, and it
is close to impossible to outperform state of the art implementations.


Best

Kai-Uwe Bux
 
J

Jon Slaughter

Protoman said:
It's not a missile; it's a space probe. And how do I code the
prescision algorithm and conversion functs; I can figure out how to do
just about everything else but constructor, destructor, and copy
constructor. Thanks.

Don't take this the wrong way, but why would Nasa invest so much money in
creating this thing is the people who code it cannot "think" for themselfs?
You don't sound as if you know alot about math(or programming) because if
you did you would know that pi is a irrational number and irrational numbers
have no finite representation(by definition)... hence you cannot represent
pi exactly in a computer... you would also know from a engineering point of
view that rarely is pi needed to more than 15 decimal places... and I'm sure
that 1M places is just a tad overkill.

It just seems that your programming experience is a little low for Nasa and
I'd definately not want you working on anything that is going to cost the
american people 100's of millions of dollars. You just don't sound like you
have what it takes to be "part" of Nasa... and if thats the kinda people(No
offense though) that nasa is highering then no wonder it's having so many
problems.

Anyways, there are plenty of archives with pi calculated to more than enough
decimal places then you or nasa will ever need.

But just incase you need 1.24 trillion decimal places of pi I'm sure you can
use your NASA creditials to get a copy of them with no problem.

I guess you are going to try to prove that pi is finite or something by
using some algorithm to find the last digit?


BTW, I will mention that there is an algorithm to determine the nth digit of
pi without finding the n-1 digits... if you do a little homework on google
you can easily find it.

Jon
 
K

Kai-Uwe Bux

Jon said:
Don't take this the wrong way, but why would Nasa invest so much money in
creating this thing is the people who code it cannot "think" for
themselfs? You don't sound as if you know alot about math(or programming)
because if you did you would know that pi is a irrational number and
irrational numbers have no finite representation(by definition)...

Well, the OP was aware of that problem. Recall that he specifically asked
for an *infinite precision* class.

Seriously though, there are some irrational numbers that do have finite
representations. E.g., I think algebraic numbers can be manipulated exactly
on a computer. Also, there is the notion of a recursive real number
(essentially that is a number whose decimal expansion can be computed by a
recursive function). It turns out that

1) recursive numbers support arithmetic operations.
2) pi is a recursive number.
BTW, I will mention that there is an algorithm to determine the nth digit
of pi without finding the n-1 digits... if you do a little homework on
google you can easily find it.

This actually is one way to prove 2).



Best

Kai-Uwe Bux
 
P

Protoman

I'm using the pi thing because NASA does want an arbitrary precision
class, besides, thats what were using it for right now. And I just
write the first draft.
 
J

John Harrison

Protoman said:
It's not a missile; it's a space probe. And how do I code the
prescision algorithm and conversion functs; I can figure out how to do
just about everything else but constructor, destructor, and copy
constructor. Thanks.

Use a vector for the bits of your number.

By using a vector you don't have to worry about destructors and copy
constructors because the compiler generated versions will work fine.

john
 
J

Jon Slaughter

Kai-Uwe Bux said:
Well, the OP was aware of that problem. Recall that he specifically asked
for an *infinite precision* class.

Seriously though, there are some irrational numbers that do have finite
representations. E.g., I think algebraic numbers can be manipulated
exactly
on a computer. Also, there is the notion of a recursive real number
(essentially that is a number whose decimal expansion can be computed by a
recursive function). It turns out that

Not really. In any base that is rational you can't write an irrational as a
finite decimal representation.

This is actually the definition of an irrational.

The irrationals are the completion of the rational field and rational
numbers are defined, atleast one way, as terminating decimals or repeating
decimals.


1) recursive numbers support arithmetic operations.
2) pi is a recursive number.


This actually is one way to prove 2).

Heh, well I don't know what a recursive number is though ;) Maybe one who's
digits can be recursively generated?
Best

Kai-Uwe Bux

Jon
 
J

Jon Slaughter

Protoman said:
I'm using the pi thing because NASA does want an arbitrary precision
class, besides, thats what were using it for right now. And I just
write the first draft.

So you are kinda like an intern? If I write the class for you and you get
hired by them will you give me your bonus?

Jon
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top